CatapultServer  v0.5.0.1 (Elephant)
BaseSetDefaultTraits.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include <memory>
23 
24 namespace catapult { namespace deltaset {
25 
26  // region key + storage traits
27 
28  // KeyType => the data key
29  // ValueType => the data value (this is assumed to be convertible to KeyType)
30  // StorageType => the underlying storage (e.g. pair) composed of both key and value
31 
33  template<typename TSet>
34  struct SetKeyTraits {
35  using KeyType = typename TSet::value_type;
36  using ValueType = typename TSet::value_type;
37  using StorageType = typename TSet::value_type;
38 
40  static constexpr const KeyType& ToKey(const StorageType& element) {
41  return element;
42  }
43  };
44 
46  template<typename TMap>
47  struct MapKeyTraits {
48  using KeyType = typename TMap::key_type;
49  using ValueType = typename TMap::mapped_type;
50  using StorageType = typename TMap::value_type;
51 
53  static constexpr const KeyType& ToKey(const StorageType& element) {
54  return element.first;
55  }
56  };
57 
59  template<typename TSet, typename TMemorySetType = TSet>
61  using SetType = TSet;
62  using MemorySetType = TMemorySetType;
63 
65  using KeyType = typename KeyTraits::KeyType;
66  using ValueType = typename KeyTraits::ValueType;
68 
70  static constexpr bool AllowsNativeValueModification = false;
71 
73  static constexpr const KeyType& ToKey(const StorageType& element) {
74  return KeyTraits::ToKey(element);
75  }
76 
78  static constexpr const StorageType& ToStorage(const ValueType& value) {
79  return value;
80  }
81 
83  template<typename TIterator>
84  static const StorageType& ToStorage(const KeyType& key, TIterator&&) {
85  return key;
86  }
87 
89  static constexpr const ValueType& ToValue(const StorageType& element) {
90  return element;
91  }
92  };
93 
95  template<typename TMap, typename TElementToKeyConverter, typename TMemoryMapType = TMap>
97  using SetType = TMap;
98  using MemorySetType = TMemoryMapType;
99 
101  using KeyType = typename KeyTraits::KeyType;
102  using ValueType = typename KeyTraits::ValueType;
104 
106  static constexpr bool AllowsNativeValueModification = true;
107 
109  static constexpr const KeyType& ToKey(const StorageType& element) {
110  return KeyTraits::ToKey(element);
111  }
112 
114  static constexpr KeyType ToKey(const ValueType& value) {
115  return TElementToKeyConverter::ToKey(value);
116  }
117 
119  static constexpr StorageType ToStorage(const ValueType& value) {
120  return std::make_pair(ToKey(value), value);
121  }
122 
124  template<typename TIterator>
125  static const StorageType& ToStorage(const KeyType&, TIterator&& iter) {
126  return *iter;
127  }
128 
130  static constexpr const ValueType& ToValue(const StorageType& element) {
131  return element.second;
132  }
133 
135  static constexpr ValueType& ToValue(StorageType& element) {
136  return element.second;
137  }
138  };
139 
140  // endregion
141 
142  // region mutability traits
143 
144  // mutability tagging allows BaseSet to optimize for immutable values that can never be modified
145  // in contrast, mutable values have copy-on-write semantics
146 
148  struct MutableTypeTag {};
149 
151  template<typename TElement>
153  using ElementType = TElement;
155 
156  static constexpr TElement Copy(const TElement* pElement) {
157  return *pElement;
158  }
159  };
160 
162  struct ImmutableTypeTag {};
163 
165  template<typename TElement>
167  using ElementType = const TElement;
169  };
170 
171  // endregion
172 
173  // region find traits
174 
175  // used to find values, ensuring that values stored in stl set-based containers are always exposed as const
176  // (because they're not modifiable)
177 
179  template<typename T, bool AllowsNativeValueModification>
180  struct FindTraitsT {
181  using ConstResultType = const T*;
182  using ResultType = const T*;
183 
184  static constexpr ResultType ToResult(const T& value) {
185  return &value;
186  }
187  };
188 
189  template<typename T>
190  struct FindTraitsT<T, true> {
191  using ConstResultType = const T*;
192  using ResultType = T*;
193 
194  // this needs to be a template in order to allow T to be const (immutable)
195  template<typename TValue>
196  static constexpr auto ToResult(TValue& value) {
197  return &value;
198  }
199  };
200 
201  // endregion
202 }}
catapult::deltaset::SetKeyTraits::StorageType
typename TSet::value_type StorageType
Definition: BaseSetDefaultTraits.h:37
catapult::deltaset::SetStorageTraits::ToValue
static constexpr const ValueType & ToValue(const StorageType &element)
Converts a storage type (element) to a value type.
Definition: BaseSetDefaultTraits.h:89
catapult::deltaset::SetStorageTraits::SetType
TSet SetType
Definition: BaseSetDefaultTraits.h:61
catapult::deltaset::SetStorageTraits
Base set compatible traits for stl set types.
Definition: BaseSetDefaultTraits.h:60
catapult::deltaset::MapStorageTraits::SetType
TMap SetType
Definition: BaseSetDefaultTraits.h:97
catapult::deltaset::MutableTypeTraits::ElementType
TElement ElementType
Definition: BaseSetDefaultTraits.h:153
catapult::deltaset::SetStorageTraits::ToStorage
static const StorageType & ToStorage(const KeyType &key, TIterator &&)
Converts key to a storage type.
Definition: BaseSetDefaultTraits.h:84
catapult::deltaset::MapStorageTraits::ValueType
typename KeyTraits::ValueType ValueType
Definition: BaseSetDefaultTraits.h:102
catapult::deltaset::SetStorageTraits::KeyType
typename KeyTraits::KeyType KeyType
Definition: BaseSetDefaultTraits.h:65
catapult::deltaset::SetStorageTraits::ToStorage
static constexpr const StorageType & ToStorage(const ValueType &value)
Converts a value type (value) to a storage type.
Definition: BaseSetDefaultTraits.h:78
catapult::deltaset::ImmutableTypeTraits
Traits used for describing an immutable type.
Definition: BaseSetDefaultTraits.h:166
catapult::deltaset::MapStorageTraits::ToStorage
static constexpr StorageType ToStorage(const ValueType &value)
Converts a value type (value) to a storage type.
Definition: BaseSetDefaultTraits.h:119
catapult::deltaset::SetStorageTraits::AllowsNativeValueModification
static constexpr bool AllowsNativeValueModification
Set values cannot be modified because they are hashed in native container.
Definition: BaseSetDefaultTraits.h:70
catapult::deltaset::MapKeyTraits::ValueType
typename TMap::mapped_type ValueType
Definition: BaseSetDefaultTraits.h:49
catapult::deltaset::MapStorageTraits::MemorySetType
TMemoryMapType MemorySetType
Definition: BaseSetDefaultTraits.h:98
catapult::deltaset::MutableTypeTraits
Traits used for describing a mutable type.
Definition: BaseSetDefaultTraits.h:152
catapult::deltaset::MapStorageTraits::ToKey
static constexpr KeyType ToKey(const ValueType &value)
Converts a value type (value) to a key.
Definition: BaseSetDefaultTraits.h:114
catapult::deltaset::MapStorageTraits
Base set compatible traits for stl map types.
Definition: BaseSetDefaultTraits.h:96
catapult::deltaset::SetStorageTraits::MemorySetType
TMemorySetType MemorySetType
Definition: BaseSetDefaultTraits.h:62
catapult::deltaset::FindTraitsT< T, true >::ToResult
static constexpr auto ToResult(TValue &value)
Definition: BaseSetDefaultTraits.h:196
catapult::deltaset::MapKeyTraits::StorageType
typename TMap::value_type StorageType
Definition: BaseSetDefaultTraits.h:50
catapult::deltaset::ImmutableTypeTag
Tag that indicates a type is immutable.
Definition: BaseSetDefaultTraits.h:162
catapult::deltaset::SetKeyTraits::KeyType
typename TSet::value_type KeyType
Definition: BaseSetDefaultTraits.h:35
catapult::deltaset::SetStorageTraits::StorageType
typename KeyTraits::StorageType StorageType
Definition: BaseSetDefaultTraits.h:67
catapult::deltaset::FindTraitsT::ResultType
const T * ResultType
Definition: BaseSetDefaultTraits.h:182
catapult::deltaset::MapStorageTraits::ToKey
static constexpr const KeyType & ToKey(const StorageType &element)
Converts a storage type (element) to a key.
Definition: BaseSetDefaultTraits.h:109
catapult::deltaset::FindTraitsT
Traits for customizing the behavior of find depending on element type.
Definition: BaseSetDefaultTraits.h:180
catapult::deltaset::MapStorageTraits::ToStorage
static const StorageType & ToStorage(const KeyType &, TIterator &&iter)
Converts iter to a storage type.
Definition: BaseSetDefaultTraits.h:125
catapult::deltaset::FindTraitsT< T, true >::ConstResultType
const T * ConstResultType
Definition: BaseSetDefaultTraits.h:191
catapult::deltaset::SetStorageTraits::ValueType
typename KeyTraits::ValueType ValueType
Definition: BaseSetDefaultTraits.h:66
catapult::deltaset::MapKeyTraits::KeyType
typename TMap::key_type KeyType
Definition: BaseSetDefaultTraits.h:48
catapult::deltaset::MapStorageTraits::StorageType
typename KeyTraits::StorageType StorageType
Definition: BaseSetDefaultTraits.h:103
catapult::deltaset::FindTraitsT::ToResult
static constexpr ResultType ToResult(const T &value)
Definition: BaseSetDefaultTraits.h:184
catapult::deltaset::SetKeyTraits
Key-related traits for stl set types.
Definition: BaseSetDefaultTraits.h:34
catapult::deltaset::MapStorageTraits::KeyType
typename KeyTraits::KeyType KeyType
Definition: BaseSetDefaultTraits.h:101
catapult::deltaset::MapStorageTraits::AllowsNativeValueModification
static constexpr bool AllowsNativeValueModification
Map values can be modified because they are not hashed in native container.
Definition: BaseSetDefaultTraits.h:106
catapult::deltaset::MapStorageTraits::ToValue
static constexpr const ValueType & ToValue(const StorageType &element)
Converts a storage type (element) to a value type.
Definition: BaseSetDefaultTraits.h:130
catapult::deltaset::FindTraitsT::ConstResultType
const T * ConstResultType
Definition: BaseSetDefaultTraits.h:181
catapult::deltaset::SetKeyTraits::ToKey
static constexpr const KeyType & ToKey(const StorageType &element)
Converts a storage type (element) to a key.
Definition: BaseSetDefaultTraits.h:40
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::deltaset::MapStorageTraits::ToValue
static constexpr ValueType & ToValue(StorageType &element)
Converts a storage type (element) to a value type.
Definition: BaseSetDefaultTraits.h:135
catapult::deltaset::ImmutableTypeTraits::ElementType
const TElement ElementType
Definition: BaseSetDefaultTraits.h:167
catapult::deltaset::MapKeyTraits
Key-related traits for stl map types.
Definition: BaseSetDefaultTraits.h:47
catapult::deltaset::MapKeyTraits::ToKey
static constexpr const KeyType & ToKey(const StorageType &element)
Converts a storage type (element) to a key.
Definition: BaseSetDefaultTraits.h:53
catapult::deltaset::SetStorageTraits::ToKey
static constexpr const KeyType & ToKey(const StorageType &element)
Converts a storage type (element) to a key.
Definition: BaseSetDefaultTraits.h:73
catapult::deltaset::FindTraitsT< T, true >::ResultType
T * ResultType
Definition: BaseSetDefaultTraits.h:192
catapult::deltaset::SetKeyTraits::ValueType
typename TSet::value_type ValueType
Definition: BaseSetDefaultTraits.h:36
catapult::deltaset::MutableTypeTraits::Copy
static constexpr TElement Copy(const TElement *pElement)
Definition: BaseSetDefaultTraits.h:156
catapult::deltaset::MutableTypeTag
Tag that indicates a type is mutable.
Definition: BaseSetDefaultTraits.h:148