SMTK
@SMTK_VERSION@
Simulation Modeling Tool Kit
|
11 #ifndef smtk_common_RuntimeTypeContainer_h
12 #define smtk_common_RuntimeTypeContainer_h
14 #include "smtk/common/TypeContainer.h"
130 typename std::enable_if<!std::is_base_of<TypeContainer, Arg>::value,
int>::type = 0>
133 insertAll(arg, args...);
141 return (m_container.find(declaredType.
id()) != m_container.end());
146 template<
typename RuntimeType,
typename ActualType>
150 std::is_convertible<ActualType*, RuntimeType*>::value,
151 "Inserted object must inherit the requested base Type.");
154 .emplace(std::make_pair(
156 #ifdef SMTK_HAVE_CXX_14
159 std::unique_ptr<Wrapper>(
166 m_runtimeObjects[smtk::common::typeName<RuntimeType>()].insert(declaredType);
173 template<
typename RuntimeType,
typename ActualType>
176 if (this->containsRuntime(declaredType))
178 this->eraseRuntime(declaredType);
180 return this->insertRuntime<RuntimeType>(declaredType.
id(), value);
184 template<
typename RuntimeType,
typename... Args>
187 bool didInsert = m_container
188 .emplace(std::make_pair(
190 #ifdef SMTK_HAVE_CXX_14
192 std::make_unique<RuntimeType>(std::forward<Args>(args)...))
194 std::unique_ptr<Wrapper>(
196 new RuntimeType(std::forward<Args>(args)...))))
202 m_runtimeObjects[smtk::common::typeName<RuntimeType>()].insert(declaredType);
208 template<
typename RuntimeType>
211 auto search = m_container.find(declaredType.
id());
212 if (search == m_container.end())
224 template<
typename RuntimeType>
225 typename std::enable_if<std::is_default_constructible<RuntimeType>::value, RuntimeType&>::type
228 auto search = m_container.find(declaredType.id());
229 if (search == m_container.end())
232 .emplace(std::make_pair(
234 #ifdef SMTK_HAVE_CXX_14
237 std::unique_ptr<Wrapper>(
251 template<
typename RuntimeType>
252 typename std::enable_if<!std::is_default_constructible<RuntimeType>::value, RuntimeType&>::type
255 auto search = m_container.find(declaredType.
id());
256 if (search == m_container.end())
269 template<
typename Type>
272 auto it = m_container.find(this->keyId<Type>());
273 if (it != m_container.end())
277 auto baseTypeIt = m_runtimeObjects.find(it->second->objectType());
278 if (baseTypeIt != m_runtimeObjects.end())
285 m_container.erase(it);
294 auto it = m_container.find(declaredType.
id());
295 if (it != m_container.end())
299 auto baseTypeIt = m_runtimeObjects.find(it->second->objectType());
300 if (baseTypeIt != m_runtimeObjects.end())
302 baseTypeIt->second.erase(declaredType);
305 m_container.erase(it);
315 m_runtimeObjects.clear();
319 std::unordered_set<smtk::string::Token> runtimeBaseTypes()
const;
322 std::unordered_set<smtk::string::Token> runtimeTypeNames(
smtk::string::Token baseType)
const;
325 template<
typename RuntimeType>
328 return this->runtimeTypeNames(smtk::common::typeName<RuntimeType>());
334 std::unordered_map<smtk::string::Token, std::unordered_set<smtk::string::Token>>
m_runtimeObjects;
The main namespace for the Simulation Modeling Tool Kit (SMTK).
Definition: doc.h:33
bool emplaceRuntime(smtk::string::Token declaredType, Args &&... args)
Emplace a RuntimeType instance into the TypeContainer.
Definition: RuntimeTypeContainer.h:185
RuntimeTypeContainer(const Arg &arg, const Args &... args)
Construct a RuntimeTypeContainer instance from any number of elements.
Definition: RuntimeTypeContainer.h:131
Definition: TypeContainer.h:53
void clear() noexcept
Erase all objects held by the container.
Definition: TypeContainer.h:252
std::enable_if<!std::is_default_constructible< RuntimeType >::value, RuntimeType & >::type getRuntime(smtk::string::Token declaredType)
For non-default-constructible types, access a Type instance; throw if it is not in the TypeContainer.
Definition: RuntimeTypeContainer.h:253
static Token fromHash(Hash h)
Construct a token given only its hash.
Definition: Token.cxx:105
std::enable_if< std::is_default_constructible< RuntimeType >::value, RuntimeType & >::type getRuntime(smtk::string::Token declaredType) noexcept
For default-constructible types, access a RuntimeType instance, creating one if it is not in the Runt...
Definition: RuntimeTypeContainer.h:226
bool eraseRuntime(smtk::string::Token declaredType)
Remove a specific type of object from the container.
Definition: RuntimeTypeContainer.h:292
const RuntimeType & getRuntime(smtk::string::Token declaredType) const
Access a Type instance, and throw if it is not in the TypeContainer.
Definition: RuntimeTypeContainer.h:209
bool insertRuntime(smtk::string::Token declaredType, const ActualType &value)
Insert a runtime RuntimeType instance into the TypeContainer.
Definition: RuntimeTypeContainer.h:147
bool containsRuntime(smtk::string::Token declaredType) const
Check if a Type is present in the TypeContainer.
Definition: RuntimeTypeContainer.h:139
bool insertOrAssignRuntime(smtk::string::Token declaredType, const ActualType &value)
Insert a Type instance into the RuntimeTypeContainer if it does not exist already or replace it if it...
Definition: RuntimeTypeContainer.h:174
A container that can hold objects by their exact compile-time type as well as a commonly-inherited,...
Definition: RuntimeTypeContainer.h:105
void clear() noexcept
Erase all objects held by the container.
Definition: RuntimeTypeContainer.h:312
Hash id() const
Return the token's ID (usually its hash but possibly not in the case of collisions).
Definition: Token.h:47
const std::string & data() const
Return the string corresponding to the token.
Definition: Token.cxx:46
bool erase()
Remove a specific type of object from the container.
Definition: RuntimeTypeContainer.h:270
Definition: TypeContainer.h:77
A string token identified by an integer.
Definition: Token.h:29
std::unordered_set< smtk::string::Token > runtimeTypeNames() const
Report the declared type-names of objects inserted using the given base type.
Definition: RuntimeTypeContainer.h:326
std::unordered_map< smtk::string::Token, std::unordered_set< smtk::string::Token > > m_runtimeObjects
Map from RuntimeType type-names (i.e., the common base classes used to insert values) to declared typ...
Definition: RuntimeTypeContainer.h:334
A container for caching and retrieving instances of types.
Definition: TypeContainer.h:42