TrinityCore
Loading...
Searching...
No Matches
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 <map>
27#include <unordered_map>
28#include <vector>
29#include "Define.h"
30#include "Dynamic/TypeList.h"
31#include "GridRefManager.h"
32
33/*
34 * @class ContainerMapList is a mulit-type container for map elements
35 * By itself its meaningless but collaborate along with TypeContainers,
36 * it become the most powerfully container in the whole system.
37 */
38template<class OBJECT>
43
44template<>
45struct ContainerMapList<TypeNull> /* nothing is in type null */
46{
47};
48
49template<class H, class T>
55
56template<class OBJECT, class KEY_TYPE>
58{
59 std::unordered_map<KEY_TYPE, OBJECT*> _element;
60};
61
62template<class KEY_TYPE>
63struct ContainerUnorderedMap<TypeNull, KEY_TYPE>
64{
65};
66
67template<class H, class T, class KEY_TYPE>
73
75
76/*
77 * @class TypeMapContainer contains a fixed number of types and is
78 * determined at compile time. This is probably the most complicated
79 * class and do its simplest thing, that is, holds objects
80 * of different types.
81 */
82
83template<class OBJECT_TYPES>
85{
86public:
89 TypeMapContainer(TypeMapContainer&&) noexcept = default;
90 TypeMapContainer& operator=(TypeMapContainer const&) = default;
91 TypeMapContainer& operator=(TypeMapContainer&&) noexcept = default;
93
94 template<class SPECIFIC_TYPE>
95 size_t Count() const;
96
98 template<class SPECIFIC_TYPE>
99 bool insert(SPECIFIC_TYPE *obj);
100
102 //template<class SPECIFIC_TYPE>
103 //bool remove(SPECIFIC_TYPE* obj)
104 //{
105 // SPECIFIC_TYPE* t = Trinity::Remove(i_elements, obj);
106 // return (t != nullptr);
107 //}
108
109 ContainerMapList<OBJECT_TYPES>& GetElements(void);
110 const ContainerMapList<OBJECT_TYPES>& GetElements(void) const;
111
112private:
114};
115
116template <class OBJECT_TYPES>
117TypeMapContainer<OBJECT_TYPES>::TypeMapContainer() = default;
118
119template <class OBJECT_TYPES>
120TypeMapContainer<OBJECT_TYPES>::~TypeMapContainer() = default;
121
122template <class OBJECT_TYPES>
123template <class SPECIFIC_TYPE>
124size_t TypeMapContainer<OBJECT_TYPES>::Count() const
125{
126 return Trinity::Count(i_elements, (SPECIFIC_TYPE*)nullptr);
127}
128
129template <class OBJECT_TYPES>
130template <class SPECIFIC_TYPE>
132{
133 SPECIFIC_TYPE* t = Trinity::Insert(i_elements, obj);
134 return (t != nullptr);
135}
136
137template <class OBJECT_TYPES>
142
143template <class OBJECT_TYPES>
145{
146 return i_elements;
147}
148
149template<class OBJECT_TYPES, class KEY_TYPE>
151{
152public:
157 TypeUnorderedMapContainer& operator=(TypeUnorderedMapContainer&&) noexcept = default;
159
160 template<class SPECIFIC_TYPE>
161 bool Insert(KEY_TYPE const& handle, SPECIFIC_TYPE* obj);
162
163 template<class SPECIFIC_TYPE>
164 bool Remove(KEY_TYPE const& handle);
165
166 template<class SPECIFIC_TYPE>
167 SPECIFIC_TYPE* Find(KEY_TYPE const& handle);
168
169 template<class SPECIFIC_TYPE>
170 std::size_t Size() const;
171
172 ContainerUnorderedMap<OBJECT_TYPES, KEY_TYPE>& GetElements();
173 ContainerUnorderedMap<OBJECT_TYPES, KEY_TYPE> const& GetElements() const;
174
175private:
176 ContainerUnorderedMap<OBJECT_TYPES, KEY_TYPE> _elements;
177};
178
179template <class OBJECT_TYPES, class KEY_TYPE>
180TypeUnorderedMapContainer<OBJECT_TYPES, KEY_TYPE>::TypeUnorderedMapContainer() = default;
181
182template <class OBJECT_TYPES, class KEY_TYPE>
183TypeUnorderedMapContainer<OBJECT_TYPES, KEY_TYPE>::~TypeUnorderedMapContainer() = default;
184
185template <class OBJECT_TYPES, class KEY_TYPE>
186template <class SPECIFIC_TYPE>
187bool TypeUnorderedMapContainer<OBJECT_TYPES, KEY_TYPE>::Insert(KEY_TYPE const& handle, SPECIFIC_TYPE* obj)
188{
189 return Trinity::Insert(_elements, handle, obj);
190}
191
192template <class OBJECT_TYPES, class KEY_TYPE>
193template <class SPECIFIC_TYPE>
195{
196 return Trinity::Remove(_elements, handle, (SPECIFIC_TYPE*)nullptr);
197}
198
199template <class OBJECT_TYPES, class KEY_TYPE>
200template <class SPECIFIC_TYPE>
202{
203 return Trinity::Find(_elements, handle, (SPECIFIC_TYPE*)nullptr);
204}
205
206template <class OBJECT_TYPES, class KEY_TYPE>
207template <class SPECIFIC_TYPE>
209{
210 std::size_t size = 0;
211 Trinity::Size(_elements, &size, (SPECIFIC_TYPE*)nullptr);
212 return size;
213}
214
215template <class OBJECT_TYPES, class KEY_TYPE>
220
221template <class OBJECT_TYPES, class KEY_TYPE>
226
227#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
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)
STL namespace.
ContainerMapList< T > _TailElements
GridRefManager< OBJECT > _element
ContainerUnorderedMap< T, KEY_TYPE > _TailElements
ContainerUnorderedMap< H, KEY_TYPE > _elements
std::unordered_map< KEY_TYPE, OBJECT * > _element