SMTK
@SMTK_VERSION@
Simulation Modeling Tool Kit
|
11 #ifndef smtk_common_TypeMap_h
12 #define smtk_common_TypeMap_h
14 #include "smtk/CoreExports.h"
16 #include "smtk/SystemConfig.h"
18 #include "smtk/common/CompilerInformation.h"
21 SMTK_THIRDPARTY_PRE_INCLUDE
22 #include "nlohmann/json.hpp"
23 SMTK_THIRDPARTY_POST_INCLUDE
27 #include <type_traits>
28 #include <unordered_map>
43 virtual void to_json(nlohmann::json&)
const {}
44 virtual void from_json(
const nlohmann::json&) {}
46 virtual void clear() {}
47 virtual std::size_t size()
const {
return 0; }
50 template<
typename KeyType>
57 template<
typename KeyType,
typename Type>
67 typedef KeyType key_type;
68 typedef Type mapped_type;
78 bool contains(
const KeyType& key)
const {
return (m_data.find(key) != m_data.end()); }
81 bool insert(
const KeyType& key,
const Type& value)
83 return m_data.insert(std::make_pair(key, value)).second;
87 bool emplace(
const KeyType& key, Type&& value)
89 return m_data.emplace(std::make_pair(key, std::move(value))).second;
93 void erase(
const KeyType& key) { m_data.erase(key); }
96 Type&
operator[](
const KeyType& key) {
return m_data[key]; }
99 Type&
at(
const KeyType& key) {
return m_data.at(key); }
102 const Type&
at(
const KeyType& key)
const {
return m_data.at(key); }
105 std::unordered_map<KeyType, Type>&
data() {
return m_data; }
106 const std::unordered_map<KeyType, Type>& data()
const {
return m_data; }
108 std::size_t size()
const override {
return m_data.size(); }
109 void clear()
override { m_data.clear(); }
111 void to_json(nlohmann::json& j)
const override {
return to_json<Type>(j); }
113 void from_json(
const nlohmann::json& j)
override {
return from_json<Type>(j); }
117 typename std::enable_if<nlohmann::detail::is_compatible_type<nlohmann::json, T>::value>::type
118 to_json(nlohmann::json& j)
const
124 typename std::enable_if<!nlohmann::detail::is_compatible_type<nlohmann::json, T>::value>::type
125 to_json(nlohmann::json&)
const
130 typename std::enable_if<nlohmann::detail::is_compatible_type<nlohmann::json, T>::value>::type
131 from_json(
const nlohmann::json& j)
133 m_data = j.get<decltype(m_data)>();
137 typename std::enable_if<!nlohmann::detail::is_compatible_type<nlohmann::json, T>::value>::type
138 from_json(
const nlohmann::json&)
const
142 std::unordered_map<KeyType, Type> m_data;
150 template<
typename KeyType>
159 template<
typename Type>
162 auto it = m_data.find(smtk::common::typeName<Type>());
163 if (it == m_data.end())
172 template<
typename Type>
173 bool insert(
const KeyType& key,
const Type& value)
175 return get<Type>().insert(key, value);
179 template<
typename Type>
180 bool emplace(
const KeyType& key, Type&& value)
182 return get<Type>().emplace(key, std::forward<Type>(value));
186 template<
typename Type>
189 auto&
property = get<Type>();
196 template<
typename Type>
197 Type&
at(
const KeyType& key)
199 return get<Type>().at(key);
205 template<
typename Type>
206 const Type&
at(
const KeyType& key)
const
208 return get<Type>().at(key);
212 template<
typename Type>
215 std::string name = smtk::common::typeName<Type>();
216 auto it = m_data.find(name);
217 if (it == m_data.end())
219 throw std::domain_error(
"No entry with given type");
226 template<
typename Type>
229 auto it = m_data.find(smtk::common::typeName<Type>());
230 if (it == m_data.end())
232 throw std::domain_error(
"No entry with given type");
239 template<
typename Type>
242 return (m_data.find(smtk::common::typeName<Type>()) != m_data.end());
246 std::unordered_map<std::string, TypeMapEntryBase*>&
data() {
return m_data; }
247 const std::unordered_map<std::string, TypeMapEntryBase*>& data()
const {
return m_data; }
250 std::unordered_map<std::string, TypeMapEntryBase*> m_data;
253 template<
typename KeyType>
254 inline TypeMapBase<KeyType>::~TypeMapBase()
256 for (
auto& pair : m_data)
269 template<
typename KeyType = std::
string>
270 class SMTK_ALWAYS_EXPORT TypeMap :
public TypeMapBase<KeyType>
273 typedef KeyType key_type;
279 template<
typename List>
285 template<
typename List>
286 TypeMap(identity<List>)
291 ~TypeMap()
override =
default;
294 template<
typename Type>
297 std::string key = smtk::common::typeName<Type>();
302 .emplace(std::make_pair(key,
new TypeMapEntry<key_type, Type>))
307 template<
typename Tuple>
310 TypeMap::insertTypes<0, Tuple>();
314 template<std::
size_t I,
typename Tuple>
315 inline typename std::enable_if<I != std::tuple_size<Tuple>::value>::type insertTypes()
317 this->insertType<typename std::tuple_element<I, Tuple>::type>();
318 TypeMap::insertTypes<I + 1, Tuple>();
321 template<std::
size_t I,
typename Tuple>
322 inline typename std::enable_if<I == std::tuple_size<Tuple>::value>::type insertTypes()
The main namespace for the Simulation Modeling Tool Kit (SMTK).
Definition: doc.h:33
void erase(const KeyType &key)
Erase value of type Type indexed by key from the map.
Definition: TypeMap.h:187
bool contains(const KeyType &key) const
Check whether a value associated with key is present.
Definition: TypeMap.h:78
TypeMapEntry< KeyType, Type > & get()
Access values of type Type.
Definition: TypeMap.h:213
The TypeMapBase class holds the storage and API for TypeMap.
Definition: TypeMap.h:151
const Type & at(const KeyType &key) const
Access value of type Type indexed by key.
Definition: TypeMap.h:206
A common base class for properties of a given type.
Definition: TypeMap.h:38
bool insert(const KeyType &key, const Type &value)
Insert (Type, key, value ) into the map.
Definition: TypeMap.h:173
bool emplace(const KeyType &key, Type &&value)
Emplace (key, value ) into the map.
Definition: TypeMap.h:87
std::unordered_map< std::string, TypeMapEntryBase * > & data()
Access the class's underlying data.
Definition: TypeMap.h:246
bool containsType() const
Check whether type Type is supported.
Definition: TypeMap.h:240
void erase(const KeyType &key)
Erase value indexed by key from the map.
Definition: TypeMap.h:93
const TypeMapEntry< KeyType, Type > & get() const
Access values of type Type.
Definition: TypeMap.h:227
bool emplace(const KeyType &key, Type &&value)
Emplace (Type, key, value ) into the map.
Definition: TypeMap.h:180
Type & at(const KeyType &key)
Access value of type Type indexed by key.
Definition: TypeMap.h:197
A specialization of the TypeMapBase for a single type.
Definition: TypeMap.h:58
Type & operator[](const KeyType &key)
Access value indexed by key.
Definition: TypeMap.h:96
Type & at(const KeyType &key)
Access value indexed by key.
Definition: TypeMap.h:99
std::unordered_map< KeyType, Type > & data()
Access the class's underlying data.
Definition: TypeMap.h:105
bool contains(const KeyType &key) const
Check whether a value of type Type associated with key is present.
Definition: TypeMap.h:160
TypeMap is a generalized map for storing and accessing data using a key.
Definition: TypeMap.h:51
bool insert(const KeyType &key, const Type &value)
Insert (key, value ) into the map.
Definition: TypeMap.h:81
const Type & at(const KeyType &key) const
Access value indexed by key.
Definition: TypeMap.h:102