root/trunk/src/framework/Dynamic/ObjectRegistry.h @ 206

Revision 206, 3.7 kB (checked in by yumileroy, 17 years ago)

[svn] * Switch from hashmap to unordered map. - cleanup source - mangos. Help - Aokromes

Original author: KingPin?
Date: 2008-11-10 06:53:00-06:00

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