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 CheckIfNull(key);
199 return m_map[key];
200 }
205 mapped_t idx_get(const key_t& key) const override
206 {
207 CheckIfNull(key);
208 auto it = m_map.find(key);
209 if (it == m_map.end())
210 {
211 throw KeyNotFoundException();
212 }
213
214 return it->second;
215 }
219 void idx_set(const key_t& key, mapped_t value) override
220 {
221 CheckIfNull(key);
222 m_map[key] = value;
223#if defined(ASPOSE_DETECT_BAD_QUALITY_HASH)
224 System::Details::CheckHashFunctionQuality(m_map);
225#endif
226 }
231 void Add(const key_t& key, const mapped_t& value) override
232 {
233 CheckIfNull(key);
234 // insert never replaces values
235 auto op_result = m_map.insert(std::make_pair(key, value));
236 if (! op_result.second)
237 {
238 throw System::ArgumentException(u"key");
239 }
240#if defined(ASPOSE_DETECT_BAD_QUALITY_HASH)
241 System::Details::CheckHashFunctionQuality(m_map);
242#endif
243 }
247 bool ContainsKey(const key_t& key) const override
248 {
249 CheckIfNull(key);
250 return m_map.find(key) != m_map.end();
251 }
255 bool Remove(const key_t& key) override
256 {
257 CheckIfNull(key);
258 auto it = m_map.find(key);
259
260 if (it != m_map.end())
261 {
262 m_map.erase(it);
263 return true;
264 }
265
266 return false;
267 }
272 bool TryGetValue(const key_t& key, mapped_t& value) const override
273 {
274 auto it = m_map.find(key);
275
276 if (it != m_map.end())
277 {
278 value = it->second;
279 return true;
280 }
281
282 // There should be the System::Default<mapped_t>() call, but it has a lot of restrictions,
283 // e.g. System::Default doesn't work with C# structures and delegates
284 value = mapped_t();
285 return false;
286 }
287
288 // BaseDictionary interface
292 bool ContainsValue(const mapped_t& value)
293 {
294 for (const auto& itr : m_map)
295 {
296 if (itr.second == value)
297 return true;
298 }
299 return false;
300 }
301
304 Map& data() { return m_map; }
307 const Map& data() const { return m_map; }
310 void _add_range(std::initializer_list<typename Map::value_type> list)
311 {
312 std::for_each(list.begin(), list.end(), [this](const typename Map::value_type &v) { m_map.insert(v); });
313 }
314
316 mapped_t GetValueOrDefault(const key_t& key) const override
317 {
318 auto it = m_map.find(key);
319
320 if (it != m_map.end())
321 {
322 return it->second;
323 }
324
325 return mapped_t();
326 }
327
329 mapped_t GetValueOrDefault(const key_t& key, const mapped_t& defaultValue) const override
330 {
331 auto it = m_map.find(key);
332
333 if (it != m_map.end())
334 {
335 return it->second;
336 }
337
338 return defaultValue;
339 }
340
342 mapped_t GetValueOrNull(const key_t& key) const override
343 {
344 auto it = m_map.find(key);
345
346 if (it != m_map.end())
347 {
348 return it->second;
349 }
350
351 return mapped_t();
352 }
353
358 const_iterator begin() const noexcept
359 {
360 return m_map.begin();
361 }
362
367 const_iterator end() const noexcept
368 {
369 return m_map.end();
370 }
371
375 stl_const_iterator cbegin() const noexcept
376 {
377 return m_map.cbegin();
378 }
379
383 stl_const_iterator cend() const noexcept
384 {
385 return m_map.cend();
386 }
387
389 System::Details::VirtualizedIteratorBase<KVPair>* virtualizeBeginIterator() override
390 {
391 return new DictionaryIterator<BaseDictionary>(m_map.begin(), m_map.end());
392 }
394 System::Details::VirtualizedIteratorBase<KVPair>* virtualizeEndIterator() override
395 {
396 return new DictionaryIterator<BaseDictionary>(m_map.end(), m_map.end());
397 }
399 System::Details::VirtualizedIteratorBase<KVPair>* virtualizeBeginConstIterator() const override
400 {
401 return new DictionaryIterator<BaseDictionary>(m_map.begin(), m_map.end());
402 }
404 System::Details::VirtualizedIteratorBase<KVPair>* virtualizeEndConstIterator() const override
405 {
406 return new DictionaryIterator<BaseDictionary>(m_map.end(), m_map.end());
407 }
408
409protected:
411 Map m_map;
412
413 // ICollection
417 bool Contains(const KeyValuePair<key_t, mapped_t>& item) const override
418 {
419 auto it = m_map.find(item.m_pair.first);
420
421 return it != m_map.end() && it->second == item.m_pair.second;
422 }
426 bool Remove(const KeyValuePair<key_t, mapped_t>& item) override
427 {
428 auto it = m_map.find(item.m_pair.first);
429
430 if (it != m_map.end() && it->second == item.m_pair.second)
431 {
432 m_map.erase(it);
433 return true;
434 }
435
436 return false;
437 }
438
439#ifdef ASPOSE_GET_SHARED_MEMBERS
442 virtual void GetSharedMembers(System::Object::shared_members_type& result) const override
443 {
444 Object::GetSharedMembers(result);
445 result.PopulateSharedMembers("System::Collections::Generic::BaseDictionary<Map>::m_map[]", m_map);
446 }
447#endif
448
449private:
452 void CopyFrom(BaseType* src)
453 {
454 if (nullptr == src)
455 throw ArgumentNullException(u"Argument cannot be nullptr");
456
457 SharedPtr<IEnumerator<KeyValuePair<key_t, mapped_t> > > enumerator = src->GetEnumerator();
458 while (enumerator->MoveNext())
459 BaseDictionary<Map>::Add(enumerator->get_Current());
460 }
463 void Add(const KeyValuePair<key_t, mapped_t>& item) override
464 {
465 m_map.insert(item.m_pair);
466 }
468 static NotSupportedException ReadOnlyException()
469 {
470 return NotSupportedException(u"The collection is readonly");
471 }
472
473protected:
475 ~BaseDictionary() override {}
476
477#ifdef __DBG_FOR_EACH_MEMBER
478 public:
481 void DBG_for_each_member(DBG::for_each_member_visitor &visitor) const override
482 {
483 visitor.add_self(this);
484 DBG::for_each_of_Object(this, m_map, visitor);
485 }
486
489 const char* DBG_class_name() const override { return "BaseDictionary<T>"; }
490#endif
491
492private:
496 template <typename T, class = void>
497 struct has_equality_operator : std::false_type {};
501 template <typename T>
502 struct has_equality_operator<T, typename std::enable_if<!std::is_enum<T>::value, decltype((void)(std::declval<T>() == nullptr))>::type> : std::true_type
503 {};
504
508 template <typename T>
509 typename std::enable_if<has_equality_operator<T>::value, void>::type CheckIfNull(const T& instance) const
510 {
511 if (instance == nullptr)
512 {
513 throw System::ArgumentNullException(u"key");
514 }
515 }
519 template <typename T>
520 typename std::enable_if<!has_equality_operator<T>::value, void>::type CheckIfNull(const T& instance) const
521 {
522 }
523
524};
525
526}}} // 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:358
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:367
System::Details::VirtualizedIteratorBase< KVPair > * virtualizeBeginConstIterator() const override
Gets the implementation of begin const iterator for the current container.
Definition: base_dictionary.h:399
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:329
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:310
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:411
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:417
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:383
void Add(const key_t &key, const mapped_t &value) override
Adds key-value pair into dictionary.
Definition: base_dictionary.h:231
System::Details::VirtualizedIteratorBase< KVPair > * virtualizeEndIterator() override
Gets the implementation of end iterator for the current container.
Definition: base_dictionary.h:394
bool ContainsKey(const key_t &key) const override
Checks if key is present in dictionary.
Definition: base_dictionary.h:247
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:475
const Map & data() const
Underlying data storage accessor.
Definition: base_dictionary.h:307
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:375
System::Details::VirtualizedIteratorBase< KVPair > * virtualizeEndConstIterator() const override
Gets the implementation of end const iterator for the current container.
Definition: base_dictionary.h:404
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:342
void idx_set(const key_t &key, mapped_t value) override
Keyed setter function. Alters or creates element.
Definition: base_dictionary.h:219
Map & data()
Underlying data storage accessor.
Definition: base_dictionary.h:304
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:255
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:316
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:205
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:426
bool ContainsValue(const mapped_t &value)
Checks if value is present in dictionary. Uses operator == to compare values.
Definition: base_dictionary.h:292
bool TryGetValue(const key_t &key, mapped_t &value) const override
Looks for keyed value and retreives it if found.
Definition: base_dictionary.h:272
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:389
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:126
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