#include <Pt/Any.h>
Contains an arbitrary type. More...
Public Member Functions | |
template<typename T > | |
Any (const T &type) | |
Construct with value. More... | |
template<typename T > | |
Any (T *type) | |
Construct with reference. More... | |
Any (void *type, const std::type_info &ti) | |
Construct with reference. More... | |
Any () | |
Default constructor. More... | |
Any (const Any &val) | |
Copy constructor. More... | |
~Any () | |
Destructor. More... | |
void | clear () |
Clear content. More... | |
bool | empty () const |
Check if empty. More... | |
void * | get () |
Get pointer to stored value. More... | |
const void * | get () const |
Get pointer to stored value. More... | |
bool | isRef () const |
Returns true if Any contains a weak reference. | |
template<typename T > | |
Any & | operator= (const T &rhs) |
Assign value. More... | |
template<typename T > | |
Any & | operator= (T *rhs) |
Assign reference. More... | |
Any & | operator= (const Any &rhs) |
Assign value of other Any. More... | |
Any & | swap (Any &other) |
Swap values. More... | |
const std::type_info & | type () const |
Returns type info of assigned type. More... | |
Related Functions | |
template<typename T > | |
T | any_cast (const Any &any) |
Get contained value. More... | |
Pt::Any can contain any other type, which is default- and copy constructible. When a value is assigned to an Any, a copy is made, just like when a type is inserted in a standard C++ container. Anys can be assigned to another Any, which will also copy the contained value. The contained type can be accessed via any_cast(). It is only possible to get the contained value, if the type matches.
The any_cast() to value and reference types will throw a std::bad_cast exception, if the type is not correct. Using a pointer type as the template argument for any_cast() will not throw an exception, but return a null pointer if the requested type does not match.
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 | ( | ) |
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 |
|
related |
This function is used to get the contained value from an Any. It is not possible to get a float out of an Any, if the contained value is an int, but the typeid's must match. It is, however, possible to get a const reference to the contained type.
std::bad_cast | on type mismatch |