32#include <Pt/TypeTraits.h>
81 friend T any_cast(
const Any&);
84 friend struct AnyCast;
92 virtual Value* clone(
char*)
const = 0;
93 virtual const std::type_info&
type()
const = 0;
94 virtual bool isRef()
const = 0;
95 virtual void*
get() = 0;
96 virtual const void*
get()
const = 0;
100 template <
typename T>
101 class BasicValue :
public Value
104 BasicValue(
const T& value = T())
108 const T& value()
const
114 virtual const std::type_info&
type()
const
115 {
return typeid(T); }
117 virtual Value* clone(
char* data)
const
119 return Any::cloneValue(_value, data);
122 virtual bool isRef()
const
128 virtual const void*
get()
const
136 template <
typename T>
137 class BasicRefValue :
public Value
140 BasicRefValue(T* value)
144 virtual const std::type_info&
type()
const
145 {
return typeid(T); }
147 virtual Value* clone(
char* data)
const
148 {
return new(data) BasicRefValue(_value); }
150 virtual bool isRef()
const
154 {
return (
void*) _value; }
156 virtual const void*
get()
const
164 class RefValue :
public Value
167 RefValue(
void* value,
const std::type_info& ti)
172 virtual bool isRef()
const
175 virtual const std::type_info&
type()
const
178 virtual Any::Value* clone(
char* data)
const
179 {
return new(data) RefValue(_value, *_ti); }
184 virtual const void*
get()
const
189 const std::type_info* _ti;
193 bool dataUsed()
const
194 {
return static_cast<const void*
>(_value) ==
static_cast<const void*
>(_data); }
207 template <
typename T>
211 _value = this->createValue(
type);
223 template <
typename T>
228 _value =
new(
static_cast<void*
>(_data)) BasicRefValue<T>(
type);
243 _value =
new(
static_cast<void*
>(_data)) RefValue(
type, ti);
325 {
return _value && _value->isRef(); }
334 const std::type_info&
type()
const
335 {
return _value ? _value->type() :
typeid(void); }
346 template <
typename T>
350 _value = this->createValue(rhs);
363 template <
typename T>
367 _value =
new(
static_cast<void*
>(_data)) BasicRefValue<T>(rhs);
384 const Any::Value* value()
const
403 return _value->get();
419 return _value->get();
426 static const unsigned sizeofData =
sizeof(RefValue);
428 template <
typename T>
429 struct IsSmallObject : BoolConstant< sizeof(BasicValue<T>) <= Any::sizeofData >
433 template <typename T>
434 static Value* cloneValue(const T& value, char* data);
436 template <typename T>
437 static Value* cloneValue(const T& value, char* data, TrueType);
439 template <typename T>
440 static Value* cloneValue(const T& value, char* data, FalseType);
442 template <typename T>
443 Value* createValue(const T& value);
445 template <typename T>
446 Value* createValue(const T& value, TrueType);
448 template <typename T>
449 Value* createValue(const T& value, FalseType);
456 alignas(Value) char _data[sizeofData];
460inline Any::Value* Any::cloneValue(const T& value, char* data)
462 return Any::cloneValue(value, data, IsSmallObject<T>());
466inline Any::Value* Any::cloneValue(const T& value, char* data, TrueType)
468 return new(data) BasicValue<T>(value);
472inline Any::Value* Any::cloneValue(const T& value, char*, FalseType)
474 return new BasicValue<T>(value);
478inline Any::Value* Any::createValue(const T& value)
480 return this->createValue(value, IsSmallObject<T>());
484inline Any::Value* Any::createValue(const T& value, TrueType)
486 return new(
static_cast<void*
>(_data)) BasicValue<T>(value);
490inline Any::Value* Any::createValue(
const T& value, FalseType)
492 return new BasicValue<T>(value);
500 static T cast(
const Any& any)
509 if( any.type() ==
typeid(ValueT) )
511 void* v = any._value->get();
512 ValueT* vtp =
reinterpret_cast<ValueT*
>(v);
517 else if( 0 == std::strcmp(any.type().name(),
typeid(ValueT).name() ) )
519 void* v = any._value->get();
520 ValueT* vtp =
reinterpret_cast<ValueT*
>(v);
525 throw std::bad_cast();
534 static T* cast(
const Any& any)
543 if( any.type() ==
typeid(ValueT) )
545 void* v = any._value->get();
546 ValueT* vtp =
reinterpret_cast<ValueT*
>(v);
551 else if( 0 == std::strcmp(any.type().name(),
typeid(ValueT).name() ) )
553 void* v = any._value->get();
554 ValueT* vtp =
reinterpret_cast<ValueT*
>(v);
559 throw std::bad_cast();
573 return AnyCast<T>::cast(any);
577inline Any& Any::assign(
Value* value)
580 _value = value->clone(_data);
589 _value = val._value->clone(_data);
607 rhs._value = tmp->clone(rhs._data);
615 Value* tmp = rhs._value;
617 _value = tmp->clone(_data);
622 Value* tmp = rhs._value;
637 _value = rhs._value->clone(_data);
Value of any copyable type.
Definition Any.h:79
Any()
Default constructor.
Definition Any.h:251
const void * get() const
Get pointer to stored value.
Definition Any.h:416
Any & operator=(T *rhs)
Assign reference.
Definition Any.h:364
Any(const T &type)
Construct with value.
Definition Any.h:208
bool empty() const
Check if empty.
Definition Any.h:310
~Any()
Destructor.
Definition Any.h:275
const std::type_info & type() const
Returns type info of assigned type.
Definition Any.h:334
bool isRef() const
Returns true if Any contains a weak reference.
Definition Any.h:324
void * get()
Get pointer to stored value.
Definition Any.h:400
Any & operator=(const T &rhs)
Assign value.
Definition Any.h:347
Any(T *type)
Construct with reference.
Definition Any.h:224
Any(void *type, const std::type_info &ti)
Construct with reference.
Definition Any.h:239
void clear()
Clear content.
Definition Any.h:292
T any_cast(const Any &any)
Returns the value stored in any as type T.
Definition Any.h:571
Any & swap(Any &other)
Swap values.
Definition Any.h:593
void swap(String &a, String &b)
Swaps two strings.
Definition Api-String.h:778
Core module.
Definition Allocator.h:33
IMPLEMENTATION_DEFINED Value
The derived value type.
Definition Api-TypeTraits.h:40