CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
dictionary.h
1
2#ifndef _aspose_system_collections_dictionary_h_
3#define _aspose_system_collections_dictionary_h_
4
5#include "system/object.h"
6#include "system/collections/base_dictionary.h"
7#include "system/collections/keyvalue_collection.h"
8#include "system/collections/iequality_comparer.h"
9#include "system/cycles_detection.h"
10#include "system/iequatable.h"
11
12#include <unordered_map>
13
14namespace System {
15namespace Collections {
16namespace Generic {
17
19template<typename T, typename V> class Dictionary;
20
26template<typename T, typename V>
27class DictionaryPtr : public SharedPtr<Dictionary<T, V> >
28{
29public:
35
40 template<class X>
41 V& operator[] (const X& key) const
42 {
43 return (**this)[T(key)];
44 }
48 V& operator[] (const T& key) const
49 {
50 return (**this)[key];
51 }
52};
53
54
93template<typename TKey, typename TValue>
94class ASPOSECPP_SHARED_CLASS Dictionary
95 : public BaseDictionary<std::unordered_map<TKey, TValue, EqualityComparerHashAdapter<TKey>, EqualityComparerAdapter<TKey>, ASPOSE_MAP_ALLOCATOR_TYPE(TKey, TValue)> >
96{
101
103
105 RTTI_INFO_TEMPLATE_CLASS(ThisType, System::BaseTypesInfo<BaseType_>);
106
107public:
113 typedef std::unordered_map<TKey, TValue, EqualityComparerHashAdapter<TKey>, EqualityComparerAdapter<TKey>, ASPOSE_MAP_ALLOCATOR_TYPE(TKey, TValue)> map_t;
114
123
126 : Dictionary(GetDefaultComparer<TKey>())
127 {}
130 Dictionary(const map_t& map)
131 : BaseType(0, map) // 0 - use forwarding ctor
132 {}
135 Dictionary(int capacity)
136 : BaseType(0) // 0 - use forwarding ctor
137 {}
141 : BaseType(src.get())
142 {}
147 : BaseType(src.get(), 8, EqualityComparerHashAdapter<TKey>(comparer), EqualityComparerAdapter<TKey>(comparer)) // 8 is a bucket count
148 {}
152 : BaseType(0, 8, EqualityComparerHashAdapter<TKey>(comparer), EqualityComparerAdapter<TKey>(comparer)) // 0 - use forwarding ctor
153 {}
157 Dictionary(int capacity, const SharedPtr<IEqualityComparer<TKey> >& comparer)
158 : Dictionary(comparer)
159 {}
160
165 class Enumerator : public SimpleEnumerator<map_t, KVPair>
166 {
169 public:
172 Enumerator(Ptr dict) : SimpleEnumerator<map_t, KVPair>(dict, dict->m_map) { }
174 RTTI_INFO_TEMPLATE_CLASS(EnumeratorType, System::BaseTypesInfo<System::Object>);
177 System::Details::VirtualizedIteratorBase<KVPair>* CloneIterator() const override
178 {
179 return new Enumerator(*this);
180 }
181 };
182
185 virtual IEnumeratorPtr GetEnumerator() override
186 {
187 return MakeObject<Enumerator>(MakeSharedPtr(this));
188 }
189
190protected:
192 ~Dictionary() override {}
193
197 {
198 return MakeObject<_KeyCollection<Dictionary<TKey, TValue>>>(MakeSharedPtr(this));
199 }
203 {
204 return MakeObject<_ValueCollection<Dictionary<TKey, TValue>>>(MakeSharedPtr(this));
205 }
206
207#ifdef ASPOSE_GET_SHARED_MEMBERS
209 DEFINE_GET_SHARED_MEMBERS(BaseType::m_map)
210#endif
211
212 private:
213 template<typename T>
214 class DefaultComparer : public IEqualityComparer<T>
215 {
216 public:
217
218 virtual bool Equals(T x, T y) const override
219 {
220 return x->Equals(y);
221 }
222
223 virtual int GetHashCode(T obj) const override
224 {
225 return obj->GetHashCode();
226 }
227 };
228
229 template<typename T>
230 typename std::enable_if<IsSmartPtr<T>::value && std::is_base_of<IEquatable<T>, typename T::Pointee_>::value, SharedPtr<IEqualityComparer<T>> >::type GetDefaultComparer()
231 {
232 return MakeObject<DefaultComparer<T>>();
233 }
234
235 template<typename T>
236 typename std::enable_if<IsSmartPtr<T>::value && !std::is_base_of<IEquatable<T>, typename T::Pointee_>::value, SharedPtr<IEqualityComparer<T>> >::type GetDefaultComparer()
237 {
238 return SharedPtr<IEqualityComparer<T> >();
239 }
240
241 template<typename T>
242 typename std::enable_if<!IsSmartPtr<T>::value, SharedPtr<IEqualityComparer<T>> >::type GetDefaultComparer()
243 {
244 return SharedPtr<IEqualityComparer<T> >();
245 }
246
247#ifdef __DBG_FOR_EACH_MEMBER
248public:
251 void DBG_for_each_member(DBG::for_each_member_visitor &visitor) const override
252 {
253 BaseType::DBG_for_each_member(visitor);
254
255 visitor.add_self(this);
256 }
257
260 const char* DBG_class_name() const override { return "Dictionary<T>"; }
261#endif
262};
263
264} // namespace Generic
265} // namespace Collections
266} // namespace System
267
268#endif // _aspose_system_collections_dictionary_h_
Implements common code for various dictionary-alike data structures (e. g. Dictionary,...
Definition: base_dictionary.h:100
Default comparator class. Uses operator < and operator == to compare values. Objects of this class sh...
Definition: icomparer.h:97
Enumerator that allows iterating through the dictionary. Objects of this class should only be allocat...
Definition: dictionary.h:166
System::Details::VirtualizedIteratorBase< KVPair > * CloneIterator() const override
Clones current iterator.
Definition: dictionary.h:177
RTTI_INFO_TEMPLATE_CLASS(EnumeratorType, System::BaseTypesInfo< System::Object >)
RTTI information.
Enumerator(Ptr dict)
Creates enumerator.
Definition: dictionary.h:172
Forward declaration of Dictionary class.
Definition: dictionary.h:96
std::unordered_map< TKey, TValue, EqualityComparerHashAdapter< TKey >, EqualityComparerAdapter< TKey >, ASPOSE_MAP_ALLOCATOR_TYPE(TKey, TValue)> map_t
Underlying data type.
Definition: dictionary.h:113
SharedPtr< Dictionary< TKey, TValue > > Ptr
Pointer type.
Definition: dictionary.h:116
Dictionary(const map_t &map)
Copies data from map.
Definition: dictionary.h:130
Dictionary(const SharedPtr< IDictionary< TKey, TValue > > &src)
Copy constructor.
Definition: dictionary.h:140
Dictionary(int capacity)
Overload which corresponds to creating pre-allocated dictionary; does no allocation,...
Definition: dictionary.h:135
Dictionary()
Creates empty dictionary.
Definition: dictionary.h:125
SharedPtr< ValueCollection > get_ValuesInternal() const override
Implementation of internal value collection accessor.
Definition: dictionary.h:202
~Dictionary() override
Destructor.
Definition: dictionary.h:192
ICollection< TKey > KeyCollection
Collection of keys to extract.
Definition: dictionary.h:109
Dictionary(int capacity, const SharedPtr< IEqualityComparer< TKey > > &comparer)
Creates empty dictionary.
Definition: dictionary.h:157
Dictionary(const SharedPtr< IEqualityComparer< TKey > > &comparer)
Creates empty dictionary.
Definition: dictionary.h:151
Dictionary(const SharedPtr< IDictionary< TKey, TValue > > &src, const SharedPtr< IEqualityComparer< TKey > > &comparer)
Copy constructor.
Definition: dictionary.h:146
SharedPtr< IEnumerator< KVPair > > IEnumeratorPtr
Pointer to enumerator.
Definition: dictionary.h:122
SharedPtr< IEnumerable< KVPair > > IEnumerablePtr
Pointer to enumerable interface.
Definition: dictionary.h:120
ICollection< TValue > ValueCollection
Collection of values to extract.
Definition: dictionary.h:111
KeyValuePair< TKey, TValue > KVPair
Key-value pair type.
Definition: dictionary.h:118
SharedPtr< KeyCollection > get_KeysInternal() const override
Implementation of internal keys collection accessor.
Definition: dictionary.h:196
virtual IEnumeratorPtr GetEnumerator() override
Creates enumerator object.
Definition: dictionary.h:185
Dictionary pointer class with operator overloads. This type is a pointer to manage other object's del...
Definition: dictionary.h:28
DictionaryPtr(const SharedPtr< Dictionary< T, V > > &obj)
Converts pointer type.
Definition: dictionary.h:34
DictionaryPtr()
Initializes null pointer.
Definition: dictionary.h:31
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 providing means to compare two objects for equality. Objects of this class should only be a...
Definition: iequality_comparer.h:17
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
Iterator class for simple containers holding elements directly using rbegin() and rend() functions....
Definition: base_enumerator.h:81
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
decltype(System::Details::GetByIndex(std::declval< const SmartPtr_ * >(), std::declval< IdxType >()) operator[])(IdxType idx) const
Accessor for array elements. Only compiles if SmartPtr_ is specialization of System::Array.
Definition: smart_ptr.h:822
Definition: db_command.h:9
bool Equals(const TA &a, const TB &b)
Determines the equality of two values applying operator==() to them.
Definition: primitive_types.h:77
SmartPtr< X > MakeSharedPtr(X *p)
Converts raw pointer to smart pointer.
Definition: smart_ptr.h:1650
std::enable_if< std::is_scalar< T >::value, int >::type GetHashCode(const T &obj)
Returns a hash code for the specified scalar value.
Definition: get_hash_code.h:21
Adapter making it possible using IEqualityComparer with STL-styled collections and algorithms....
Definition: iequality_comparer.h:38
Adapter to use IEqualityComparer for hashing. Uses comparator object, if set; otherwise,...
Definition: iequality_comparer.h:159