CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
base_dictionary.h
1
2#pragma once
3
4#include <system/collections/idictionary.h>
5#include <system/collections/iread_only_dictionary.h>
6#include <system/cycles_detection.h>
7#include <system/collections/base_enumerator.h>
8#include <system/details/hash_quality_checker.h>
9#include <system/collections/virtualized_iterator.h>
10
11#include <algorithm>
12#include <type_traits>
13
14namespace System { namespace Collections { namespace Generic {
15
18template <typename Dict>
20 : public System::Details::NativeIteratorWrapperBase<typename Dict::KeyValuePairType, typename Dict::map_t::const_iterator>
21 , private System::Details::IteratorPointerUpdater<typename Dict::KeyValuePairType, false>
22{
23 using System::Details::NativeIteratorWrapperBase<typename Dict::KeyValuePairType, typename Dict::map_t::const_iterator>::m_iterator;
24 using System::Details::NativeIteratorWrapperBase<typename Dict::KeyValuePairType, typename Dict::map_t::const_iterator>::m_end;
25 using System::Details::VirtualizedIteratorBase<typename Dict::KeyValuePairType>::m_is_end;
26 using System::Details::VirtualizedIteratorBase<typename Dict::KeyValuePairType>::m_pointer;
27 using System::Details::IteratorPointerUpdater<typename Dict::KeyValuePairType, false>::InitializeIteratorPointer;
28 using System::Details::IteratorPointerUpdater<typename Dict::KeyValuePairType, false>::UpdateIteratorPointer;
29
30public:
34 DictionaryIterator(typename Dict::map_t::const_iterator &&iterator, typename Dict::map_t::const_iterator &&end) noexcept
35 : System::Details::NativeIteratorWrapperBase<typename Dict::KeyValuePairType, typename Dict::map_t::const_iterator>(
36 std::forward<typename Dict::map_t::const_iterator>(iterator),
37 std::forward<typename Dict::map_t::const_iterator>(end)
38 )
39 {
40 InitializeIteratorPointer(m_pointer);
41 if (!m_is_end)
42 UpdateIteratorPointer(m_pointer, typename Dict::KeyValuePairType(m_iterator->first, m_iterator->second));
43 }
47 DictionaryIterator(const typename Dict::map_t::const_iterator &iterator, const typename Dict::map_t::const_iterator &end)
48 : System::Details::NativeIteratorWrapperBase<typename Dict::KeyValuePairType, typename Dict::map_t::const_iterator>(iterator, end)
49 {
50 InitializeIteratorPointer(m_pointer);
51 if (!m_is_end)
52 UpdateIteratorPointer(m_pointer, typename Dict::KeyValuePairType(m_iterator->first, m_iterator->second));
53 }
57 : System::Details::NativeIteratorWrapperBase<typename Dict::KeyValuePairType, typename Dict::map_t::const_iterator>(std::forward(other.m_iterator))
58 {
59 if (!m_is_end)
60 UpdateIteratorPointer(m_pointer, typename Dict::KeyValuePairType(m_iterator->first, m_iterator->second));
61 }
63 virtual ~DictionaryIterator() = default;
64
66 void IncrementIterator() override
67 {
68 if (System::Details::NativeIteratorWrapperBase<typename Dict::KeyValuePairType, typename Dict::map_t::const_iterator>::IteratorIncrementImplementation())
69 UpdateIteratorPointer(m_pointer, typename Dict::KeyValuePairType(m_iterator->first, m_iterator->second));
70 }
72 void DecrementIterator() override
73 {
74 System::Details::IteratorDecrementer<typename Dict::map_t::const_iterator>::decrement(m_iterator);
75 UpdateIteratorPointer(m_pointer, typename Dict::KeyValuePairType(m_iterator->first, m_iterator->second));
76 }
79 void ShiftIteratorBy(std::ptrdiff_t offset) override
80 {
81 if (System::Details::NativeIteratorWrapperBase<typename Dict::KeyValuePairType, typename Dict::map_t::const_iterator>::IteratorShiftImplementation(offset))
82 UpdateIteratorPointer(m_pointer, typename Dict::KeyValuePairType(m_iterator->first, m_iterator->second));
83 }
86 System::Details::VirtualizedIteratorBase<typename Dict::KeyValuePairType>* CloneIterator() const override
87 {
88 return new DictionaryIterator(m_iterator, m_end);
89 }
90};
91
92
99template<typename Map>
100class ASPOSECPP_SHARED_CLASS BaseDictionary
101 : public IDictionary<typename Map::key_type, typename Map::mapped_type>,
102 public IReadOnlyDictionary<typename Map::key_type, typename Map::mapped_type>
103{
104 ASPOSE_MAP_POINTER_MODE_CONTROL(Map)
105
106 typedef System::BaseTypesInfo<IDictionary<typename Map::key_type, typename Map::mapped_type>> BaseTypesInfo;
107
109 RTTI_INFO_TEMPLATE_CLASS(System::Collections::Generic::BaseDictionary<Map>, BaseTypesInfo);
110
111public:
112 void SetTemplateWeakPtr(unsigned int argument) override
113 {
114 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");
115 m_pointer_mode.SetWeak(argument, m_map);
116 }
117
119 typedef Map map_t;
120
121protected:
122 pointer_mode_t m_pointer_mode;
123
124private:
126 typedef typename Map::key_type key_t;
128 typedef typename Map::mapped_type mapped_t;
130 typedef typename Map::iterator mapIt_t;
132 typedef typename Map::const_iterator stl_const_iterator;
133
135 static_assert(std::is_same<allocator_t, typename Map::allocator_type>::value, "Using wrong map type with BaseDictionary");
136
137public:
144
147
150
155
157 BaseDictionary() : ASPOSE_MAP_INIT_ALLOCATOR() {}
158
162 template<class... Args>
163 BaseDictionary(int, const Args&... args)
164 : m_map(args..., ASPOSE_COLLECTION_ALLOCATOR)
165 {}
166
171 template<class... Args>
172 BaseDictionary(BaseType* src, const Args&... args)
173 : m_map(args..., ASPOSE_COLLECTION_ALLOCATOR)
174 {
175 CopyFrom(src);
176 }
177
180 BaseDictionary(BaseType* src) : ASPOSE_MAP_INIT_ALLOCATOR()
181 {
182 CopyFrom(src);
183 }
184
185 // IEnumerable
189
190 // ICollection
193 int32_t get_Count() const override
194 {
195 return static_cast<int32_t>(m_map.size());
196 }
198 void Clear() override
199 {
200 m_map.clear();
201 }
202
203 // IDictionary interface
207 virtual mapped_t& operator[](const key_t& key)
208 {
209 CheckIfNull(key);
210 return m_map[key];
211 }
216 mapped_t idx_get(const key_t& key) const override
217 {
218 CheckIfNull(key);
219 auto it = m_map.find(key);
220 if (it == m_map.end())
221 {
222 throw KeyNotFoundException();
223 }
224
225 return it->second;
226 }
230 void idx_set(const key_t& key, mapped_t value) override
231 {
232 CheckIfNull(key);
233 m_map[key] = value;
234#if defined(ASPOSE_DETECT_BAD_QUALITY_HASH)
235 System::Details::CheckHashFunctionQuality(m_map);
236#endif
237 }
242 void Add(const key_t& key, const mapped_t& value) override
243 {
244 CheckIfNull(key);
245 // insert never replaces values
246 auto op_result = m_map.insert(std::make_pair(key, value));
247 if (! op_result.second)
248 {
249 throw System::ArgumentException(u"key");
250 }
251#if defined(ASPOSE_DETECT_BAD_QUALITY_HASH)
252 System::Details::CheckHashFunctionQuality(m_map);
253#endif
254 }
258 bool ContainsKey(const key_t& key) const override
259 {
260 CheckIfNull(key);
261 return m_map.find(key) != m_map.end();
262 }
266 bool Remove(const key_t& key) override
267 {
268 CheckIfNull(key);
269 auto it = m_map.find(key);
270
271 if (it != m_map.end())
272 {
273 m_map.erase(it);
274 return true;
275 }
276
277 return false;
278 }
283 bool TryGetValue(const key_t& key, mapped_t& value) const override
284 {
285 auto it = m_map.find(key);
286
287 if (it != m_map.end())
288 {
289 value = it->second;
290 return true;
291 }
292
293 // There should be the System::Default<mapped_t>() call, but it has a lot of restrictions,
294 // e.g. System::Default doesn't work with C# structures and delegates
295 value = mapped_t();
296 return false;
297 }
298
299 // BaseDictionary interface
303 bool ContainsValue(const mapped_t& value)
304 {
305 for (const auto& itr : m_map)
306 {
307 if (itr.second == value)
308 return true;
309 }
310 return false;
311 }
312
315 Map& data() { return m_map; }
318 const Map& data() const { return m_map; }
321 void _add_range(std::initializer_list<typename Map::value_type> list)
322 {
323 std::for_each(list.begin(), list.end(), [this](const typename Map::value_type &v) { m_map.insert(v); });
324 }
325
327 mapped_t GetValueOrDefault(const key_t& key) const override
328 {
329 auto it = m_map.find(key);
330
331 if (it != m_map.end())
332 {
333 return it->second;
334 }
335
336 return mapped_t();
337 }
338
340 mapped_t GetValueOrDefault(const key_t& key, const mapped_t& defaultValue) const override
341 {
342 auto it = m_map.find(key);
343
344 if (it != m_map.end())
345 {
346 return it->second;
347 }
348
349 return defaultValue;
350 }
351
353 mapped_t GetValueOrNull(const key_t& key) const override
354 {
355 auto it = m_map.find(key);
356
357 if (it != m_map.end())
358 {
359 return it->second;
360 }
361
362 return mapped_t();
363 }
364
369 const_iterator begin() const noexcept
370 {
371 return m_map.begin();
372 }
373
378 const_iterator end() const noexcept
379 {
380 return m_map.end();
381 }
382
386 stl_const_iterator cbegin() const noexcept
387 {
388 return m_map.cbegin();
389 }
390
394 stl_const_iterator cend() const noexcept
395 {
396 return m_map.cend();
397 }
398
400 System::Details::VirtualizedIteratorBase<KVPair>* virtualizeBeginIterator() override
401 {
402 return new DictionaryIterator<BaseDictionary>(m_map.begin(), m_map.end());
403 }
405 System::Details::VirtualizedIteratorBase<KVPair>* virtualizeEndIterator() override
406 {
407 return new DictionaryIterator<BaseDictionary>(m_map.end(), m_map.end());
408 }
410 System::Details::VirtualizedIteratorBase<KVPair>* virtualizeBeginConstIterator() const override
411 {
412 return new DictionaryIterator<BaseDictionary>(m_map.begin(), m_map.end());
413 }
415 System::Details::VirtualizedIteratorBase<KVPair>* virtualizeEndConstIterator() const override
416 {
417 return new DictionaryIterator<BaseDictionary>(m_map.end(), m_map.end());
418 }
419
420protected:
422 Map m_map;
423
424 // ICollection
428 bool Contains(const KeyValuePair<key_t, mapped_t>& item) const override
429 {
430 auto it = m_map.find(item.m_pair.first);
431
432 return it != m_map.end() && it->second == item.m_pair.second;
433 }
437 bool Remove(const KeyValuePair<key_t, mapped_t>& item) override
438 {
439 auto it = m_map.find(item.m_pair.first);
440
441 if (it != m_map.end() && it->second == item.m_pair.second)
442 {
443 m_map.erase(it);
444 return true;
445 }
446
447 return false;
448 }
449
450#ifdef ASPOSE_GET_SHARED_MEMBERS
453 virtual void GetSharedMembers(System::Object::shared_members_type& result) const override
454 {
455 Object::GetSharedMembers(result);
456 result.PopulateSharedMembers("System::Collections::Generic::BaseDictionary<Map>::m_map[]", m_map);
457 }
458#endif
459
460private:
463 void CopyFrom(BaseType* src)
464 {
465 if (nullptr == src)
466 throw ArgumentNullException(u"Argument cannot be nullptr");
467
468 SharedPtr<IEnumerator<KeyValuePair<key_t, mapped_t> > > enumerator = src->GetEnumerator();
469 while (enumerator->MoveNext())
470 BaseDictionary<Map>::Add(enumerator->get_Current());
471 }
474 void Add(const KeyValuePair<key_t, mapped_t>& item) override
475 {
476 m_map.insert(item.m_pair);
477 }
479 static NotSupportedException ReadOnlyException()
480 {
481 return NotSupportedException(u"The collection is readonly");
482 }
483
484protected:
486 ~BaseDictionary() override {}
487
488#ifdef __DBG_FOR_EACH_MEMBER
489 public:
492 void DBG_for_each_member(DBG::for_each_member_visitor &visitor) const override
493 {
494 visitor.add_self(this);
495 DBG::for_each_of_Object(this, m_map, visitor);
496 }
497
500 const char* DBG_class_name() const override { return "BaseDictionary<T>"; }
501#endif
502
503private:
507 template <typename T, class = void>
508 struct has_equality_operator : std::false_type {};
512 template <typename T>
513 struct has_equality_operator<T, typename std::enable_if<!std::is_enum<T>::value, decltype((void)(std::declval<T>() == nullptr))>::type> : std::true_type
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 if (instance == nullptr)
523 {
524 throw System::ArgumentNullException(u"key");
525 }
526 }
530 template <typename T>
531 typename std::enable_if<!has_equality_operator<T>::value, void>::type CheckIfNull(const T& instance) const
532 {
533 }
534
535};
536
537}}} // namespace System::Collections::Generic
Implements common code for various dictionary-alike data structures (e. g. Dictionary,...
Definition: base_dictionary.h:103
BaseDictionary(BaseType *src, const Args &... args)
Copying constructor.
Definition: base_dictionary.h:172
ICollection< mapped_t > ValueCollection
Collection of values.
Definition: base_dictionary.h:141
KVPair ValueType
Value type name.
Definition: base_dictionary.h:149
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:369
void SetTemplateWeakPtr(unsigned int argument) override
Definition: base_dictionary.h:112
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:378
System::Details::VirtualizedIteratorBase< KVPair > * virtualizeBeginConstIterator() const override
Gets the implementation of begin const iterator for the current container.
Definition: base_dictionary.h:410
pointer_mode_t m_pointer_mode
Definition: base_dictionary.h:122
mapped_t GetValueOrDefault(const key_t &key, const mapped_t &defaultValue) const override
Returns value if found; or defaultValue otherwise.
Definition: base_dictionary.h:340
KeyValuePair< key_t, mapped_t > KVPair
Key-value pair type.
Definition: base_dictionary.h:143
void _add_range(std::initializer_list< typename Map::value_type > list)
C++ specific.
Definition: base_dictionary.h:321
virtual mapped_t & operator[](const key_t &key)
Accessor function.
Definition: base_dictionary.h:207
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:422
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:428
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:394
void Add(const key_t &key, const mapped_t &value) override
Adds key-value pair into dictionary.
Definition: base_dictionary.h:242
System::Details::VirtualizedIteratorBase< KVPair > * virtualizeEndIterator() override
Gets the implementation of end iterator for the current container.
Definition: base_dictionary.h:405
bool ContainsKey(const key_t &key) const override
Checks if key is present in dictionary.
Definition: base_dictionary.h:258
ICollection< key_t > KeyCollection
Make sure we use correct allocator with underlying storage type.
Definition: base_dictionary.h:139
~BaseDictionary() override
Destructor.
Definition: base_dictionary.h:486
const Map & data() const
Underlying data storage accessor.
Definition: base_dictionary.h:318
Map map_t
Internal map type.
Definition: base_dictionary.h:119
stl_const_iterator cbegin() const noexcept
Returns an iterator to the first element of the container. Implemented in STL-style....
Definition: base_dictionary.h:386
System::Details::VirtualizedIteratorBase< KVPair > * virtualizeEndConstIterator() const override
Gets the implementation of end const iterator for the current container.
Definition: base_dictionary.h:415
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:353
void idx_set(const key_t &key, mapped_t value) override
Keyed setter function. Alters or creates element.
Definition: base_dictionary.h:230
Map & data()
Underlying data storage accessor.
Definition: base_dictionary.h:315
KVPairIterator< KVPair, Map > iterator
Iterator type.
Definition: base_dictionary.h:152
bool Remove(const key_t &key) override
Removes specific key from dictionary.
Definition: base_dictionary.h:266
BaseDictionary()
Creates empty data structure.
Definition: base_dictionary.h:157
void Clear() override
Deletes all elements.
Definition: base_dictionary.h:198
mapped_t GetValueOrDefault(const key_t &key) const override
Returns value if found; or Value() otherwise.
Definition: base_dictionary.h:327
KVPairIterator< KVPair, Map > const_iterator
Const iterator type.
Definition: base_dictionary.h:154
BaseDictionary(BaseType *src)
Copying constructor.
Definition: base_dictionary.h:180
mapped_t idx_get(const key_t &key) const override
Keyed getter function.
Definition: base_dictionary.h:216
BaseDictionary(int, const Args &... args)
Forwarding constructor to push arguments into underlying map constructor.
Definition: base_dictionary.h:163
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:437
bool ContainsValue(const mapped_t &value)
Checks if value is present in dictionary. Uses operator == to compare values.
Definition: base_dictionary.h:303
bool TryGetValue(const key_t &key, mapped_t &value) const override
Looks for keyed value and retreives it if found.
Definition: base_dictionary.h:283
int32_t get_Count() const override
Gets elements count.
Definition: base_dictionary.h:193
IDictionary< typename Map::key_type, typename Map::mapped_type > BaseType
Implemented interface.
Definition: base_dictionary.h:146
System::Details::VirtualizedIteratorBase< KVPair > * virtualizeBeginIterator() override
Gets the implementation of begin iterator for the current container.
Definition: base_dictionary.h:400
Dictionary iterator that provides KeyValuePair notation.
Definition: base_dictionary.h:22
virtual ~DictionaryIterator()=default
Destructor.
DictionaryIterator(DictionaryIterator &&other) noexcept
Move constructor.
Definition: base_dictionary.h:56
DictionaryIterator(typename Dict::map_t::const_iterator &&iterator, typename Dict::map_t::const_iterator &&end) noexcept
Constructor.
Definition: base_dictionary.h:34
void DecrementIterator() override
Moves the iterator step back.
Definition: base_dictionary.h:72
void IncrementIterator() override
Moves the iterator step forward.
Definition: base_dictionary.h:66
void ShiftIteratorBy(std::ptrdiff_t offset) override
Moves the iterator by the specified number of steps.
Definition: base_dictionary.h:79
DictionaryIterator(const typename Dict::map_t::const_iterator &iterator, const typename Dict::map_t::const_iterator &end)
Constructor.
Definition: base_dictionary.h:47
System::Details::VirtualizedIteratorBase< typename Dict::KeyValuePairType > * CloneIterator() const override
Clones current iterator.
Definition: base_dictionary.h:86
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
Interface for read-only dictionary-alike containers. Objects of this class should only be allocated u...
Definition: iread_only_dictionary.h:19
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
System::ExceptionWrapper< Details_ArgumentException > ArgumentException
Definition: throw_helper.h:23