Any Class Reference

#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.
Anyswap (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>
Anyoperator= (const T &rhs)
 Assign value.
template<typename T>
Anyoperator= (T *rhs)
 Assign reference.
Anyoperator= (const Any &rhs)
 Assign value of other Any.
void * get ()
 Get pointer to stored value.
const void * get () const
 Get pointer to stored value.

(Note that these are not member symbols.)

template<typename T>
any_cast (const Any &any)
 Returns the value stored in any as type T.

Detailed Description

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.

Pt::Any a = 5;
int i = Pt::any_cast<int>(a);
float f = Pt::any_cast<float>(a); // throws std::bad_cast
Value of any copyable type.
Definition Any.h:79
T any_cast(const Any &any)
Returns the value stored in any as type T.
Definition Any.h:571

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.

Constructor & Destructor Documentation

◆ Any() [1/5]

template<typename T>
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() [2/5]

template<typename T>
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() [3/5]

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() [4/5]

Any ( )

Constructs an empty any. No memory needs to be allocated for empty Anys.

◆ Any() [5/5]

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()

~Any ( )

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

Member Function Documentation

◆ clear()

void clear ( )

Removes the stored type resulting in a destructor call for the stored type. All memory required to hold the value is deallocated.

◆ empty()

bool empty ( ) const

Returns true if no value has been assigned, false otherwise.

Returns
True if empty

◆ swap()

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

◆ type()

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

◆ operator=() [1/3]

template<typename T>
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

◆ operator=() [2/3]

template<typename T>
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

◆ operator=() [3/3]

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

◆ get() [1/2]

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

◆ get() [2/2]

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

◆ any_cast()

template<typename T>
T any_cast ( const Any & any)
related

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.