SMTK
@SMTK_VERSION@
Simulation Modeling Tool Kit
|
11 #ifndef smtk_common_TypeContainer_h
12 #define smtk_common_TypeContainer_h
14 #include "smtk/CoreExports.h"
16 #include "smtk/SystemConfig.h"
18 #include "smtk/common/CompilerInformation.h"
21 #include "smtk/string/Token.h"
27 #include <unordered_map>
41 virtual ~Wrapper() =
default;
42 virtual std::unique_ptr<Wrapper> clone()
const = 0;
45 template<
typename Type>
46 struct WrapperFor : Wrapper
48 template<
typename... Args>
49 WrapperFor(Args&&... v)
50 : value(std::forward<Args>(v)...)
54 std::unique_ptr<Wrapper> clone()
const override
56 #ifdef SMTK_HAVE_CXX_14
57 return std::make_unique<WrapperFor<Type>>(std::make_unique<Type>(*value));
59 return std::unique_ptr<Wrapper>(
new WrapperFor<Type>(
new Type(*value)));
63 std::unique_ptr<Type> value;
71 : std::out_of_range(
"Type \"" +
typeName +
"\" not available in this container")
100 typename std::enable_if<!std::is_base_of<TypeContainer, Arg>::value,
int>::type = 0>
103 insertAll(arg, args...);
109 template<
typename Type>
113 std::string keyName = smtk::common::typeName<Type>();
119 template<
typename Type>
122 return (m_container.find(this->keyId<Type>()) != m_container.end());
127 template<
typename Type>
131 .emplace(std::make_pair(
133 #ifdef SMTK_HAVE_CXX_14
134 std::make_unique<WrapperFor<Type>>(std::make_unique<Type>(value))))
136 std::unique_ptr<Wrapper>(
new WrapperFor<Type>(std::unique_ptr<Type>(
new Type((value)))))))
142 template<
typename Type>
145 if (this->contains<Type>())
149 return this->insert<Type>(value);
153 template<
typename Type,
typename... Args>
157 .emplace(std::make_pair(
159 #ifdef SMTK_HAVE_CXX_14
160 std::make_unique<WrapperFor<Type>>(std::make_unique<Type>(std::forward<Args>(args)...))))
162 std::unique_ptr<Wrapper>(
163 new WrapperFor<Type>(std::unique_ptr<Type>(
new Type(std::forward<Args>(args)...))))))
169 template<
typename Type>
172 auto search = m_container.find(this->keyId<Type>());
173 if (search == m_container.end())
178 return *(
static_cast<WrapperFor<Type>*
>(search->second.get()))->value;
183 template<
typename Type>
184 typename std::enable_if<std::is_default_constructible<Type>::value, Type&>::type
get() noexcept
186 auto search = m_container.find(this->keyId<Type>());
187 if (search == m_container.end())
190 .emplace(std::make_pair(
192 #ifdef SMTK_HAVE_CXX_14
193 std::make_unique<WrapperFor<Type>>(std::make_unique<Type>())))
195 std::unique_ptr<Wrapper>(
new WrapperFor<Type>(std::unique_ptr<Type>(
new Type)))))
200 return *(
static_cast<WrapperFor<Type>*
>(search->second.get()))->value;
205 template<
typename Type>
206 typename std::enable_if<!std::is_default_constructible<Type>::value, Type&>::type
get()
208 auto search = m_container.find(this->keyId<Type>());
209 if (search == m_container.end())
214 return *(
static_cast<WrapperFor<Type>*
>(search->second.get()))->value;
218 template<
typename Type>
221 return m_container.erase(this->keyId<Type>()) > 0;
225 bool empty() const noexcept {
return m_container.empty(); }
228 std::size_t
size() const noexcept {
return m_container.size(); }
231 void clear() noexcept { m_container.clear(); }
239 std::set<smtk::string::Token>
keys()
const
241 std::set<smtk::string::Token> result;
242 for (
const auto& entry : m_container)
248 catch (std::invalid_argument&)
257 template<
typename Arg,
typename... Args>
258 typename std::enable_if<!std::is_base_of<TypeContainer, Arg>::value,
bool>::type insertAll(
262 return insert<Arg>(arg) && insertAll(args...);
264 bool insertAll() {
return true; }
266 std::unordered_map<std::size_t, std::unique_ptr<Wrapper>> m_container;
bool empty() const noexcept
Return true if the container holds no objects and false otherwise.
Definition: TypeContainer.h:225
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:120
bool insert_or_assign(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:143
void clear() noexcept
Erase all objects held by the container.
Definition: TypeContainer.h:231
const Type & get() const
Access a Type instance, and throw if it is not in the TypeContainer.
Definition: TypeContainer.h:170
static Token fromHash(Hash h)
Construct a token given only its hash.
Definition: Token.cxx:95
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:206
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:184
KeyType keyId() const
Return the ID used to index a given Type.
Definition: TypeContainer.h:110
std::size_t Hash
A fixed-size integer type used to represent an arbitrary-length string.
Definition: Manager.h:34
bool emplace(Args &&... args)
Emplace a Type instance into the TypeContainer.
Definition: TypeContainer.h:154
Hash id() const
Return the token's ID (usually its hash but possibly not in the case of collisions).
Definition: Token.h:45
bool erase()
Remove a specific type of object from the container.
Definition: TypeContainer.h:219
std::string typeName()
Return the name of a class.
Definition: TypeName.h:286
Definition: TypeContainer.h:67
bool insert(const Type &value)
Insert a Type instance into the TypeContainer.
Definition: TypeContainer.h:128
std::size_t size() const noexcept
Return the nubmer of objects held by the container.
Definition: TypeContainer.h:228
A string token identified by an integer.
Definition: Token.h:30
TypeContainer(const Arg &arg, const Args &... args)
Construct a TypeContainer instance from any number of elements.
Definition: TypeContainer.h:101
std::set< smtk::string::Token > keys() const
Return a set of keys corresponding to the values in the container.
Definition: TypeContainer.h:239
A container for caching and retrieving instances of types.
Definition: TypeContainer.h:37