1 | /* |
---|
2 | * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/> |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation; either version 2 of the License, or |
---|
7 | * (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program; if not, write to the Free Software |
---|
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | */ |
---|
18 | |
---|
19 | #ifndef MANGOS_OBJECTREGISTRY_H |
---|
20 | #define MANGOS_OBJECTREGISTRY_H |
---|
21 | |
---|
22 | #include "Platform/Define.h" |
---|
23 | #include "Utilities/HashMap.h" |
---|
24 | #include "Policies/Singleton.h" |
---|
25 | |
---|
26 | #include <string> |
---|
27 | #include <map> |
---|
28 | |
---|
29 | /** ObjectRegistry holds all registry item of the same type |
---|
30 | */ |
---|
31 | template<class T, class Key = std::string> |
---|
32 | class MANGOS_DLL_DECL ObjectRegistry |
---|
33 | { |
---|
34 | public: |
---|
35 | typedef std::map<Key, T *> RegistryMapType; |
---|
36 | |
---|
37 | /// Returns a registry item |
---|
38 | const T* GetRegistryItem(Key key) const |
---|
39 | { |
---|
40 | typename RegistryMapType::const_iterator iter = i_registeredObjects.find(key); |
---|
41 | return( iter == i_registeredObjects.end() ? NULL : iter->second ); |
---|
42 | } |
---|
43 | |
---|
44 | /// Inserts a registry item |
---|
45 | bool InsertItem(T *obj, Key key, bool override = false) |
---|
46 | { |
---|
47 | typename RegistryMapType::iterator iter = i_registeredObjects.find(key); |
---|
48 | if( iter != i_registeredObjects.end() ) |
---|
49 | { |
---|
50 | if( !override ) |
---|
51 | return false; |
---|
52 | delete iter->second; |
---|
53 | i_registeredObjects.erase(iter); |
---|
54 | } |
---|
55 | |
---|
56 | i_registeredObjects[key] = obj; |
---|
57 | return true; |
---|
58 | } |
---|
59 | |
---|
60 | /// Removes a registry item |
---|
61 | void RemoveItem(Key key, bool delete_object = true) |
---|
62 | { |
---|
63 | typename RegistryMapType::iterator iter = i_registeredObjects.find(key); |
---|
64 | if( iter != i_registeredObjects.end() ) |
---|
65 | { |
---|
66 | if( delete_object ) |
---|
67 | delete iter->second; |
---|
68 | i_registeredObjects.erase(iter); |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | /// Returns true if registry contains an item |
---|
73 | bool HasItem(Key key) const |
---|
74 | { |
---|
75 | return (i_registeredObjects.find(key) != i_registeredObjects.end()); |
---|
76 | } |
---|
77 | |
---|
78 | /// Inefficiently return a vector of registered items |
---|
79 | unsigned int GetRegisteredItems(std::vector<Key> &l) const |
---|
80 | { |
---|
81 | unsigned int sz = l.size(); |
---|
82 | l.resize(sz + i_registeredObjects.size()); |
---|
83 | for(typename RegistryMapType::const_iterator iter = i_registeredObjects.begin(); iter != i_registeredObjects.end(); ++iter) |
---|
84 | l[sz++] = iter->first; |
---|
85 | return i_registeredObjects.size(); |
---|
86 | } |
---|
87 | |
---|
88 | /// Return the map of registered items |
---|
89 | RegistryMapType const &GetRegisteredItems() const |
---|
90 | { |
---|
91 | return i_registeredObjects; |
---|
92 | } |
---|
93 | |
---|
94 | private: |
---|
95 | RegistryMapType i_registeredObjects; |
---|
96 | friend class MaNGOS::OperatorNew<ObjectRegistry<T, Key> >; |
---|
97 | |
---|
98 | // protected for friend use since it should be a singleton |
---|
99 | ObjectRegistry() {} |
---|
100 | ~ObjectRegistry() |
---|
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 | }; |
---|
107 | #endif |
---|