Any Class Reference

#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 >
Anyoperator= (const T &rhs)
 Assign value. More...
 
template<typename T >
Anyoperator= (T *rhs)
 Assign reference. More...
 
Anyoperator= (const Any &rhs)
 Assign value of other Any. More...
 
Anyswap (Any &other)
 Swap values. More...
 
const std::type_info & type () const
 Returns type info of assigned type. More...
 

Related Functions

template<typename T >
any_cast (const Any &any)
 Get contained value. More...
 

Detailed Description

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.

#include <Pt/Any.h>
Pt::Any a = 5;
int i = any_cast<int>( a ); // i is 5 now
float f = any_cast<float>( a ) // throws std::bad_cast

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.

Constructor & Destructor Documentation

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.

Parameters
typeValue to assign
Any ( T *  type)
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.

Parameters
typeValue 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)

Constructs the Any by copying the value of the other Any. It is legal to assign an empty Any. If an exception is thrown during construction, the Any will be empty and the exception is porpagated.

Parameters
valAny to assign
~Any ( )

Deallocates the memory needed to hold the value. This will also destruct the contained type.

Member Function Documentation

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.

Returns
True if empty
Any & swap ( Any other)

The member function swaps the assigned values between *this and right. No exceptions are thrown, and no memory needs to be allocated.

Parameters
otherOther any to swap value
Returns
self reference
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.

Returns
Type info
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.

Parameters
rhsValue 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.

Parameters
rhsValue to assign
Any & operator= ( const Any rhs)

Assignes the value of another Any by copying the value of the other Any. It is legal to assign an empty Any. If an exception is thrown during assignment, the Any will remain unchanged and the exception is porpagated.

Parameters
rhsAny to assign
void* get ( )

Returns a pointer to the stored value or 0 if the Any is empty. Use Any::type to find out which type is stored in the &Any.

Returns
Pointer to stored value or 0 if empty
const void* get ( ) const

Returns a pointer to the stored value or 0 if the Any is empty. Use Any::type to find out which type is stored in the &Any.

Returns
Pointer to stored value or 0 if empty

Friends And Related Function Documentation

T any_cast ( const Any any)
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.

Returns
contained value
Exceptions
std::bad_caston type mismatch