32 #include <Pt/TypeTraits.h>
66 friend T any_cast(
const Any&);
69 friend struct AnyCast;
77 virtual Value* clone(
char*)
const = 0;
78 virtual const std::type_info&
type()
const = 0;
79 virtual bool isRef()
const = 0;
80 virtual void*
get() = 0;
81 virtual const void*
get()
const = 0;
86 class BasicValue :
public Value
89 BasicValue(
const T& value = T())
93 const T& value()
const
99 virtual const std::type_info&
type()
const
100 {
return typeid(T); }
102 virtual Value* clone(
char* data)
const
104 return sizeof(BasicValue<T>) > Any::sizeofData ?
new BasicValue(_value)
105 :
new(data) BasicValue(_value);
108 virtual bool isRef()
const
114 virtual const void*
get()
const
122 template <
typename T>
123 class BasicRefValue :
public Value
126 BasicRefValue(T* value)
130 virtual const std::type_info&
type()
const
131 {
return typeid(T); }
133 virtual Value* clone(
char* data)
const
134 {
return new(data) BasicRefValue(_value); }
136 virtual bool isRef()
const
140 {
return (
void*) _value; }
142 virtual const void*
get()
const
150 class RefValue :
public Value
153 RefValue(
void* value,
const std::type_info& ti)
158 virtual bool isRef()
const
161 virtual const std::type_info&
type()
const
164 virtual Any::Value* clone(
char* data)
const
165 {
return new(data) RefValue(_value, *_ti); }
170 virtual const void*
get()
const
175 const std::type_info* _ti;
179 bool dataUsed()
const
180 {
return static_cast<const void*
>(_value) == static_cast<const void*>(_data); }
193 template <
typename T>
197 _value =
sizeof(BasicValue<T>) > Any::sizeofData ?
new BasicValue<T>(type)
198 :
new(
static_cast<void*
>(_data)) BasicValue<T>(
type);
210 template <
typename T>
215 _value =
new(
static_cast<void*
>(_data)) BasicRefValue<T>(
type);
230 _value =
new(
static_cast<void*
>(_data)) RefValue(type, ti);
244 Any& assign(Value* value);
312 {
return _value && _value->isRef(); }
321 const std::type_info&
type()
const
322 {
return _value ? _value->type() :
typeid(void); }
333 template <
typename T>
337 _value =
sizeof(BasicValue<T>) > Any::sizeofData ?
new BasicValue<T>(rhs)
338 :
new(
static_cast<void*
>(_data)) BasicValue<T>(rhs);
351 template <
typename T>
355 _value =
new(
static_cast<void*
>(_data)) BasicRefValue<T>(rhs);
372 const Any::Value* value()
const
391 return _value->get();
404 const void*
get()
const
407 return _value->get();
414 static const unsigned sizeofData =
sizeof(RefValue);
420 char _data[sizeofData];
425 template <
typename T>
428 static T cast(
const Any& any)
435 typedef typename TypeTraits<T>::Value ValueT;
437 if( any.type() ==
typeid(ValueT) )
439 void* v = any._value->get();
440 ValueT* vtp =
reinterpret_cast<ValueT*
>(v);
445 else if( 0 == std::strcmp(any.type().name(),
typeid(ValueT).name() ) )
447 void* v = any._value->get();
448 ValueT* vtp =
reinterpret_cast<ValueT*
>(v);
453 throw std::bad_cast();
459 template <
typename T>
462 static T* cast(
const Any& any)
469 typedef typename TypeTraits<T>::Value ValueT;
471 if( any.type() ==
typeid(ValueT) )
473 void* v = any._value->get();
474 ValueT* vtp =
reinterpret_cast<ValueT*
>(v);
479 else if( 0 == std::strcmp(any.type().name(),
typeid(ValueT).name() ) )
481 void* v = any._value->get();
482 ValueT* vtp =
reinterpret_cast<ValueT*
>(v);
487 throw std::bad_cast();
502 template <
typename T>
505 return AnyCast<T>::cast(any);
509 inline Any& Any::assign(Value* value)
512 _value = value->clone(_data);
521 _value = val._value->clone(_data);
539 rhs._value = tmp->clone(rhs._data);
547 Value* tmp = rhs._value;
549 _value = tmp->clone(_data);
554 Value* tmp = rhs._value;
569 _value = rhs._value->clone(_data);
Any & swap(Any &other)
Swap values.
Definition: Any.h:525
Any(T *type)
Construct with reference.
Definition: Any.h:211
void clear()
Clear content.
Definition: Any.h:279
Any(void *type, const std::type_info &ti)
Construct with reference.
Definition: Any.h:226
const std::type_info & type() const
Returns type info of assigned type.
Definition: Any.h:321
Any()
Default constructor.
Definition: Any.h:238
bool empty() const
Check if empty.
Definition: Any.h:297
bool isRef() const
Returns true if Any contains a weak reference.
Definition: Any.h:311
T any_cast(const Any &any)
Get contained value.
Definition: Any.h:503
Any & operator=(const T &rhs)
Assign value.
Definition: Any.h:334
Any(const T &type)
Construct with value.
Definition: Any.h:194
Contains an arbitrary type.
Definition: Any.h:63
~Any()
Destructor.
Definition: Any.h:262
Any & operator=(T *rhs)
Assign reference.
Definition: Any.h:352