TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ObjectRegistry.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef TRINITY_OBJECTREGISTRY_H
20 #define TRINITY_OBJECTREGISTRY_H
21 
22 #include "Define.h"
23 
24 #include <string>
25 #include <map>
26 #include <vector>
27 
30 template<class T, class Key = std::string>
32 {
33  public:
34  typedef std::map<Key, T*> RegistryMapType;
35 
37  {
39  return &instance;
40  }
41 
43  const T* GetRegistryItem(Key key) const
44  {
45  typename RegistryMapType::const_iterator iter = i_registeredObjects.find(key);
46  return( iter == i_registeredObjects.end() ? NULL : iter->second );
47  }
48 
50  bool InsertItem(T *obj, Key key, bool _override = false)
51  {
52  typename RegistryMapType::iterator iter = i_registeredObjects.find(key);
53  if ( iter != i_registeredObjects.end() )
54  {
55  if ( !_override )
56  return false;
57  delete iter->second;
58  i_registeredObjects.erase(iter);
59  }
60 
61  i_registeredObjects[key] = obj;
62  return true;
63  }
64 
66  void RemoveItem(Key key, bool delete_object = true)
67  {
68  typename RegistryMapType::iterator iter = i_registeredObjects.find(key);
69  if ( iter != i_registeredObjects.end() )
70  {
71  if ( delete_object )
72  delete iter->second;
73  i_registeredObjects.erase(iter);
74  }
75  }
76 
78  bool HasItem(Key key) const
79  {
80  return (i_registeredObjects.find(key) != i_registeredObjects.end());
81  }
82 
84  unsigned int GetRegisteredItems(std::vector<Key> &l) const
85  {
86  unsigned int sz = l.size();
87  l.resize(sz + i_registeredObjects.size());
88  for (typename RegistryMapType::const_iterator iter = i_registeredObjects.begin(); iter != i_registeredObjects.end(); ++iter)
89  l[sz++] = iter->first;
90  return i_registeredObjects.size();
91  }
92 
94  RegistryMapType const &GetRegisteredItems() const
95  {
96  return i_registeredObjects;
97  }
98 
101  {
102  for (typename RegistryMapType::iterator iter=i_registeredObjects.begin(); iter != i_registeredObjects.end(); ++iter)
103  delete iter->second;
104  i_registeredObjects.clear();
105  }
106  private:
107  RegistryMapType i_registeredObjects;
108 };
109 
110 #endif
RegistryMapType const & GetRegisteredItems() const
Return the map of registered items.
Definition: ObjectRegistry.h:94
Definition: ObjectRegistry.h:31
bool InsertItem(T *obj, Key key, bool _override=false)
Inserts a registry item.
Definition: ObjectRegistry.h:50
const T * GetRegistryItem(Key key) const
Returns a registry item.
Definition: ObjectRegistry.h:43
std::map< Key, T * > RegistryMapType
Definition: ObjectRegistry.h:34
arena_t NULL
Definition: jemalloc_internal.h:624
void RemoveItem(Key key, bool delete_object=true)
Removes a registry item.
Definition: ObjectRegistry.h:66
RegistryMapType i_registeredObjects
Definition: ObjectRegistry.h:107
~ObjectRegistry()
Definition: ObjectRegistry.h:100
static ObjectRegistry< T, Key > * instance()
Definition: ObjectRegistry.h:36
bool HasItem(Key key) const
Returns true if registry contains an item.
Definition: ObjectRegistry.h:78
ObjectRegistry()
Definition: ObjectRegistry.h:99
unsigned int GetRegisteredItems(std::vector< Key > &l) const
Inefficiently return a vector of registered items.
Definition: ObjectRegistry.h:84