TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
TypeContainerFunctions.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef TYPECONTAINER_FUNCTIONS_H
20 #define TYPECONTAINER_FUNCTIONS_H
21 
22 /*
23  * Here you'll find a list of helper functions to make
24  * the TypeContainer usefull. Without it, its hard
25  * to access or mutate the container.
26  */
27 
28 #include "Define.h"
29 #include "Dynamic/TypeList.h"
30 #include <map>
31 #include <unordered_map>
32 
33 namespace Trinity
34 {
35  // Helpers
36  // Insert helpers
37  template<class SPECIFIC_TYPE, class KEY_TYPE>
38  bool Insert(ContainerUnorderedMap<SPECIFIC_TYPE, KEY_TYPE>& elements, KEY_TYPE const& handle, SPECIFIC_TYPE* obj)
39  {
40  auto i = elements._element.find(handle);
41  if (i == elements._element.end())
42  {
43  elements._element[handle] = obj;
44  return true;
45  }
46  else
47  {
48  ASSERT(i->second == obj, "Object with certain key already in but objects are different!");
49  return false;
50  }
51  }
52 
53  template<class SPECIFIC_TYPE, class KEY_TYPE>
54  bool Insert(ContainerUnorderedMap<TypeNull, KEY_TYPE>& /*elements*/, KEY_TYPE const& /*handle*/, SPECIFIC_TYPE* /*obj*/)
55  {
56  return false;
57  }
58 
59  template<class SPECIFIC_TYPE, class KEY_TYPE, class T>
60  bool Insert(ContainerUnorderedMap<T, KEY_TYPE>& /*elements*/, KEY_TYPE const& /*handle*/, SPECIFIC_TYPE* /*obj*/)
61  {
62  return false;
63  }
64 
65  template<class SPECIFIC_TYPE, class KEY_TYPE, class H, class T>
66  bool Insert(ContainerUnorderedMap<TypeList<H, T>, KEY_TYPE>& elements, KEY_TYPE const& handle, SPECIFIC_TYPE* obj)
67  {
68  bool ret = Insert(elements._elements, handle, obj);
69  return ret ? ret : Insert(elements._TailElements, handle, obj);
70  }
71 
72  // Find helpers
73  template<class SPECIFIC_TYPE, class KEY_TYPE>
74  SPECIFIC_TYPE* Find(ContainerUnorderedMap<SPECIFIC_TYPE, KEY_TYPE> const& elements, KEY_TYPE const& handle, SPECIFIC_TYPE* /*obj*/)
75  {
76  auto i = elements._element.find(handle);
77  if (i == elements._element.end())
78  return nullptr;
79  else
80  return i->second;
81  }
82 
83  template<class SPECIFIC_TYPE, class KEY_TYPE>
84  SPECIFIC_TYPE* Find(ContainerUnorderedMap<TypeNull, KEY_TYPE> const& /*elements*/, KEY_TYPE const& /*handle*/, SPECIFIC_TYPE* /*obj*/)
85  {
86  return nullptr;
87  }
88 
89  template<class SPECIFIC_TYPE, class KEY_TYPE, class T>
90  SPECIFIC_TYPE* Find(ContainerUnorderedMap<T, KEY_TYPE> const& /*elements*/, KEY_TYPE const& /*handle*/, SPECIFIC_TYPE* /*obj*/)
91  {
92  return nullptr;
93  }
94 
95  template<class SPECIFIC_TYPE, class KEY_TYPE, class H, class T>
96  SPECIFIC_TYPE* Find(ContainerUnorderedMap<TypeList<H, T>, KEY_TYPE> const& elements, KEY_TYPE const& handle, SPECIFIC_TYPE* /*obj*/)
97  {
98  SPECIFIC_TYPE* ret = Find(elements._elements, handle, (SPECIFIC_TYPE*)nullptr);
99  return ret ? ret : Find(elements._TailElements, handle, (SPECIFIC_TYPE*)nullptr);
100  }
101 
102  // Erase helpers
103  template<class SPECIFIC_TYPE, class KEY_TYPE>
104  bool Remove(ContainerUnorderedMap<SPECIFIC_TYPE, KEY_TYPE>& elements, KEY_TYPE const& handle, SPECIFIC_TYPE* /*obj*/)
105  {
106  elements._element.erase(handle);
107  return true;
108  }
109 
110  template<class SPECIFIC_TYPE, class KEY_TYPE>
111  bool Remove(ContainerUnorderedMap<TypeNull, KEY_TYPE>& /*elements*/, KEY_TYPE const& /*handle*/, SPECIFIC_TYPE* /*obj*/)
112  {
113  return false;
114  }
115 
116  template<class SPECIFIC_TYPE, class KEY_TYPE, class T>
117  bool Remove(ContainerUnorderedMap<T, KEY_TYPE>& /*elements*/, KEY_TYPE const& /*handle*/, SPECIFIC_TYPE* /*obj*/)
118  {
119  return false;
120  }
121 
122  template<class SPECIFIC_TYPE, class KEY_TYPE, class H, class T>
123  bool Remove(ContainerUnorderedMap<TypeList<H, T>, KEY_TYPE>& elements, KEY_TYPE const& handle, SPECIFIC_TYPE* /*obj*/)
124  {
125  bool ret = Remove(elements._elements, handle, (SPECIFIC_TYPE*)nullptr);
126  return ret ? ret : Remove(elements._TailElements, handle, (SPECIFIC_TYPE*)nullptr);
127  }
128 
129  /* ContainerMapList Helpers */
130  // count functions
131  template<class SPECIFIC_TYPE>
132  size_t Count(ContainerMapList<SPECIFIC_TYPE> const& elements, SPECIFIC_TYPE* /*fake*/)
133  {
134  return elements._element.getSize();
135  }
136 
137  template<class SPECIFIC_TYPE>
138  size_t Count(ContainerMapList<TypeNull> const& /*elements*/, SPECIFIC_TYPE* /*fake*/)
139  {
140  return 0;
141  }
142 
143  template<class SPECIFIC_TYPE, class T>
144  size_t Count(ContainerMapList<T> const& /*elements*/, SPECIFIC_TYPE* /*fake*/)
145  {
146  return 0;
147  }
148 
149  template<class SPECIFIC_TYPE, class T>
150  size_t Count(ContainerMapList<TypeList<SPECIFIC_TYPE, T>> const& elements, SPECIFIC_TYPE* fake)
151  {
152  return Count(elements._elements, fake);
153  }
154 
155  template<class SPECIFIC_TYPE, class H, class T>
156  size_t Count(ContainerMapList<TypeList<H, T>> const& elements, SPECIFIC_TYPE* fake)
157  {
158  return Count(elements._TailElements, fake);
159  }
160 
161  // non-const insert functions
162  template<class SPECIFIC_TYPE>
163  SPECIFIC_TYPE* Insert(ContainerMapList<SPECIFIC_TYPE>& elements, SPECIFIC_TYPE* obj)
164  {
165  //elements._element[hdl] = obj;
166  obj->AddToGrid(elements._element);
167  return obj;
168  }
169 
170  template<class SPECIFIC_TYPE>
171  SPECIFIC_TYPE* Insert(ContainerMapList<TypeNull>& /*elements*/, SPECIFIC_TYPE* /*obj*/)
172  {
173  return nullptr;
174  }
175 
176  // this is a missed
177  template<class SPECIFIC_TYPE, class T>
178  SPECIFIC_TYPE* Insert(ContainerMapList<T>& /*elements*/, SPECIFIC_TYPE* /*obj*/)
179  {
180  return nullptr; // a missed
181  }
182 
183  // Recursion
184  template<class SPECIFIC_TYPE, class H, class T>
185  SPECIFIC_TYPE* Insert(ContainerMapList<TypeList<H, T>>& elements, SPECIFIC_TYPE* obj)
186  {
187  SPECIFIC_TYPE* t = Insert(elements._elements, obj);
188  return (t != nullptr ? t : Insert(elements._TailElements, obj));
189  }
190 
192  //template<class SPECIFIC_TYPE> SPECIFIC_TYPE* Remove(ContainerMapList<SPECIFIC_TYPE> & /*elements*/, SPECIFIC_TYPE *obj)
193  //{
194  // obj->GetGridRef().unlink();
195  // return obj;
196  //}
197 
198  //template<class SPECIFIC_TYPE> SPECIFIC_TYPE* Remove(ContainerMapList<TypeNull> &/*elements*/, SPECIFIC_TYPE * /*obj*/)
199  //{
200  // return nullptr;
201  //}
202 
204  //template<class SPECIFIC_TYPE, class T> SPECIFIC_TYPE* Remove(ContainerMapList<T> &/*elements*/, SPECIFIC_TYPE * /*obj*/)
205  //{
206  // return nullptr; // a missed
207  //}
208 
209  //template<class SPECIFIC_TYPE, class T, class H> SPECIFIC_TYPE* Remove(ContainerMapList<TypeList<H, T> > &elements, SPECIFIC_TYPE *obj)
210  //{
211  // // The head element is bad
212  // SPECIFIC_TYPE* t = Remove(elements._elements, obj);
213  // return (t != nullptr ? t : Remove(elements._TailElements, obj));
214  //}
215 }
216 #endif
217 
bool Remove(ContainerUnorderedMap< SPECIFIC_TYPE, KEY_TYPE > &elements, KEY_TYPE const &handle, SPECIFIC_TYPE *)
Definition: TypeContainerFunctions.h:104
Definition: TypeList.h:31
Definition: TypeContainer.h:47
Definition: TypeContainer.h:65
size_t Count(ContainerMapList< SPECIFIC_TYPE > const &elements, SPECIFIC_TYPE *)
Definition: TypeContainerFunctions.h:132
GridRefManager< OBJECT > _element
Definition: TypeContainer.h:43
bool Insert(ContainerUnorderedMap< SPECIFIC_TYPE, KEY_TYPE > &elements, KEY_TYPE const &handle, SPECIFIC_TYPE *obj)
Definition: TypeContainerFunctions.h:38
SPECIFIC_TYPE * Find(ContainerUnorderedMap< SPECIFIC_TYPE, KEY_TYPE > const &elements, KEY_TYPE const &handle, SPECIFIC_TYPE *)
Definition: TypeContainerFunctions.h:74
Definition: TypeContainer.h:59
#define ASSERT
Definition: Errors.h:55
std::unordered_map< KEY_TYPE, OBJECT * > _element
Definition: TypeContainer.h:61
Definition: Common.h:172
Definition: TypeContainer.h:40