TrinityCore
Loading...
Searching...
No Matches
TypeContainerFunctions.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 TYPECONTAINER_FUNCTIONS_H
19#define TYPECONTAINER_FUNCTIONS_H
20
21/*
22 * Here you'll find a list of helper functions to make
23 * the TypeContainer usefull. Without it, its hard
24 * to access or mutate the container.
25 */
26
27#include "Define.h"
28#include "Dynamic/TypeList.h"
29#include <map>
30#include <unordered_map>
31
32namespace Trinity
33{
34 // Helpers
35 // Insert helpers
36 template<class SPECIFIC_TYPE, class KEY_TYPE, class H, class T>
37 inline bool Insert(ContainerUnorderedMap<TypeList<H, T>, KEY_TYPE>& elements, KEY_TYPE const& handle, SPECIFIC_TYPE* obj)
38 {
39 if constexpr (std::is_same_v<H, SPECIFIC_TYPE>)
40 {
41 auto i = elements._elements._element.find(handle);
42 if (i == elements._elements._element.end())
43 {
44 elements._elements._element[handle] = obj;
45 return true;
46 }
47 else
48 {
49 ASSERT(i->second == obj, "Object with certain key already in but objects are different!");
50 return false;
51 }
52 }
53
54 if constexpr (std::is_same_v<T, TypeNull>)
55 return false;
56 else
57 return Insert(elements._TailElements, handle, obj);
58 }
59
60 // Find helpers
61 template<class SPECIFIC_TYPE, class KEY_TYPE, class H, class T>
62 inline SPECIFIC_TYPE* Find(ContainerUnorderedMap<TypeList<H, T>, KEY_TYPE> const& elements, KEY_TYPE const& handle, SPECIFIC_TYPE* obj)
63 {
64 if constexpr (std::is_same_v<H, SPECIFIC_TYPE>)
65 {
66 auto i = elements._elements._element.find(handle);
67 if (i == elements._elements._element.end())
68 return nullptr;
69 else
70 return i->second;
71 }
72
73 if constexpr (std::is_same_v<T, TypeNull>)
74 return nullptr;
75 else
76 return Find(elements._TailElements, handle, obj);
77 }
78
79 // Erase helpers
80 template<class SPECIFIC_TYPE, class KEY_TYPE, class H, class T>
81 inline bool Remove(ContainerUnorderedMap<TypeList<H, T>, KEY_TYPE>& elements, KEY_TYPE const& handle, SPECIFIC_TYPE* obj)
82 {
83 if constexpr (std::is_same_v<H, SPECIFIC_TYPE>)
84 {
85 elements._elements._element.erase(handle);
86 return true;
87 }
88
89 if constexpr (std::is_same_v<T, TypeNull>)
90 return false;
91 else
92 return Remove(elements._TailElements, handle, obj);
93 }
94
95 // Count helpers
96 template<class SPECIFIC_TYPE, class KEY_TYPE, class H, class T>
97 inline bool Size(ContainerUnorderedMap<TypeList<H, T>, KEY_TYPE> const& elements, std::size_t* size, SPECIFIC_TYPE* obj)
98 {
99 if constexpr (std::is_same_v<H, SPECIFIC_TYPE>)
100 {
101 *size = elements._elements._element.size();
102 return true;
103 }
104
105 if constexpr (std::is_same_v<T, TypeNull>)
106 return false;
107 else
108 return Size(elements._TailElements, size, obj);
109 }
110
111 /* ContainerMapList Helpers */
112 // count functions
113 template<class SPECIFIC_TYPE, class H, class T>
114 inline size_t Count(ContainerMapList<TypeList<H, T>> const& elements, SPECIFIC_TYPE* fake)
115 {
116 if constexpr (std::is_same_v<H, SPECIFIC_TYPE>)
117 {
118 return elements._elements._element.getSize();
119 }
120
121 if constexpr (std::is_same_v<T, TypeNull>)
122 return 0;
123 else
124 return Count(elements._TailElements, fake);
125 }
126
127 // non-const insert functions
128 template<class SPECIFIC_TYPE, class H, class T>
129 inline SPECIFIC_TYPE* Insert(ContainerMapList<TypeList<H, T>>& elements, SPECIFIC_TYPE* obj)
130 {
131 if constexpr (std::is_same_v<H, SPECIFIC_TYPE>)
132 {
133 obj->AddToGrid(elements._elements._element);
134 return obj;
135 }
136
137 if constexpr (std::is_same_v<T, TypeNull>)
138 return nullptr;
139 else
140 return Insert(elements._TailElements, obj);
141 }
142
144 //template<class SPECIFIC_TYPE, class H, class T>
145 //SPECIFIC_TYPE* Remove(ContainerMapList<TypeList<H, T>>& elements, SPECIFIC_TYPE* obj)
146 //{
147 // if constexpr (std::is_same_v<H, SPECIFIC_TYPE>)
148 // {
149 // obj->GetGridRef().unlink();
150 // return obj;
151 // }
152
153 // if constexpr (std::is_same_v<T, TypeNull>)
154 // return nullptr;
155 // else
156 // return Remove(elements._TailElements, obj);
157 //}
158}
159#endif
#define ASSERT
Definition Errors.h:68
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)