TrinityCore
ObjectRegistry.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef TRINITY_OBJECTREGISTRY_H
19#define TRINITY_OBJECTREGISTRY_H
20
21#include <string>
22#include <map>
23#include <memory>
24
27template<class T, class Key = std::string>
28class ObjectRegistry final
29{
30 public:
31 typedef std::map<Key, std::unique_ptr<T>> RegistryMapType;
32
34 {
36 return &instance;
37 }
38
40 T const* GetRegistryItem(Key const& key) const
41 {
42 auto itr = _registeredObjects.find(key);
43 if (itr == _registeredObjects.end())
44 return nullptr;
45 return itr->second.get();
46 }
47
49 bool InsertItem(T* obj, Key const& key, bool force = false)
50 {
51 auto itr = _registeredObjects.find(key);
52 if (itr != _registeredObjects.end())
53 {
54 if (!force)
55 return false;
56 _registeredObjects.erase(itr);
57 }
58
59 _registeredObjects.emplace(std::piecewise_construct, std::forward_as_tuple(key), std::forward_as_tuple(obj));
60 return true;
61 }
62
64 bool HasItem(Key const& key) const
65 {
66 return (_registeredObjects.count(key) > 0);
67 }
68
71 {
72 return _registeredObjects;
73 }
74
75 private:
77
78 // non instanceable, only static
83};
84
85#endif
ObjectRegistry & operator=(ObjectRegistry const &)=delete
bool HasItem(Key const &key) const
Returns true if registry contains an item.
ObjectRegistry(ObjectRegistry const &)=delete
T const * GetRegistryItem(Key const &key) const
Returns a registry item.
bool InsertItem(T *obj, Key const &key, bool force=false)
Inserts a registry item.
RegistryMapType _registeredObjects
static ObjectRegistry< T, Key > * instance()
std::map< Key, std::unique_ptr< T > > RegistryMapType
RegistryMapType const & GetRegisteredItems() const
Return the map of registered items.