SMTK
@SMTK_VERSION@
Simulation Modeling Tool Kit
|
11 #ifndef smtk_common_TypeContainer_h
12 #define smtk_common_TypeContainer_h
14 #include "smtk/common/Deprecation.h"
16 #include "smtk/CoreExports.h"
18 #include "smtk/SystemConfig.h"
20 #include "smtk/common/CompilerInformation.h"
23 #include "smtk/string/Token.h"
29 #include <unordered_map>
48 virtual std::unique_ptr<Wrapper> clone()
const = 0;
52 template<
typename Type>
55 template<
typename... Args>
57 : value(std::forward<Args>(v)...)
59 m_objectType = smtk::common::typeName<Type>();
62 std::unique_ptr<Wrapper> clone()
const override
64 #ifdef SMTK_HAVE_CXX_14
65 return std::make_unique<WrapperFor<Type>>(std::make_unique<Type>(*value));
72 std::unique_ptr<Type> value;
81 : std::out_of_range(
"Type \"" +
typeName +
"\" not available in this container")
110 typename std::enable_if<!std::is_base_of<TypeContainer, Arg>::value,
int>::type = 0>
113 insertAll(arg, args...);
119 template<
typename Type>
123 std::string keyName = smtk::common::typeName<Type>();
129 template<
typename Type>
132 return (m_container.find(this->keyId<Type>()) != m_container.end());
137 template<
typename Type>
141 .emplace(std::make_pair(
143 #ifdef SMTK_HAVE_CXX_14
146 std::unique_ptr<Wrapper>(
new WrapperFor<Type>(std::unique_ptr<Type>(
new Type((value)))))
153 template<
typename Type>
156 if (this->contains<Type>())
160 return this->insert<Type>(value);
164 template<
typename Type>
165 SMTK_DEPRECATED_IN_24_01(
"Use insertOrAssign() instead.")
166 bool insert_or_assign(const Type& value)
168 return this->insertOrAssign(value);
172 template<
typename Type,
typename... Args>
176 .emplace(std::make_pair(
178 #ifdef SMTK_HAVE_CXX_14
179 std::make_unique<
WrapperFor<Type>>(std::make_unique<Type>(std::forward<Args>(args)...))
181 std::unique_ptr<Wrapper>(
182 new WrapperFor<Type>(std::unique_ptr<Type>(
new Type(std::forward<Args>(args)...))))
189 template<
typename Type>
192 auto search = m_container.find(this->keyId<Type>());
193 if (search == m_container.end())
203 template<
typename Type>
204 typename std::enable_if<std::is_default_constructible<Type>::value, Type&>::type
get() noexcept
206 auto search = m_container.find(this->keyId<Type>());
207 if (search == m_container.end())
210 .emplace(std::make_pair(
212 #ifdef SMTK_HAVE_CXX_14
215 std::unique_ptr<Wrapper>(
new WrapperFor<Type>(std::unique_ptr<Type>(
new Type)))
226 template<
typename Type>
227 typename std::enable_if<!std::is_default_constructible<Type>::value, Type&>::type
get()
229 auto search = m_container.find(this->keyId<Type>());
230 if (search == m_container.end())
239 template<
typename Type>
242 return m_container.erase(this->keyId<Type>()) > 0;
246 bool empty() const noexcept {
return m_container.empty(); }
249 std::size_t
size() const noexcept {
return m_container.size(); }
252 void clear() noexcept { m_container.clear(); }
260 std::set<smtk::string::Token>
keys()
const
262 std::set<smtk::string::Token> result;
263 for (
const auto& entry : m_container)
269 catch (std::invalid_argument&)
278 template<
typename Arg,
typename... Args>
279 typename std::enable_if<!std::is_base_of<TypeContainer, Arg>::value,
bool>::type insertAll(
283 return insert<Arg>(arg) && insertAll(args...);
285 bool insertAll() {
return true; }
287 std::unordered_map<KeyType, std::unique_ptr<Wrapper>> m_container;
bool empty() const noexcept
Return true if the container holds no objects and false otherwise.
Definition: TypeContainer.h:246
The main namespace for the Simulation Modeling Tool Kit (SMTK).
Definition: doc.h:33
bool contains() const
Check if a Type is present in the TypeContainer.
Definition: TypeContainer.h:130
Definition: TypeContainer.h:53
void clear() noexcept
Erase all objects held by the container.
Definition: TypeContainer.h:252
const Type & get() const
Access a Type instance, and throw if it is not in the TypeContainer.
Definition: TypeContainer.h:190
static Token fromHash(Hash h)
Construct a token given only its hash.
Definition: Token.cxx:105
std::enable_if<!std::is_default_constructible< Type >::value, Type & >::type get()
For non-default-constructible types, access a Type instance; throw if it is not in the TypeContainer.
Definition: TypeContainer.h:227
std::enable_if< std::is_default_constructible< Type >::value, Type & >::type get() noexcept
For default-constructible types, access a Type instance, creating one if it is not in the TypeContain...
Definition: TypeContainer.h:204
KeyType keyId() const
Return the ID used to index a given Type.
Definition: TypeContainer.h:120
std::size_t Hash
A fixed-size integer type used to represent an arbitrary-length string.
Definition: Manager.h:34
Definition: TypeContainer.h:45
bool emplace(Args &&... args)
Emplace a Type instance into the TypeContainer.
Definition: TypeContainer.h:173
Hash id() const
Return the token's ID (usually its hash but possibly not in the case of collisions).
Definition: Token.h:47
bool erase()
Remove a specific type of object from the container.
Definition: TypeContainer.h:240
std::string typeName()
Return the name of a class.
Definition: TypeName.h:289
Definition: TypeContainer.h:77
bool insert(const Type &value)
Insert a Type instance into the TypeContainer.
Definition: TypeContainer.h:138
std::size_t size() const noexcept
Return the nubmer of objects held by the container.
Definition: TypeContainer.h:249
A string token identified by an integer.
Definition: Token.h:29
TypeContainer(const Arg &arg, const Args &... args)
Construct a TypeContainer instance from any number of elements.
Definition: TypeContainer.h:111
std::set< smtk::string::Token > keys() const
Return a set of keys corresponding to the values in the container.
Definition: TypeContainer.h:260
bool insertOrAssign(const Type &value)
Insert a Type instance into the TypeContainer if it does not exist already or replace it if it does.
Definition: TypeContainer.h:154
A container for caching and retrieving instances of types.
Definition: TypeContainer.h:42