From eb8650fc14c960520848bc3e215eb90d74f2041d Mon Sep 17 00:00:00 2001 From: Adam Frisby Date: Mon, 24 Nov 2008 12:58:41 +0000 Subject: * Adding EntityManager.cs * Not referenced anywhere yet. * Designed to replace Dictionary within SceneGraph.Entities * Allows indexed access and better handling of locks. * Someone needs to implement IEnumerable on this before we can switch it over. --- OpenSim/Region/Environment/Scenes/EntityManager.cs | 148 +++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 OpenSim/Region/Environment/Scenes/EntityManager.cs (limited to 'OpenSim/Region/Environment/Scenes/EntityManager.cs') diff --git a/OpenSim/Region/Environment/Scenes/EntityManager.cs b/OpenSim/Region/Environment/Scenes/EntityManager.cs new file mode 100644 index 0000000..f515cba --- /dev/null +++ b/OpenSim/Region/Environment/Scenes/EntityManager.cs @@ -0,0 +1,148 @@ +using System; +using System.Collections.Generic; +using OpenMetaverse; + +namespace OpenSim.Region.Environment.Scenes +{ + public class EntityManager + { + private readonly Dictionary m_eb_uuid = new Dictionary(); + private readonly Dictionary m_eb_localID = new Dictionary(); + private readonly Object m_lock = new Object(); + + [Obsolete("Use Add() instead.")] + public void Add(UUID id, EntityBase eb) + { + Add(eb); + } + + public void Add(EntityBase entity) + { + lock(m_lock) + { + m_eb_uuid.Add(entity.UUID, entity); + m_eb_localID.Add(entity.LocalId, entity); + } + } + + public void InsertOrReplace(EntityBase entity) + { + lock(m_lock) + { + m_eb_uuid[entity.UUID] = entity; + m_eb_localID[entity.LocalId] = entity; + } + } + + public void Clear() + { + lock (m_lock) + { + m_eb_uuid.Clear(); + m_eb_localID.Clear(); + } + } + + public int Count + { + get + { + lock (m_lock) + { + return m_eb_uuid.Count; + } + } + } + + public bool ContainsKey(UUID id) + { + lock(m_lock) + { + return m_eb_uuid.ContainsKey(id); + } + } + + public bool ContainsKey(uint localID) + { + lock (m_lock) + { + return m_eb_localID.ContainsKey(localID); + } + } + + public void Remove(uint localID) + { + lock(m_lock) + { + m_eb_uuid.Remove(m_eb_localID[localID].UUID); + m_eb_localID.Remove(localID); + } + } + + public void Remove(UUID id) + { + lock(m_lock) + { + m_eb_localID.Remove(m_eb_uuid[id].LocalId); + m_eb_uuid.Remove(id); + } + } + + public List GetAllByType() + { + List tmp = new List(); + + lock(m_lock) + { + foreach (KeyValuePair pair in m_eb_uuid) + { + if(pair.Value is T) + { + tmp.Add(pair.Value); + } + } + } + + return tmp; + } + + [Obsolete("Please used indexed access to this instead. Indexes can be accessed via EntitiesManager[index]. LocalID and UUID are supported.")] + public List GetEntities() + { + lock (m_lock) + { + return new List(m_eb_uuid.Values); + } + } + + public EntityBase this[UUID id] + { + get + { + lock (m_lock) + { + return m_eb_uuid[id]; + } + } + set + { + InsertOrReplace(value); + } + } + + public EntityBase this[uint localID] + { + get + { + lock (m_lock) + { + return m_eb_localID[localID]; + } + } + set + { + InsertOrReplace(value); + } + } + } +} -- cgit v1.1