CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
base_dictionary.h
1
2#pragma once
3
4#include <system/collections/idictionary.h>
5#include <system/cycles_detection.h>
6#include <system/collections/base_enumerator.h>
7#include <system/details/hash_quality_checker.h>
8#include <system/collections/virtualized_iterator.h>
9
10#include <algorithm>
11#include <type_traits>
12
13namespace System { namespace Collections { namespace Generic {
14
17template <typename Dict>
19 : public System::Details::NativeIteratorWrapperBase<typename Dict::KeyValuePairType, typename Dict::map_t::const_iterator>
20 , private System::Details::IteratorPointerUpdater<typename Dict::KeyValuePairType, false>
21{
22 using System::Details::NativeIteratorWrapperBase<typename Dict::KeyValuePairType, typename Dict::map_t::const_iterator>::m_iterator;
23 using System::Details::NativeIteratorWrapperBase<typename Dict::KeyValuePairType, typename Dict::map_t::const_iterator>::m_end;
24 using System::Details::VirtualizedIteratorBase<typename Dict::KeyValuePairType>::m_is_end;
25 using System::Details::VirtualizedIteratorBase<typename Dict::KeyValuePairType>::m_pointer;
26 using System::Details::IteratorPointerUpdater<typename Dict::KeyValuePairType, false>::InitializeIteratorPointer;
27 using System::Details::IteratorPointerUpdater<typename Dict::KeyValuePairType, false>::UpdateIteratorPointer;
28
29public:
33 DictionaryIterator(typename Dict::map_t::const_iterator &&iterator, typename Dict::map_t::const_iterator &&end) noexcept
34 : System::Details::NativeIteratorWrapperBase<typename Dict::KeyValuePairType, typename Dict::map_t::const_iterator>(
35 std::forward<typename Dict::map_t::const_iterator>(iterator),
36 std::forward<typename Dict::map_t::const_iterator>(end)
37 )
38 {
39 InitializeIteratorPointer(m_pointer);
40 if (!m_is_end)
41 UpdateIteratorPointer(m_pointer, typename Dict::KeyValuePairType(m_iterator->first, m_iterator->second));
42 }
46 DictionaryIterator(const typename Dict::map_t::const_iterator &iterator, const typename Dict::map_t::const_iterator &end)
47 : System::Details::NativeIteratorWrapperBase<typename Dict::KeyValuePairType, typename Dict::map_t::const_iterator>(iterator, end)
48 {
49 InitializeIteratorPointer(m_pointer);
50 if (!m_is_end)
51 UpdateIteratorPointer(m_pointer, typename Dict::KeyValuePairType(m_iterator->first, m_iterator->second));
52 }
56 : System::Details::NativeIteratorWrapperBase<typename Dict::KeyValuePairType, typename Dict::map_t::const_iterator>(std::forward(other.m_iterator))
57 {
58 if (!m_is_end)
59 UpdateIteratorPointer(m_pointer, typename Dict::KeyValuePairType(m_iterator->first, m_iterator->second));
60 }
62 virtual ~DictionaryIterator() = default;
63
65 void IncrementIterator() override
66 {
67 if (System::Details::NativeIteratorWrapperBase<typename Dict::KeyValuePairType, typename Dict::map_t::const_iterator>::IteratorIncrementImplementation())
68 UpdateIteratorPointer(m_pointer, typename Dict::KeyValuePairType(m_iterator->first, m_iterator->second));
69 }
71 void DecrementIterator() override
72 {
73 System::Details::IteratorDecrementer<typename Dict::map_t::const_iterator>::decrement(m_iterator);
74 UpdateIteratorPointer(m_pointer, typename Dict::KeyValuePairType(m_iterator->first, m_iterator->second));
75 }
78 void ShiftIteratorBy(std::ptrdiff_t offset) override
79 {
80 if (System::Details::NativeIteratorWrapperBase<typename Dict::KeyValuePairType, typename Dict::map_t::const_iterator>::IteratorShiftImplementation(offset))
81 UpdateIteratorPointer(m_pointer, typename Dict::KeyValuePairType(m_iterator->first, m_iterator->second));
82 }
85 System::Details::VirtualizedIteratorBase<typename Dict::KeyValuePairType>* CloneIterator() const override
86 {
87 return new DictionaryIterator(m_iterator, m_end);
88 }
89};
90
91
98template<typename Map>
99class ASPOSECPP_SHARED_CLASS BaseDictionary : public IDictionary<typename Map::key_type, typename Map::mapped_type>
100{
101 ASPOSE_MAP_POINTER_MODE_CONTROL(Map)
102
103public:
104 void SetTemplateWeakPtr(unsigned int argument) override
105 {
106 static_assert(std::is_same<decltype(m_map.get_allocator()), typename pointer_mode_t::allocator_type>::value, "Container's allocator is not set to typename pointer_mode_t::allocator_type");
107 m_pointer_mode.SetWeak(argument, m_map);
108 }
109
111 typedef Map map_t;
112
113protected:
114 pointer_mode_t m_pointer_mode;
115
116private:
118 typedef typename Map::key_type key_t;
120 typedef typename Map::mapped_type mapped_t;
122 typedef typename Map::iterator mapIt_t;
124 typedef typename Map::const_iterator stl_const_iterator;
125
127 static_assert(std::is_same<allocator_t, typename Map::allocator_type>::value, "Using wrong map type with BaseDictionary");
128
129public:
136
139
144
146 BaseDictionary() : ASPOSE_MAP_INIT_ALLOCATOR() {}
147
151 template<class... Args>
152 BaseDictionary(int, const Args&... args)
153 : m_map(args..., ASPOSE_COLLECTION_ALLOCATOR)
154 {}
155
160 template<class... Args>
161 BaseDictionary(BaseType* src, const Args&... args)
162 : m_map(args..., ASPOSE_COLLECTION_ALLOCATOR)
163 {
164 CopyFrom(src);
165 }
166
169 BaseDictionary(BaseType* src) : ASPOSE_MAP_INIT_ALLOCATOR()
170 {
171 CopyFrom(src);
172 }
173
174 // IEnumerable
178
179 // ICollection
182 int32_t get_Count() const override
183 {
184 return static_cast<int32_t>(m_map.size());
185 }
187 void Clear() override
188 {
189 m_map.clear();
190 }
191
192 // IDictionary interface
196 virtual mapped_t& operator[](const key_t& key)
197 {
198 return m_map[key];
199 }
204 mapped_t idx_get(const key_t& key) const override
205 {
206 CheckIfNull(key);
207 auto it = m_map.find(key);
208 if (it == m_map.end())
209 {
210 throw KeyNotFoundException();
211 }
212
213 return it->second;
214 }
218 void idx_set(const key_t& key, mapped_t value) override
219 {
220 CheckIfNull(key);
221 m_map[key] = value;
222#if defined(ASPOSE_DETECT_BAD_QUALITY_HASH)
223 System::Details::CheckHashFunctionQuality(m_map);
224#endif
225 }
230 void Add(const key_t& key, const mapped_t& value) override
231 {
232 CheckIfNull(key);
233 // insert never replaces values
234 auto op_result = m_map.insert(std::make_pair(key, value));
235 if (! op_result.second)
236 {
237 throw System::ArgumentException(u"key");
238 }
239#if defined(ASPOSE_DETECT_BAD_QUALITY_HASH)
240 System::Details::CheckHashFunctionQuality(m_map);
241#endif
242 }
246 bool ContainsKey(const key_t& key) const override
247 {
248 CheckIfNull(key);
249 return m_map.find(key) != m_map.end();
250 }
254 bool Remove(const key_t& key) override
255 {
256 CheckIfNull(key);
257 auto it = m_map.find(key);
258
259 if (it != m_map.end())
260 {
261 m_map.erase(it);
262 return true;
263 }
264
265 return false;
266 }
271 bool TryGetValue(const key_t& key, mapped_t& value) const override
272 {
273 auto it = m_map.find(key);
274
275 if (it != m_map.end())
276 {
277 value = it->second;
278 return true;
279 }
280
281 // There should be the System::Default<mapped_t>() call, but it has a lot of restrictions,
282 // e.g. System::Default doesn't work with C# structures and delegates
283 value = mapped_t();
284 return false;
285 }
286
287 // BaseDictionary interface
291 bool ContainsValue(const mapped_t& value)
292 {
293 for (const auto& itr : m_map)
294 {
295 if (itr.second == value)
296 return true;
297 }
298 return false;
299 }
300
303 Map& data() { return m_map; }
306 const Map& data() const { return m_map; }
309 void _add_range(std::initializer_list<typename Map::value_type> list)
310 {
311 std::for_each(list.begin(), list.end(), [this](const typename Map::value_type &v) { m_map.insert(v); });
312 }
313
315 mapped_t GetValueOrDefault(const key_t& key) const override
316 {
317 auto it = m_map.find(key);
318
319 if (it != m_map.end())
320 {
321 return it->second;
322 }
323
324 return mapped_t();
325 }
326
328 mapped_t GetValueOrDefault(const key_t& key, const mapped_t& defaultValue) const override
329 {
330 auto it = m_map.find(key);
331
332 if (it != m_map.end())
333 {
334 return it->second;
335 }
336
337 return defaultValue;
338 }
339
341 mapped_t GetValueOrNull(const key_t& key) const override
342 {
343 auto it = m_map.find(key);
344
345 if (it != m_map.end())
346 {
347 return it->second;
348 }
349
350 return mapped_t();
351 }
352
357 const_iterator begin() const noexcept
358 {
359 return m_map.begin();
360 }
361
366 const_iterator end() const noexcept
367 {
368 return m_map.end();
369 }
370
374 stl_const_iterator cbegin() const noexcept
375 {
376 return m_map.cbegin();
377 }
378
382 stl_const_iterator cend() const noexcept
383 {
384 return m_map.cend();
385 }
386
388 System::Details::VirtualizedIteratorBase<KVPair>* virtualizeBeginIterator() override
389 {
390 return new DictionaryIterator<BaseDictionary>(m_map.begin(), m_map.end());
391 }
393 System::Details::VirtualizedIteratorBase<KVPair>* virtualizeEndIterator() override
394 {
395 return new DictionaryIterator<BaseDictionary>(m_map.end(), m_map.end());
396 }
398 System::Details::VirtualizedIteratorBase<KVPair>* virtualizeBeginConstIterator() const override
399 {
400 return new DictionaryIterator<BaseDictionary>(m_map.begin(), m_map.end());
401 }
403 System::Details::VirtualizedIteratorBase<KVPair>* virtualizeEndConstIterator() const override
404 {
405 return new DictionaryIterator<BaseDictionary>(m_map.end(), m_map.end());
406 }
407
408protected:
410 Map m_map;
411
412 // ICollection
416 bool Contains(const KeyValuePair<key_t, mapped_t>& item) const override
417 {
418 auto it = m_map.find(item.m_pair.first);
419
420 return it != m_map.end() && it->second == item.m_pair.second;
421 }
425 bool Remove(const KeyValuePair<key_t, mapped_t>& item) override
426 {
427 auto it = m_map.find(item.m_pair.first);
428
429 if (it != m_map.end() && it->second == item.m_pair.second)
430 {
431 m_map.erase(it);
432 return true;
433 }
434
435 return false;
436 }
437
438#ifdef ASPOSE_GET_SHARED_MEMBERS
441 virtual void GetSharedMembers(System::Object::shared_members_type& result) const override
442 {
443 Object::GetSharedMembers(result);
444 result.PopulateSharedMembers("System::Collections::Generic::BaseDictionary<Map>::m_map[]", m_map);
445 }
446#endif
447
448private:
451 void CopyFrom(BaseType* src)
452 {
453 if (nullptr == src)
454 throw ArgumentNullException(u"Argument cannot be nullptr");
455
456 SharedPtr<IEnumerator<KeyValuePair<key_t, mapped_t> > > enumerator = src->GetEnumerator();
457 while (enumerator->MoveNext())
458 BaseDictionary<Map>::Add(enumerator->get_Current());
459 }
462 void Add(const KeyValuePair<key_t, mapped_t>& item) override
463 {
464 m_map.insert(item.m_pair);
465 }
467 static NotSupportedException ReadOnlyException()
468 {
469 return NotSupportedException(u"The collection is readonly");
470 }
471
472protected:
474 ~BaseDictionary() override {}
475
476#ifdef __DBG_FOR_EACH_MEMBER
477 public:
480 void DBG_for_each_member(DBG::for_each_member_visitor &visitor) const override
481 {
482 visitor.add_self(this);
483 DBG::for_each_of_Object(this, m_map, visitor);
484 }
485
488 const char* DBG_class_name() const override { return "BaseDictionary<T>"; }
489#endif
490
491private:
495 template <typename T, class = void>
496 struct has_equality_operator : std::false_type {};
500 template <typename T>
501 struct has_equality_operator<T, typename std::enable_if<!std::is_enum<T>::value, decltype((void)(std::declval<T>() == nullptr))>::type> : std::true_type
502 {};
503
507 template <typename T>
508 typename std::enable_if<has_equality_operator<T>::value, void>::type CheckIfNull(const T& instance) const
509 {
510 if (instance == nullptr)
511 {
512 throw System::ArgumentNullException(u"key");
513 }
514 }
518 template <typename T>
519 typename std::enable_if<!has_equality_operator<T>::value, void>::type CheckIfNull(const T& instance) const
520 {
521 }
522
523};
524
525}}} // namespace System::Collections::Generic
Implements common code for various dictionary-alike data structures (e. g. Dictionary,...
Definition: base_dictionary.h:100
BaseDictionary(BaseType *src, const Args &... args)
Copying constructor.
Definition: base_dictionary.h:161
ICollection< mapped_t > ValueCollection
Collection of values.
Definition: base_dictionary.h:133
const_iterator begin() const noexcept
Returns an iterator to the KVPair-wrapper for key-value-element of the container. Implemented in C# s...
Definition: base_dictionary.h:357
void SetTemplateWeakPtr(unsigned int argument) override
Definition: base_dictionary.h:104
const_iterator end() const noexcept
Returns an iterator to the KVPair-wrapper for key-value-element following the last element of the con...
Definition: base_dictionary.h:366
System::Details::VirtualizedIteratorBase< KVPair > * virtualizeBeginConstIterator() const override
Gets the implementation of begin const iterator for the current container.
Definition: base_dictionary.h:398
pointer_mode_t m_pointer_mode
Definition: base_dictionary.h:114
mapped_t GetValueOrDefault(const key_t &key, const mapped_t &defaultValue) const override
Returns value if found; or defaultValue otherwise.
Definition: base_dictionary.h:328
KeyValuePair< key_t, mapped_t > KVPair
Key-value pair type.
Definition: base_dictionary.h:135
void _add_range(std::initializer_list< typename Map::value_type > list)
C++ specific.
Definition: base_dictionary.h:309
virtual mapped_t & operator[](const key_t &key)
Accessor function.
Definition: base_dictionary.h:196
SharedPtr< IEnumerator< KeyValuePair< key_t, mapped_t > > > GetEnumerator() override=0
Creates enumerator instance, should be implemented by subclass.
Map m_map
Underlying C++ data structure.
Definition: base_dictionary.h:410
bool Contains(const KeyValuePair< key_t, mapped_t > &item) const override
Checks if key-value pair is present in dictionary, comparing both keys and values.
Definition: base_dictionary.h:416
stl_const_iterator cend() const noexcept
Returns an iterator to the element following the last element of the container. Implemented in STL-st...
Definition: base_dictionary.h:382
void Add(const key_t &key, const mapped_t &value) override
Adds key-value pair into dictionary.
Definition: base_dictionary.h:230
System::Details::VirtualizedIteratorBase< KVPair > * virtualizeEndIterator() override
Gets the implementation of end iterator for the current container.
Definition: base_dictionary.h:393
bool ContainsKey(const key_t &key) const override
Checks if key is present in dictionary.
Definition: base_dictionary.h:246
ICollection< key_t > KeyCollection
Make sure we use correct allocator with underlying storage type.
Definition: base_dictionary.h:131
~BaseDictionary() override
Destructor.
Definition: base_dictionary.h:474
const Map & data() const
Underlying data storage accessor.
Definition: base_dictionary.h:306
Map map_t
Internal map type.
Definition: base_dictionary.h:111
stl_const_iterator cbegin() const noexcept
Returns an iterator to the first element of the container. Implemented in STL-style....
Definition: base_dictionary.h:374
System::Details::VirtualizedIteratorBase< KVPair > * virtualizeEndConstIterator() const override
Gets the implementation of end const iterator for the current container.
Definition: base_dictionary.h:403
mapped_t GetValueOrNull(const key_t &key) const override
Returns value if found; or null otherwise. Make sense only for reference types.
Definition: base_dictionary.h:341
void idx_set(const key_t &key, mapped_t value) override
Keyed setter function. Alters or creates element.
Definition: base_dictionary.h:218
Map & data()
Underlying data storage accessor.
Definition: base_dictionary.h:303
KVPairIterator< KVPair, Map > iterator
Iterator type.
Definition: base_dictionary.h:141
bool Remove(const key_t &key) override
Removes specific key from dictionary.
Definition: base_dictionary.h:254
BaseDictionary()
Creates empty data structure.
Definition: base_dictionary.h:146
void Clear() override
Deletes all elements.
Definition: base_dictionary.h:187
mapped_t GetValueOrDefault(const key_t &key) const override
Returns value if found; or Value() otherwise.
Definition: base_dictionary.h:315
KVPairIterator< KVPair, Map > const_iterator
Const iterator type.
Definition: base_dictionary.h:143
BaseDictionary(BaseType *src)
Copying constructor.
Definition: base_dictionary.h:169
mapped_t idx_get(const key_t &key) const override
Keyed getter function.
Definition: base_dictionary.h:204
BaseDictionary(int, const Args &... args)
Forwarding constructor to push arguments into underlying map constructor.
Definition: base_dictionary.h:152
bool Remove(const KeyValuePair< key_t, mapped_t > &item) override
Removes specified key-value pair from dictionary, compares both keys and values.
Definition: base_dictionary.h:425
bool ContainsValue(const mapped_t &value)
Checks if value is present in dictionary. Uses operator == to compare values.
Definition: base_dictionary.h:291
bool TryGetValue(const key_t &key, mapped_t &value) const override
Looks for keyed value and retreives it if found.
Definition: base_dictionary.h:271
int32_t get_Count() const override
Gets elements count.
Definition: base_dictionary.h:182
IDictionary< typename Map::key_type, typename Map::mapped_type > BaseType
Implemented interface.
Definition: base_dictionary.h:138
System::Details::VirtualizedIteratorBase< KVPair > * virtualizeBeginIterator() override
Gets the implementation of begin iterator for the current container.
Definition: base_dictionary.h:388
Dictionary iterator that provides KeyValuePair notation.
Definition: base_dictionary.h:21
virtual ~DictionaryIterator()=default
Destructor.
DictionaryIterator(DictionaryIterator &&other) noexcept
Move constructor.
Definition: base_dictionary.h:55
DictionaryIterator(typename Dict::map_t::const_iterator &&iterator, typename Dict::map_t::const_iterator &&end) noexcept
Constructor.
Definition: base_dictionary.h:33
void DecrementIterator() override
Moves the iterator step back.
Definition: base_dictionary.h:71
void IncrementIterator() override
Moves the iterator step forward.
Definition: base_dictionary.h:65
void ShiftIteratorBy(std::ptrdiff_t offset) override
Moves the iterator by the specified number of steps.
Definition: base_dictionary.h:78
DictionaryIterator(const typename Dict::map_t::const_iterator &iterator, const typename Dict::map_t::const_iterator &end)
Constructor.
Definition: base_dictionary.h:46
System::Details::VirtualizedIteratorBase< typename Dict::KeyValuePairType > * CloneIterator() const override
Clones current iterator.
Definition: base_dictionary.h:85
Interface of collection of elements. Objects of this class should only be allocated using System::Mak...
Definition: icollection.h:20
Interface for dictionary-alike containers. Objects of this class should only be allocated using Syste...
Definition: idictionary.h:21
Adapting iterator, wraps std::pair into KVPair expected from Dictionary.
Definition: base_enumerator.h:109
Pair of key and value. This type should be allocated on stack and passed to functions by value or by ...
Definition: keyvalue_pair.h:20
std::pair< TKey, TValue > m_pair
Internal data structure.
Definition: keyvalue_pair.h:23
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
Definition: db_command.h:9