TrinityCore
Loading...
Searching...
No Matches
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 "Define.h"
22
23#include <string>
24#include <map>
25#include <vector>
26#include <memory>
27
30template<class T, class Key = std::string>
31class ObjectRegistry final
32{
33 public:
34 typedef std::map<Key, std::unique_ptr<T>> RegistryMapType;
35
37 {
39 return &instance;
40 }
41
43 T const* GetRegistryItem(Key const& key) const
44 {
45 auto itr = _registeredObjects.find(key);
46 if (itr == _registeredObjects.end())
47 return nullptr;
48 return itr->second.get();
49 }
50
52 bool InsertItem(T* obj, Key const& key, bool force = false)
53 {
54 auto itr = _registeredObjects.find(key);
55 if (itr != _registeredObjects.end())
56 {
57 if (!force)
58 return false;
59 _registeredObjects.erase(itr);
60 }
61
62 _registeredObjects.emplace(std::piecewise_construct, std::forward_as_tuple(key), std::forward_as_tuple(obj));
63 return true;
64 }
65
67 bool HasItem(Key const& key) const
68 {
69 return (_registeredObjects.count(key) > 0);
70 }
71
74 {
75 return _registeredObjects;
76 }
77
78 private:
80
81 // non instanceable, only static
86};
87
88#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.