CppMicroServices

C++ Micro Services: Any Class Reference
Any Class Reference

An Any class represents a general type and is capable of storing any type, supporting type-safe extraction of the internally stored data. More...

Public Member Functions

 Any ()
 Creates an empty any type. More...
 
template<typename ValueType >
 Any (const ValueType &value)
 Creates an Any which stores the init parameter inside. More...
 
 Any (const Any &other)
 Copy constructor, works with empty Anys and initialized Any values. More...
 
 ~Any ()
 
AnySwap (Any &rhs)
 Swaps the content of the two Anys. More...
 
template<typename ValueType >
Anyoperator= (const ValueType &rhs)
 Assignment operator for all types != Any. More...
 
Anyoperator= (const Any &rhs)
 Assignment operator for Any. More...
 
bool Empty () const
 returns true if the Any is empty More...
 
std::string ToString () const
 Returns a string representation for the content. More...
 
const std::type_info & Type () const
 Returns the type information of the stored content. More...
 

Friends

template<typename ValueType >
ValueType * any_cast (Any *)
 any_cast operator used to extract the ValueType from an Any*. More...
 
template<typename ValueType >
ValueType * unsafe_any_cast (Any *)
 

Detailed Description

An Any class represents a general type and is capable of storing any type, supporting type-safe extraction of the internally stored data.

Code taken from the Boost 1.46.1 library. Original copyright by Kevlin Henney. Modified for CppMicroServices.

Constructor & Destructor Documentation

Any::Any ( )
inline

Creates an empty any type.

Referenced by operator=().

template<typename ValueType >
Any::Any ( const ValueType &  value)
inline

Creates an Any which stores the init parameter inside.

Parameters
valueThe content of the Any

Example:

* Any a(13);
* Any a(string("12345"));
*
Any::Any ( const Any other)
inline

Copy constructor, works with empty Anys and initialized Any values.

Parameters
otherThe Any to copy
Any::~Any ( )
inline

Member Function Documentation

bool Any::Empty ( ) const
inline

returns true if the Any is empty

template<typename ValueType >
Any& Any::operator= ( const ValueType &  rhs)
inline

Assignment operator for all types != Any.

Parameters
rhsThe value which should be assigned to this Any.

Example:

* Any a = 13;
* Any a = string("12345");
*

References Any().

Any& Any::operator= ( const Any rhs)
inline

Assignment operator for Any.

Parameters
rhsThe Any which should be assigned to this Any.

References Any().

Any& Any::Swap ( Any rhs)
inline

Swaps the content of the two Anys.

Parameters
rhsThe Any to swap this Any with.
std::string Any::ToString ( ) const
inline

Returns a string representation for the content.

Custom types should either provide a std::ostream& operator<<(std::ostream& os, const CustomType& ct) function or specialize the any_value_to_string template function for meaningful output.

const std::type_info& Any::Type ( ) const
inline

Returns the type information of the stored content.

If the Any is empty typeid(void) is returned. It is suggested to always query an Any for its type info before trying to extract data via an any_cast/ref_any_cast.

Friends And Related Function Documentation

template<typename ValueType >
ValueType* any_cast ( Any operand)
friend

any_cast operator used to extract the ValueType from an Any*.

Will return a pointer to the stored value.

Example Usage:

* MyType* pTmp = any_cast<MyType*>(pAny)
*

Will return NULL if the cast fails, i.e. types don't match.

template<typename ValueType >
ValueType* unsafe_any_cast ( Any operand)
friend