Lunchbox  1.15.0
Multi-threaded C++ toolbox library for all application developers creating high-performance multi-threaded programs.
lunchbox::Any Class Reference

A class which can hold instances of any type. More...

#include <any.h>

+ Collaboration diagram for lunchbox::Any:

Public Member Functions

Construction/Destruction
 Any ()
 Construct a new, empty Any. More...
 
template<typename ValueType >
 Any (const ValueType &value)
 Construct a new Any with the given value. More...
 
 Any (const Any &other)
 Copy-construct a new Any with copying content of other. More...
 
 ~Any ()
 Destruct this Any. More...
 
Modifiers
Anyswap (Any &rhs)
 Exchange the content of this and rhs. More...
 
template<typename ValueType >
Anyoperator= (const ValueType &rhs)
 Assign a new value to this Any. More...
 
Anyoperator= (Any rhs)
 Exchange the content of this and rhs. More...
 
Queries
bool empty () const
 
const std::type_info & type () const
 
bool operator== (const Any &rhs) const
 
bool operator!= (const Any &rhs) const
 

Friends

class boost::serialization::access
 
template<typename ValueType >
ValueType * any_cast (Any *)
 Retrieve the value stored in an Any including type checking. More...
 
template<typename ValueType >
ValueType * unsafe_any_cast (Any *)
 Retrieve the value stored in an Any without type checking. More...
 

Detailed Description

A class which can hold instances of any type.

This class is based on boost.any with the extension to support serialization if the ValueType supports boost.serialization.

Example:

/* Copyright (c) 2012, Daniel Nachbaur <danielnachbaur@gmail.com>
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 2.1 as published
* by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <lunchbox/test.h>
#include <lunchbox/any.h>
int main( int, char** )
{
lunchbox::Any otherAny;
TEST( any.empty( ));
TEST( any.type() == typeid(void));
TEST( any == otherAny );
any = 5;
otherAny = any;
TEST( lunchbox::any_cast< int >( any ) == 5 );
TEST( any.type() == typeid(int));
TEST( any == otherAny );
any = 42;
otherAny = 42;
TEST( lunchbox::any_cast< int >( any ) == 42 );
TEST( any == otherAny );
any = std::string( "blablub" );
TEST( lunchbox::any_cast< std::string >( any ) == "blablub" );
TEST( any.type() == typeid(std::string));
TEST( any != otherAny );
try { TEST( lunchbox::any_cast< int >( any ) != 42 ); }
catch( const lunchbox::bad_any_cast& ) {}
return EXIT_SUCCESS;
}

Definition at line 67 of file any.h.

Constructor & Destructor Documentation

lunchbox::Any::Any ( )

Construct a new, empty Any.

Version
1.5.0

Referenced by Any(), and operator=().

+ Here is the caller graph for this function:

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

Construct a new Any with the given value.

Version
1.5.0

Definition at line 77 of file any.h.

References Any(), swap(), and ~Any().

+ Here is the call graph for this function:

lunchbox::Any::Any ( const Any other)

Copy-construct a new Any with copying content of other.

Version
1.5.0
lunchbox::Any::~Any ( )

Destruct this Any.

Version
1.5.0

Referenced by Any().

+ Here is the caller graph for this function:

Member Function Documentation

bool lunchbox::Any::empty ( ) const
Returns
true if this Any is not holding a value.
Version
1.5.0

Referenced by operator=().

+ Here is the caller graph for this function:

bool lunchbox::Any::operator!= ( const Any rhs) const
inline
Returns
true if the value from this and rhs are not equal.
Version
1.5.0

Definition at line 127 of file any.h.

References any_cast, operator=(), operator==(), type(), and unsafe_any_cast.

+ Here is the call graph for this function:

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

Assign a new value to this Any.

Version
1.5.0

Definition at line 95 of file any.h.

References Any(), empty(), operator==(), and type().

Referenced by operator!=().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Any& lunchbox::Any::operator= ( Any  rhs)

Exchange the content of this and rhs.

Version
1.5.0
bool lunchbox::Any::operator== ( const Any rhs) const
Returns
true if this and rhs are empty or if their values are equal.
Version
1.5.0

Referenced by operator!=(), and operator=().

+ Here is the caller graph for this function:

Any& lunchbox::Any::swap ( Any rhs)

Exchange the content of this and rhs.

Version
1.5.0

Referenced by Any().

+ Here is the caller graph for this function:

const std::type_info& lunchbox::Any::type ( ) const
Returns
the typeid of the contained value if non-empty, otherwise typeid(void).
Version
1.5.0

Referenced by lunchbox::any_cast(), operator!=(), and operator=().

+ Here is the caller graph for this function:

Friends And Related Function Documentation

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

Retrieve the value stored in an Any including type checking.

Returns
the value stored in the given Any, 0 if types are not matching
Version
1.5.0

Definition at line 235 of file any.h.

Referenced by lunchbox::any_cast(), and operator!=().

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

Retrieve the value stored in an Any without type checking.

Returns
the value stored in the given Any
Version
1.5.0

Definition at line 301 of file any.h.

Referenced by operator!=(), and lunchbox::unsafe_any_cast().


The documentation for this class was generated from the following file: