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)))))
143 template<
typename Type>
146 if (this->contains<Type>())
150 return this->insert<Type>(value);
154 template<
typename Type,
typename... Args>
158 .emplace(std::make_pair(
160 #ifdef SMTK_HAVE_CXX_14
161 std::make_unique<WrapperFor<Type>>(std::make_unique<Type>(std::forward<Args>(args)...))
163 std::unique_ptr<Wrapper>(
164 new WrapperFor<Type>(std::unique_ptr<Type>(
new Type(std::forward<Args>(args)...))))
171 template<
typename Type>
174 auto search = m_container.find(this->keyId<Type>());
175 if (search == m_container.end())
180 return *(
static_cast<WrapperFor<Type>*
>(search->second.get()))->value;
185 template<
typename Type>
186 typename std::enable_if<std::is_default_constructible<Type>::value, Type&>::type
get() noexcept
188 auto search = m_container.find(this->keyId<Type>());
189 if (search == m_container.end())
192 .emplace(std::make_pair(
194 #ifdef SMTK_HAVE_CXX_14
195 std::make_unique<WrapperFor<Type>>(std::make_unique<Type>())
197 std::unique_ptr<Wrapper>(
new WrapperFor<Type>(std::unique_ptr<Type>(
new Type)))
203 return *(
static_cast<WrapperFor<Type>*
>(search->second.get()))->value;
208 template<
typename Type>
209 typename std::enable_if<!std::is_default_constructible<Type>::value, Type&>::type
get()
211 auto search = m_container.find(this->keyId<Type>());
212 if (search == m_container.end())
217 return *(
static_cast<WrapperFor<Type>*
>(search->second.get()))->value;
221 template<
typename Type>
224 return m_container.erase(this->keyId<Type>()) > 0;
228 bool empty() const noexcept {
return m_container.empty(); }
231 std::size_t
size() const noexcept {
return m_container.size(); }
234 void clear() noexcept { m_container.clear(); }
242 std::set<smtk::string::Token>
keys()
const
244 std::set<smtk::string::Token> result;
245 for (
const auto& entry : m_container)
251 catch (std::invalid_argument&)
260 template<
typename Arg,
typename... Args>
261 typename std::enable_if<!std::is_base_of<TypeContainer, Arg>::value,
bool>::type insertAll(
265 return insert<Arg>(arg) && insertAll(args...);
267 bool insertAll() {
return true; }
269 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:228
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:144
void clear() noexcept
Erase all objects held by the container.
Definition: TypeContainer.h:234
const Type & get() const
Access a Type instance, and throw if it is not in the TypeContainer.
Definition: TypeContainer.h:172
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:209
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:186
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:155
Hash id() const
Return the token's ID (usually its hash but possibly not in the case of collisions).
Definition: Token.h:48
bool erase()
Remove a specific type of object from the container.
Definition: TypeContainer.h:222
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:231
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:242
A container for caching and retrieving instances of types.
Definition: TypeContainer.h:37