19 virtual const std::type_info& getTypeInfo()
const = 0;
23 template <
typename Type>
24 class Holder :
public AbstractHolder {
25 static_assert(!std::is_reference<Type>::value,
"Cannot store references in Any");
31 Holder(
const Type& value)
35 : value(std::move(value)) {}
37 virtual const std::type_info& getTypeInfo()
const {
42 return makeAuto<Holder>(value);
49 const Type& get()
const {
56 template <
typename Type>
57 using HolderType = Holder<std::decay_t<Type>>;
59 template <
typename Type>
60 HolderType<Type>* safeCast() {
62 return dynamic_cast<HolderType<Type>*
>(&*data);
68 template <
typename Type>
69 const HolderType<Type>* safeCast()
const {
71 return dynamic_cast<const HolderType<Type>*
>(&*data);
77 template <
typename Type>
78 HolderType<Type>* cast() {
80 return assert_cast<HolderType<Type>*>(&*data);
83 template <
typename Type>
84 const HolderType<Type>* cast()
const {
86 return assert_cast<const HolderType<Type>*>(&*data);
101 *
this = std::move(other);
105 template <
typename Type,
typename = std::enable_if_t<!std::is_same<Any, std::decay_t<Type>>::value>>
107 *
this = std::forward<Type>(value);
113 data = other.data->clone();
122 data = std::move(other.data);
127 template <
typename Type,
typename = std::enable_if_t<!std::is_same<Any, std::decay_t<Type>>::value>>
129 data = makeAuto<HolderType<Type>>(std::forward<Type>(value));
137 template <
typename Type>
138 explicit operator const Type&()
const {
139 return cast<Type>()->
get();
146 template <
typename Type>
147 explicit operator Type&() {
148 return cast<Type>()->get();
159 template <
typename Type>
165 template <
typename Type,
typename = std::enable_if_t<!std::is_same<Any, std::decay_t<Type>>::value>>
167 auto casted = safeCast<Type>();
168 return casted && casted->get() == value;
171 template <
typename Type,
typename = std::enable_if_t<!std::is_same<Any, std::decay_t<Type>>::value>>
173 return !(*
this == std::forward<Type>(value));
177 template <
typename Type>
179 const Any::Holder<Type>* casted =
any.safeCast<Type>();
180 if (casted ==
nullptr) {
184 return casted->get();
Optional< Type > anyCast(const Any &any)
#define SPH_ASSERT(x,...)
Simplified implementation of std::unique_ptr, using only default deleter.
INLINE bool any(const Indices &i)
#define NAMESPACE_SPH_END
Wrapper of type value of which may or may not be present.
const NothingType NOTHING
Type-safe object that can store value of any type, similar to std::any.
bool operator!=(Type &&value) const
Any & operator=(Type &&value)
Assigns given value to Any, type is deduced.
bool hasValue() const
Checks if Any currently holds a values.
bool operator==(Type &&value) const
Compares Any with another value.
friend Optional< Type > anyCast(const Any &any)
Tries to extract value of given type from Any.
Any(Any &&other)
Moves the value from another Any.
Any & operator=(const Any &other)
Copies the value from another Any.
Any(Type &&value)
Constructs Any from given value, type is deduced.
Any & operator=(Any &&other)
Moves the value from another Any.
Any(const Any &other)
Copies the value from another Any.
Any()=default
Constructs an empty Any.
INLINE RawPtr< T > get() const
Wrapper of type value of which may or may not be present.
Base class for all polymorphic objects.