1 #ifndef SIPLASPLAS_TYPEERASURE_SIPLEANY_HPP 2 #define SIPLASPLAS_TYPEERASURE_SIPLEANY_HPP 4 #include "typeinfo.hpp" 5 #include "anystorage/deadpool.hpp" 6 #include "anystorage/nonowning.hpp" 8 #include <siplasplas/utility/assert.hpp> 14 template<
typename Storage>
21 class
SimpleAny<cpp::ConstNonOwningStorage>;
30 template<typename LhsStorage, typename RhsStorage>
31 bool sameType(const SimpleAny<LhsStorage>& lhs, const SimpleAny<RhsStorage>& rhs)
33 return lhs.typeInfo() == rhs.typeInfo();
48 _typeInfo{cpp::typeerasure::TypeInfo::get<EmptyTag>()}
60 return hasType<EmptyTag>();
66 _typeInfo{cpp::typeerasure::TypeInfo::get<T>()}
79 static SimpleAny
create(
const T& value)
92 return cpp::typeerasure::TypeInfo::get<T>() == _typeInfo;
102 const std::decay_t<T>&
get()
const 105 #ifdef SIPLASPLAS_TYPEERASURE_SIMPLEANY_TYPECHECKS 134 _typeInfo = cpp::typeerasure::TypeInfo::get<T>();
169 _typeInfo{cpp::typeerasure::TypeInfo::get<EmptyTag>()}
181 return hasType<EmptyTag>();
187 _typeInfo{cpp::typeerasure::TypeInfo::get<std::decay_t<T>>()}
213 return cpp::typeerasure::TypeInfo::get<T>() == _typeInfo;
223 const std::decay_t<T>&
get()
const 226 #ifdef SIPLASPLAS_TYPEERASURE_SIMPLEANY_TYPECHECKS 239 std::decay_t<T>&
get()
242 #ifdef SIPLASPLAS_TYPEERASURE_SIMPLEANY_TYPECHECKS 271 _typeInfo = cpp::typeerasure::TypeInfo::get<T>();
295 if(!
hasType<std::decay_t<T>>())
297 throw cpp::exception<std::runtime_error>(
298 "Cannot assign an rvalue of type {} to an lvalue reference of type {}",
299 ctti::type_id(value).name(),
305 get<std::decay_t<T>>() = std::move(value);
347 template<
typename Storage>
368 template<
typename T,
typename... Args>
371 return SimpleAny{meta::identity<T>(), std::forward<Args>(args)...};
378 _typeInfo{cpp::typeerasure::TypeInfo::get<EmptyTag>()}
403 return hasType<EmptyTag>();
413 template<
typename T,
typename = std::enable_if_t<
420 _typeInfo{cpp::typeerasure::TypeInfo::get<T>()}
423 _typeInfo.copyConstruct(Storage::storage(_typeInfo), &value);
426 template<
typename OtherStorage>
433 template<
typename OtherStorage>
460 return cpp::typeerasure::TypeInfo::get<T>() == _typeInfo;
472 const std::decay_t<T>&
get()
const 475 #ifdef SIPLASPLAS_TYPEERASURE_SIMPLEANY_TYPECHECKS 478 if(_typeInfo.isPointer())
480 return **
reinterpret_cast<const std::decay_t<T>*
const *
>(Storage::storage(_typeInfo));
484 return *
reinterpret_cast<const std::decay_t<T>*
>(Storage::storage(_typeInfo));
497 std::decay_t<T>&
get()
500 #ifdef SIPLASPLAS_TYPEERASURE_SIMPLEANY_TYPECHECKS 503 if(_typeInfo.isPointer())
505 return **
reinterpret_cast<std::decay_t<T>**
>(Storage::storage(_typeInfo));
509 return *
reinterpret_cast<std::decay_t<T>*
>(Storage::storage(_typeInfo));
526 return {Storage::storage(_typeInfo), _typeInfo};
534 return {Storage::storage(_typeInfo), _typeInfo};
552 _typeInfo.destroy(Storage::storage(_typeInfo));
553 _typeInfo = cpp::typeerasure::TypeInfo::get<T>();
555 _typeInfo.copyConstruct(Storage::storage(_typeInfo), &value);
559 _typeInfo.copyAssign(Storage::storage(_typeInfo), &value);
565 SimpleAny&
operator=(
const SimpleAny& other)
567 if(!sameType(*
this, other))
569 _typeInfo.destroy(Storage::storage(_typeInfo));
570 _typeInfo = other._typeInfo;
575 _typeInfo.copyAssign(Storage::storage(_typeInfo), other.storage(other.
typeInfo()));
583 if(!sameType(*
this, other))
585 _typeInfo.destroy(Storage::storage(_typeInfo));
586 _typeInfo = other._typeInfo;
591 _typeInfo.moveAssign(Storage::storage(_typeInfo), other.storage(other.
typeInfo()));
599 _typeInfo.destroy(Storage::storage(_typeInfo));
607 return *
static_cast<const Storage*
>(
this);
613 template<
typename T,
typename... Args>
614 SimpleAny(meta::identity<T>, Args&&... args) :
615 _typeInfo{cpp::typeerasure::TypeInfo::get<T>()}
618 features::Constructible::apply<T>(Storage::storage(_typeInfo), std::forward<Args>(args)...);
662 #endif // SIPLASPLAS_TYPEERASURE_SIPLEANY_HPP SimpleAny(const T &value)
Constructs an any of type T from an lvalue of type T.
Definition: simpleany.hpp:419
SimpleAny(const cpp::typeerasure::TypeInfo &typeInfo)
Default constructs a value of the given type.
Definition: simpleany.hpp:388
SimpleAny & operator=(const T &value)
Assigns a value of type T.
Definition: simpleany.hpp:548
cpp::SimpleAny specialization for non-owning const references to existing objects. See cpp::ConstNonOwningStorage
Definition: simpleany.hpp:41
void defaultConstruct(void *where) const
Default constructs a value of the type If the passed argument is not of the represented type...
Definition: typeinfo.hpp:166
void * storage(cpp::typeerasure::TypeInfo typeInfo) const
Returns a pointer to the storage memory space.
Definition: nonowning.hpp:99
void moveConstruct(void *where, void *other) const
Move constructs values of the type If the passed arguments are not of the represented type...
Definition: typeinfo.hpp:191
Definition: canary_allocator.hpp:7
#define SIPLASPLAS_ASSERT_FALSE(...)
Defines a false assertion.
Definition: assert.hpp:415
cpp::SimpleAny< cpp::ConstNonOwningStorage > getReference() const
Returns a const reference any to the hosted object.
Definition: simpleany.hpp:532
void rebind(const void *object)
Reassigns the storage to reference another object.
Definition: nonowning.hpp:36
Implements a read-only non owning storage.
Definition: nonowning.hpp:17
static SimpleAny create(const T &value)
Creates a const reference SimpleAny referencing the given object of type T.
Definition: simpleany.hpp:200
cpp::typeerasure::TypeInfo typeInfo() const
Returns the type information of the hosted type.
Definition: simpleany.hpp:251
SimpleAny & operator=(const T &value)
Assigns a value of type T.
Definition: simpleany.hpp:130
cpp::typeerasure::TypeInfo typeInfo() const
Returns the type information of the hosted type.
Definition: simpleany.hpp:516
bool empty() const
Checks whether the any has an object hosted in or if is empty.
Definition: simpleany.hpp:401
SimpleAny()
Constructs an empty SimpleAny.
Definition: simpleany.hpp:377
const void * storage(cpp::typeerasure::TypeInfo typeInfo) const
Returns a pointer to the storage memory space.
Definition: nonowning.hpp:49
void copyConstruct(void *where, const void *other) const
Copy constructs values of the type If the passed arguments are not of the represented type...
Definition: typeinfo.hpp:178
const ConstNonOwningStorage & getStorage() const
Returns the storage backend of the SimpleAny object.
Definition: simpleany.hpp:148
const Storage & getStorage() const
Returns the storage backend of the SimpleAny object.
Definition: simpleany.hpp:605
bool empty() const
Checks whether the any has an object hosted in or if is empty.
Definition: simpleany.hpp:58
void rebind(void *object)
Reassigns the storage to reference another object.
Definition: nonowning.hpp:86
cpp::SimpleAny specialization for non-owning references to existing objects. See cpp::NonOwningStorag...
Definition: simpleany.hpp:162
bool hasType() const
Checks if the any has a value of type T.
Definition: simpleany.hpp:211
static SimpleAny create(const T &value)
Creates a const reference SimpleAny referencing the given object of type T.
Definition: simpleany.hpp:79
Implements a type-erased value container with minimal value semantics requirements.
Definition: simpleany.hpp:15
cpp::SimpleAny< cpp::NonOwningStorage > getReference()
Returns a reference any to the hosted object.
Definition: simpleany.hpp:524
bool hasType() const
Checks if the any has a value of type T.
Definition: simpleany.hpp:90
bool empty() const
Checks whether the any has an object hosted in or if is empty.
Definition: simpleany.hpp:179
bool hasType() const
Checks if the any has a value of type T.
Definition: simpleany.hpp:458
const char * typeName() const
Returns the name of the type.
Definition: typeinfo.hpp:138
Implements a non owning storage.
Definition: nonowning.hpp:66
Contains minimal information to execute the value semantics operations of a type. ...
Definition: typeinfo.hpp:112
SimpleAny & operator=(T &&value)
Assigns an rvalue of type T.
Definition: simpleany.hpp:293
Type used to represent empty state.
Definition: simpleany.hpp:354
const NonOwningStorage & getStorage() const
Returns the storage backend of the SimpleAny object.
Definition: simpleany.hpp:313
SimpleAny & operator=(T &value)
Assigns a value of type T.
Definition: simpleany.hpp:267
static SimpleAny create(Args &&...args)
Constructs a SimpleAny with an in-place constructed value of type T.
Definition: simpleany.hpp:369
#define SIPLASPLAS_ASSERT_TRUE(...)
Defines a true assertion.
Definition: assert.hpp:398
cpp::typeerasure::TypeInfo typeInfo() const
Returns the type information of the hosted type.
Definition: simpleany.hpp:114