Drive Engine  1.0.0
Plateforme de reconstruction 3D
DictionnaryImplementationUtilities.h
1 // Copyright (c) 2015. All rights reserved to DriveEngine Team.
2 
3 #pragma once
4 
5 #include <vector>
6 #include "CollectionsImplementationUtilities.h"
7 
8 
12 #define _GENERATE_DICTIONNARY_ENUMERATOR_H() \
13  _GENERATE_COLLECTION_ENUMERATOR_H(Dictionnary)
14 
15 
19 #define _GENERATE_DICTIONNARY_CONST_ENUMERATOR_H() \
20  _GENERATE_COLLECTION_CONST_ENUMERATOR_H(Dictionnary)
21 
22 
26 #define _GENERATE_DICTIONNARY_ENUMERATOR(dictionnaryClassName, pairClassName)\
27  _GENERATE_COLLECTION_ENUMERATOR(dictionnaryClassName, Dictionnary, result = pairClassName::Create(*iterator->first, *iterator->second);)
28 
29 
33 #define _GENERATE_DICTIONNARY_CONST_ENUMERATOR(dictionnaryClassName, pairClassName)\
34  _GENERATE_COLLECTION_CONST_ENUMERATOR(dictionnaryClassName, Dictionnary, result = pairClassName::Create(*iterator->first, *iterator->second);)
35 
36 
40 #define _GENERATE_DICTIONNARY_H(keyInterfaceName, elementInterfaceName, pairInterfaceName) \
41 private:\
42  typedef std::map<const keyInterfaceName*, elementInterfaceName*> Dictionnary;\
43  typedef std::pair<const keyInterfaceName*, elementInterfaceName*> DictionnaryPair;\
44  Dictionnary dictionnary;\
45 \
46 public:\
47 \
48  /* ICollection */\
49 \
50  virtual Size GetSize() const;\
51  virtual bool IsEmpty() const;\
52  virtual IEnumerator* GetEnumerator() const;\
53  virtual IConstEnumerator* GetConstEnumerator() const;\
54  virtual void Clear();\
55 \
56 \
57  /* IDictionnary */\
58 \
59  virtual IExportable* GetElement(const IExportable &key) const;\
60  virtual const IExportable* GetConstElement(const IExportable &key) const;\
61  virtual void Add(const IExportable &key, IExportable &element);\
62  virtual void Remove(const IExportable &key);\
63  virtual void AddDictionnary(const IDictionnary &dictionnary);\
64  virtual void RemoveDictionnary(const IDictionnary &dictionnary);\
65 \
66  /* elementInterfaceName */\
67  elementInterfaceName* Get##elementInterfaceName##(const keyInterfaceName &key) const;\
68  virtual void Add(const keyInterfaceName &key, elementInterfaceName &element);\
69  virtual void Remove(const keyInterfaceName &key);\
70 \
71 \
72 private:\
73  elementInterfaceName* ConvertElement(IExportable& element) const;\
74  const keyInterfaceName* ConvertKey(const IExportable& key) const;\
75  pairInterfaceName* ConvertPair(IExportable& pair) const;\
76  const pairInterfaceName* ConvertPair(const IExportable& pair) const;\
77 \
78 _GENERATE_DICTIONNARY_ENUMERATOR_H()\
79 _GENERATE_DICTIONNARY_CONST_ENUMERATOR_H()
80 
81 
85 #define GENERATE_DICTIONNARY_H(keyInterfaceName, elementInterfaceName, pairInterfaceName) \
86  _GENERATE_DICTIONNARY_H(keyInterfaceName, elementInterfaceName, pairInterfaceName)
87 
88 
92 #define _GENERATE_DICTIONNARY(dictionnaryClassName, keyInterfaceName, elementInterfaceName, pairInterfaceName, pairClassName) \
93 \
94 /* ICollection */\
95 \
96 _GENERATE_COLLECTION_COMMON(dictionnaryClassName, dictionnary)\
97 \
98 void dictionnaryClassName::Clear()\
99 {\
100  Dictionnary::const_iterator iterator = dictionnary.cbegin();\
101  while (iterator != dictionnary.cend())\
102  {\
103  ::DeleteReference(iterator->first); \
104  ::DeleteReference(iterator->second); \
105  dictionnary.erase(iterator);\
106  iterator = dictionnary.cbegin();\
107  }\
108 }\
109 \
110 \
111 /* IDictionnary */\
112 \
113 IExportable* dictionnaryClassName::GetElement(const IExportable &key) const\
114 {\
115  const keyInterfaceName* convertedKey = ConvertKey(key);\
116  if (convertedKey == nullptr){ /* An error has been thrown */ return nullptr; }\
117 \
118  return Get##elementInterfaceName##(*convertedKey);\
119 }\
120 \
121 const IExportable* dictionnaryClassName::GetConstElement(const IExportable &key) const\
122 {\
123  return GetElement(key);\
124 }\
125 \
126 void dictionnaryClassName::Add(const IExportable &key, IExportable &element)\
127 {\
128  elementInterfaceName* convertedElement = ConvertElement(element);\
129  if (convertedElement == nullptr){ /* An error has been thrown */ return; }\
130 \
131  const keyInterfaceName* convertedKey = ConvertKey(key);\
132  if (convertedKey == nullptr){ /* An error has been thrown */ return; }\
133 \
134  Add(*convertedKey, *convertedElement);\
135 }\
136 \
137 void dictionnaryClassName::Remove(const IExportable &key)\
138 {\
139  const keyInterfaceName* convertedKey = ConvertKey(key);\
140  if (convertedKey == nullptr){ /* An error has been thrown */ return; }\
141 \
142  Remove(*convertedKey);\
143 }\
144 \
145 void dictionnaryClassName::AddDictionnary(const IDictionnary &dictionnary)\
146 {\
147  if (Precondition::IsNotNull(dictionnary, "The dictionnary must not be null."))\
148  {\
149  IEnumerator* enumerator = dictionnary.GetEnumerator();\
150  while (enumerator->MoveNext())\
151  {\
152  IExportable* pair = enumerator->GetCurrent();\
153  pairInterfaceName* convertedPair = ConvertPair(*pair);\
154  if (convertedPair == nullptr){ /* An error has been thrown */ return; }\
155 \
156  Add(*convertedPair->GetKey(), *convertedPair->GetValue());\
157  convertedPair->Delete();\
158  }\
159  }\
160 }\
161 \
162 void dictionnaryClassName::RemoveDictionnary(const IDictionnary &dictionnary)\
163 {\
164  if (Precondition::IsNotNull(dictionnary, "The dictionnary must not be null."))\
165  {\
166  IEnumerator* enumerator = dictionnary.GetEnumerator();\
167  while (enumerator->MoveNext())\
168  {\
169  IExportable* pair = enumerator->GetCurrent();\
170  pairInterfaceName* convertedPair = ConvertPair(*pair);\
171  if (convertedPair == nullptr){ /* An error has been thrown */ return; }\
172 \
173  Remove(*convertedPair->GetKey());\
174  convertedPair->Delete();\
175  }\
176  }\
177 }\
178 \
179 \
180 /* dictionnaryClassName */\
181 \
182 elementInterfaceName* dictionnaryClassName::Get##elementInterfaceName##(const keyInterfaceName &key) const\
183 {\
184  if (Precondition::IsNotNull(key, "The key of the element to remove must not be null."))\
185  {\
186  Dictionnary::const_iterator iterator = dictionnary.find(&key);\
187  if (iterator != dictionnary.end())\
188  {\
189  return iterator->second;\
190  }\
191  }\
192  return nullptr;\
193 }\
194 \
195 void dictionnaryClassName::Add(const keyInterfaceName &key, elementInterfaceName &element)\
196 {\
197  if (Precondition::IsNotNull(key, "The image to add must not be null.") &&\
198  Precondition::IsNotNull(element, "The position to add must not be null."))\
199  {\
200  Dictionnary::iterator iterator = dictionnary.find(&key);\
201  if (iterator == dictionnary.end())\
202  {\
203  ::AddReference(&key);\
204  ::AddReference(&element);\
205  dictionnary.insert(DictionnaryPair(&key, &element));\
206  }\
207  else\
208  {\
209  ::AddReference(&element);\
210  ::DeleteReference(iterator->second);\
211  iterator->second = &element;\
212  }\
213  }\
214 }\
215 \
216 void dictionnaryClassName::Remove(const keyInterfaceName &key)\
217 {\
218  if (Precondition::IsNotNull(key, "The key of the element to remove must not be null."))\
219  {\
220  Dictionnary::iterator iterator = dictionnary.find(&key);\
221  if (iterator == dictionnary.end()){ return; }\
222 \
223  ::DeleteReference(iterator->first);\
224  ::DeleteReference(iterator->second);\
225  dictionnary.erase(iterator);\
226  }\
227 }\
228 \
229 elementInterfaceName* dictionnaryClassName::ConvertElement(IExportable& element) const\
230 {\
231  if (Precondition::IsNotNull(element, "The element must not be null."))\
232  {\
233  elementInterfaceName* convertedElement = dynamic_cast<elementInterfaceName*>(&element);\
234  if (Precondition::IsNotNull(convertedElement, "The element cannot be converted."))\
235  {\
236  return convertedElement;\
237  }\
238  }\
239  return nullptr;\
240 }\
241 \
242 const keyInterfaceName* dictionnaryClassName::ConvertKey(const IExportable& key) const\
243 {\
244  if (Precondition::IsNotNull(key, "The key must not be null."))\
245  {\
246  const keyInterfaceName* convertedKey = dynamic_cast<const keyInterfaceName*>(&key);\
247  if (Precondition::IsNotNull(convertedKey, "The key cannot be converted."))\
248  {\
249  return convertedKey;\
250  }\
251  }\
252  return nullptr;\
253 }\
254 \
255 pairInterfaceName* dictionnaryClassName::ConvertPair(IExportable& pair) const\
256 {\
257  if (Precondition::IsNotNull(pair, "The pair must not be null."))\
258  {\
259  pairInterfaceName* convertedPair = dynamic_cast<pairInterfaceName*>(&pair);\
260  if (Precondition::IsNotNull(convertedPair, "The key cannot be converted."))\
261  {\
262  return convertedPair;\
263  }\
264  }\
265  return nullptr;\
266 }\
267 \
268 const pairInterfaceName* dictionnaryClassName::ConvertPair(const IExportable& pair) const\
269 {\
270  if (Precondition::IsNotNull(pair, "The pair must not be null."))\
271  {\
272  const pairInterfaceName* convertedPair = dynamic_cast<const pairInterfaceName*>(&pair);\
273  if (Precondition::IsNotNull(convertedPair, "The key cannot be converted."))\
274  {\
275  return convertedPair;\
276  }\
277  }\
278  return nullptr;\
279 }\
280 _GENERATE_DICTIONNARY_ENUMERATOR(dictionnaryClassName, pairClassName)\
281 _GENERATE_DICTIONNARY_CONST_ENUMERATOR(dictionnaryClassName, pairClassName)
282 
283 
287 #define GENERATE_DICTIONNARY(dictionnaryClassName, keyInterfaceName, elementInterfaceName, pairInterfaceName, pairClassName) \
288  _GENERATE_DICTIONNARY(dictionnaryClassName, keyInterfaceName, elementInterfaceName, pairInterfaceName, pairClassName)