TrinityCore
TypeContainer.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_TYPECONTAINER_H
19#define TRINITY_TYPECONTAINER_H
20
21/*
22 * Here, you'll find a series of containers that allow you to hold multiple
23 * types of object at the same time.
24 */
25
26#include <unordered_map>
27#include "Define.h"
28#include "Dynamic/TypeList.h"
29#include "GridRefManager.h"
30
31/*
32 * @class ContainerMapList is a mulit-type container for map elements
33 * By itself its meaningless but collaborate along with TypeContainers,
34 * it become the most powerfully container in the whole system.
35 */
36template<class OBJECT>
38{
40};
41
42template<>
43struct ContainerMapList<TypeNull> /* nothing is in type null */
44{
45};
46
47template<class H, class T>
49{
52};
53
54template<class OBJECT, class KEY_TYPE>
56{
57 std::unordered_map<KEY_TYPE, OBJECT*> _element;
58};
59
60template<class KEY_TYPE>
61struct ContainerUnorderedMap<TypeNull, KEY_TYPE>
62{
63};
64
65template<class H, class T, class KEY_TYPE>
66struct ContainerUnorderedMap<TypeList<H, T>, KEY_TYPE>
67{
70};
71
73
74/*
75 * @class TypeMapContainer contains a fixed number of types and is
76 * determined at compile time. This is probably the most complicated
77 * class and do its simplest thing, that is, holds objects
78 * of different types.
79 */
80
81template<class OBJECT_TYPES>
83{
84public:
87 TypeMapContainer(TypeMapContainer&&) noexcept = default;
88 TypeMapContainer& operator=(TypeMapContainer const&) = default;
89 TypeMapContainer& operator=(TypeMapContainer&&) noexcept = default;
91
92 template<class SPECIFIC_TYPE>
93 size_t Count() const;
94
96 template<class SPECIFIC_TYPE>
97 bool insert(SPECIFIC_TYPE *obj);
98
100 //template<class SPECIFIC_TYPE>
101 //bool remove(SPECIFIC_TYPE* obj)
102 //{
103 // SPECIFIC_TYPE* t = Trinity::Remove(i_elements, obj);
104 // return (t != nullptr);
105 //}
106
107 ContainerMapList<OBJECT_TYPES>& GetElements(void);
108 const ContainerMapList<OBJECT_TYPES>& GetElements(void) const;
109
110private:
112};
113
114template <class OBJECT_TYPES>
115TypeMapContainer<OBJECT_TYPES>::TypeMapContainer() = default;
116
117template <class OBJECT_TYPES>
118TypeMapContainer<OBJECT_TYPES>::~TypeMapContainer() = default;
119
120template <class OBJECT_TYPES>
121template <class SPECIFIC_TYPE>
122size_t TypeMapContainer<OBJECT_TYPES>::Count() const
123{
124 return Trinity::Count(i_elements, (SPECIFIC_TYPE*)nullptr);
125}
126
127template <class OBJECT_TYPES>
128template <class SPECIFIC_TYPE>
130{
131 SPECIFIC_TYPE* t = Trinity::Insert(i_elements, obj);
132 return (t != nullptr);
133}
134
135template <class OBJECT_TYPES>
137{
138 return i_elements;
139}
140
141template <class OBJECT_TYPES>
143{
144 return i_elements;
145}
146
147template<class OBJECT_TYPES, class KEY_TYPE>
149{
150public:
155 TypeUnorderedMapContainer& operator=(TypeUnorderedMapContainer&&) noexcept = default;
157
158 template<class SPECIFIC_TYPE>
159 bool Insert(KEY_TYPE const& handle, SPECIFIC_TYPE* obj);
160
161 template<class SPECIFIC_TYPE>
162 bool Remove(KEY_TYPE const& handle);
163
164 template<class SPECIFIC_TYPE>
165 SPECIFIC_TYPE* Find(KEY_TYPE const& handle);
166
167 template<class SPECIFIC_TYPE>
168 std::size_t Size() const;
169
170 ContainerUnorderedMap<OBJECT_TYPES, KEY_TYPE>& GetElements();
171 ContainerUnorderedMap<OBJECT_TYPES, KEY_TYPE> const& GetElements() const;
172
173private:
174 ContainerUnorderedMap<OBJECT_TYPES, KEY_TYPE> _elements;
175};
176
177template <class OBJECT_TYPES, class KEY_TYPE>
178TypeUnorderedMapContainer<OBJECT_TYPES, KEY_TYPE>::TypeUnorderedMapContainer() = default;
179
180template <class OBJECT_TYPES, class KEY_TYPE>
181TypeUnorderedMapContainer<OBJECT_TYPES, KEY_TYPE>::~TypeUnorderedMapContainer() = default;
182
183template <class OBJECT_TYPES, class KEY_TYPE>
184template <class SPECIFIC_TYPE>
185bool TypeUnorderedMapContainer<OBJECT_TYPES, KEY_TYPE>::Insert(KEY_TYPE const& handle, SPECIFIC_TYPE* obj)
186{
187 return Trinity::Insert(_elements, handle, obj);
188}
189
190template <class OBJECT_TYPES, class KEY_TYPE>
191template <class SPECIFIC_TYPE>
193{
194 return Trinity::Remove(_elements, handle, (SPECIFIC_TYPE*)nullptr);
195}
196
197template <class OBJECT_TYPES, class KEY_TYPE>
198template <class SPECIFIC_TYPE>
200{
201 return Trinity::Find(_elements, handle, (SPECIFIC_TYPE*)nullptr);
202}
203
204template <class OBJECT_TYPES, class KEY_TYPE>
205template <class SPECIFIC_TYPE>
207{
208 std::size_t size = 0;
209 Trinity::Size(_elements, &size, (SPECIFIC_TYPE*)nullptr);
210 return size;
211}
212
213template <class OBJECT_TYPES, class KEY_TYPE>
215{
216 return _elements;
217}
218
219template <class OBJECT_TYPES, class KEY_TYPE>
221{
222 return _elements;
223}
224
225#endif
bool insert(SPECIFIC_TYPE *obj)
inserts a specific object into the container
TypeMapContainer(TypeMapContainer const &)=default
ContainerMapList< OBJECT_TYPES > & GetElements(void)
Removes the object from the container, and returns the removed object.
size_t Count() const
ContainerMapList< OBJECT_TYPES > i_elements
TypeMapContainer(TypeMapContainer &&) noexcept=default
SPECIFIC_TYPE * Find(KEY_TYPE const &handle)
std::size_t Size() const
TypeUnorderedMapContainer(TypeUnorderedMapContainer &&) noexcept=default
bool Remove(KEY_TYPE const &handle)
ContainerUnorderedMap< OBJECT_TYPES, KEY_TYPE > & GetElements()
TypeUnorderedMapContainer(TypeUnorderedMapContainer const &)=default
void Insert(Player *p)
Player * Find(std::string_view name)
bool Insert(ContainerUnorderedMap< TypeList< H, T >, KEY_TYPE > &elements, KEY_TYPE const &handle, SPECIFIC_TYPE *obj)
size_t Count(ContainerMapList< TypeList< H, T > > const &elements, SPECIFIC_TYPE *fake)
bool Remove(ContainerUnorderedMap< TypeList< H, T >, KEY_TYPE > &elements, KEY_TYPE const &handle, SPECIFIC_TYPE *obj)
bool Size(ContainerUnorderedMap< TypeList< H, T >, KEY_TYPE > const &elements, std::size_t *size, SPECIFIC_TYPE *obj)
SPECIFIC_TYPE * Find(ContainerUnorderedMap< TypeList< H, T >, KEY_TYPE > const &elements, KEY_TYPE const &handle, SPECIFIC_TYPE *obj)
constexpr std::size_t size()
Definition: UpdateField.h:796
void Remove(VignetteData &vignette, WorldObject const *owner)
Definition: Vignette.cpp:100
STL namespace.
ContainerMapList< T > _TailElements
Definition: TypeContainer.h:51
GridRefManager< OBJECT > _element
Definition: TypeContainer.h:39
ContainerUnorderedMap< T, KEY_TYPE > _TailElements
Definition: TypeContainer.h:69
ContainerUnorderedMap< H, KEY_TYPE > _elements
Definition: TypeContainer.h:68
std::unordered_map< KEY_TYPE, OBJECT * > _element
Definition: TypeContainer.h:57