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

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

[svn] * Improve some arena team related DB access
* Cache GM tickets on server startup.
* Remove unused src/game/HateMatrix.h and references.
* Better check client inventory pos data received in some client packets to
skip invalid cases

Original author: KingPin?
Date: 2008-11-10 09:04:23-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 <vector>
30#include <map>
31
32/** ObjectRegistry holds all registry item of the same type
33 */
34template<class T, class Key = std::string>
35class TRINITY_DLL_DECL ObjectRegistry
36{
37    public:
38        typedef std::map<Key, T *> RegistryMapType;
39
40        /// Returns a registry item
41        const T* GetRegistryItem(Key key) const
42        {
43            typename RegistryMapType::const_iterator iter = i_registeredObjects.find(key);
44            return( iter == i_registeredObjects.end() ? NULL : iter->second );
45        }
46
47        /// Inserts a registry item
48        bool InsertItem(T *obj, Key key, bool override = false)
49        {
50            typename RegistryMapType::iterator iter = i_registeredObjects.find(key);
51            if( iter != i_registeredObjects.end() )
52            {
53                if( !override )
54                    return false;
55                delete iter->second;
56                i_registeredObjects.erase(iter);
57            }
58
59            i_registeredObjects[key] = obj;
60            return true;
61        }
62
63        /// Removes a registry item
64        void RemoveItem(Key key, bool delete_object = true)
65        {
66            typename RegistryMapType::iterator iter = i_registeredObjects.find(key);
67            if( iter != i_registeredObjects.end() )
68            {
69                if( delete_object )
70                    delete iter->second;
71                i_registeredObjects.erase(iter);
72            }
73        }
74
75        /// Returns true if registry contains an item
76        bool HasItem(Key key) const
77        {
78            return (i_registeredObjects.find(key) != i_registeredObjects.end());
79        }
80
81        /// Inefficiently return a vector of registered items
82        unsigned int GetRegisteredItems(std::vector<Key> &l) const
83        {
84            unsigned int sz = l.size();
85            l.resize(sz + i_registeredObjects.size());
86            for(typename RegistryMapType::const_iterator iter = i_registeredObjects.begin(); iter != i_registeredObjects.end(); ++iter)
87                l[sz++] = iter->first;
88            return i_registeredObjects.size();
89        }
90
91        /// Return the map of registered items
92        RegistryMapType const &GetRegisteredItems() const
93        {
94            return i_registeredObjects;
95        }
96
97    private:
98        RegistryMapType i_registeredObjects;
99        friend class Trinity::OperatorNew<ObjectRegistry<T, Key> >;
100
101        // protected for friend use since it should be a singleton
102        ObjectRegistry() {}
103        ~ObjectRegistry()
104        {
105            for(typename RegistryMapType::iterator iter=i_registeredObjects.begin(); iter != i_registeredObjects.end(); ++iter)
106                delete iter->second;
107            i_registeredObjects.clear();
108        }
109};
110#endif
Note: See TracBrowser for help on using the browser.