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