#include <Pt/Any.h>
Value of any copyable type. More...
Public Member Functions | |
| template<typename T> | |
| Any (const T &type) | |
| Construct with value. | |
| template<typename T> | |
| Any (T *type) | |
| Construct with reference. | |
| Any (void *type, const std::type_info &ti) | |
| Construct with reference. | |
| Any () | |
| Default constructor. | |
| Any (const Any &val) | |
| Copy constructor. | |
| ~Any () | |
| Destructor. | |
| void | clear () |
| Clear content. | |
| bool | empty () const |
| Check if empty. | |
| Any & | swap (Any &other) |
| Swap values. | |
| bool | isRef () const |
| Returns true if Any contains a weak reference. | |
| const std::type_info & | type () const |
| Returns type info of assigned type. | |
| template<typename T> | |
| Any & | operator= (const T &rhs) |
| Assign value. | |
| template<typename T> | |
| Any & | operator= (T *rhs) |
| Assign reference. | |
| Any & | operator= (const Any &rhs) |
| Assign value of other Any. | |
| void * | get () |
| Get pointer to stored value. | |
| const void * | get () const |
| Get pointer to stored value. | |
Related Symbols | |
(Note that these are not member symbols.) | |
| template<typename T> | |
| T | any_cast (const Any &any) |
| Returns the value stored in any as type T. | |
Any holds a single value whose type is chosen at run time. Assigning a value by copy stores a copy, as a standard container would. Assigning another Any copies that stored value. The stored type is recovered with any_cast(); the cast succeeds only when the requested type is the type that was stored.
Construction and assignment from a value of type T require T to be copy-constructible. Small values live in an internal buffer. Larger values are allocated. An exception during construction or assignment leaves the Any empty on construction, or unchanged on assignment.
Construction and assignment from a pointer store a reference, not a copy. isRef() is then true. The pointed-to object must outlive the Any. There is also a type-erased pointer constructor that takes a void* and a std::type_info, which must describe the object that pointer refers to.
An empty Any stores nothing. empty() is true, type() is typeid(void), and get() returns a null pointer. clear() destroys the stored value and returns to that state. swap() exchanges two Any objects without allocating.
any_cast<T>() copies or binds the stored value as T. A mismatch throws std::bad_cast. any_cast<T*>() returns a pointer to the stored object, or null when the types do not match. Do not any_cast to a type that is only related by conversion: an Any that holds int does not yield a float.
| Any | ( | const T & | type | ) |
Constructs the Any from an value of arbitrary type. The type to be assigned must be copy-constructible. Memory is allocated to store the value. If an exception is thrown during construction, the Any will be empty and the exception is porpagated.
| type | Value to assign |
|
explicit |
Constructs the Any from a pointer to an arbitrary type. The constructed Any will not make a copy, but only keep a shallow pointer. It is the resposibility of the caller to make sure the type pointed to exists longer than the created Any.
| type | Value to assign |
| Any | ( | void * | type, |
| const std::type_info & | ti ) |
Constructs the Any from a pointer to an arbitrary type. The constructed Any will not make a copy, but only keep a shallow pointer. It is the resposibility of the caller to make sure the type pointed to exists longer than the created Any. The type information ti must match the type pointed to by type.
| Any | ( | ) |
Constructs an empty any. No memory needs to be allocated for empty Anys.
| Any | ( | const Any & | val | ) |
| ~Any | ( | ) |
Deallocates the memory needed to hold the value. This will also destruct the contained type.
| void clear | ( | ) |
Removes the stored type resulting in a destructor call for the stored type. All memory required to hold the value is deallocated.
| bool empty | ( | ) | const |
Returns true if no value has been assigned, false otherwise.
The member function swaps the assigned values between *this and right. No exceptions are thrown, and no memory needs to be allocated.
| other | Other any to swap value |
| const std::type_info & type | ( | ) | const |
Returns the std::type_info of the currently assigned type. If the Any is empty the type_info of void is returned.
| Any & operator= | ( | const T & | rhs | ) |
Assigns a value of an arbitrary type. The type to be assigned must be copy-constructible. Memory is allocated to store the value. If an exception is thrown during construction, the Any will remain unaltered and the exception is porpagated.
| rhs | Value to assign |
| Any & operator= | ( | T * | rhs | ) |
Initializes an Any from a pointer to an arbitrary type. The assignment will not make a copy, but only keep a shallow pointer. It is the resposibility of the caller to make sure the type pointed to exists longer than the Any.
| rhs | Value to assign |
| void * get | ( | ) |
| const void * get | ( | ) | const |
|
A value or reference cast throws std::bad_cast when the stored type is not T. A pointer cast returns a null pointer on mismatch.