Variant< _num, _TypeMap > Class Template Reference


Detailed Description

template<int _num, typename _TypeMap>
class lemon::Variant< _num, _TypeMap >

Simple Variant type. The Variant type is a type safe union. The C++ has strong limitations for using unions, by example we cannot store type with non default constructor or destructor in a union. This class always knowns the current state of the variant and it cares for the proper construction and destruction.

Parameters:
_num The number of the types which can be stored in the variant type.
_TypeMap This class describes the types of the Variant. The _TypeMap::Map<index>::Type should be a valid type for each index in the range {0, 1, ..., _num - 1}. The VariantTypeMap is helper class to define such type mappings up to 10 types.
And the usage of the class:
      typedef Variant<3, VariantTypeMap<int, std::string, double> > MyVariant;
      MyVariant var;
      var.set<0>(12);
      std::cout << var.get<0>() << std::endl;
      var.set<1>("alpha");
      std::cout << var.get<1>() << std::endl;
      var.set<2>(0.75);
      std::cout << var.get<2>() << std::endl;

The result of course:

      12
      alpha
      0.75
#include <lemon/bits/variant.h>

List of all members.

Public Member Functions

 Variant ()
 Constructor.
 Variant (const Variant &variant)
 Copy constructor.
Variantoperator= (const Variant &variant)
 Assign operator.
 ~Variant ()
 Destrcutor.
template<int _idx>
Variantset ()
 Set to the default value of the type with _idx index.
template<int _idx>
Variantset (const typename _TypeMap::template Map< _idx >::Type &init)
 Set to the given value of the type with _idx index.
template<int _idx>
const TypeMap::template Map
< _idx >::Type & 
get () const
template<int _idx>
_TypeMap::template Map< _idx >
::Type & 
get ()
int state () const


Constructor & Destructor Documentation

Variant (  )  [inline]

This constructor initalizes to the default value of the type with 0 index.

Variant ( const Variant< _num, _TypeMap > &  variant  )  [inline]

Copy constructor

~Variant (  )  [inline]

Destructor


Member Function Documentation

Variant& operator= ( const Variant< _num, _TypeMap > &  variant  )  [inline]

Assign operator

Variant& set (  )  [inline]

This function sets the variant to the default value of the type with _idx index.

Variant& set ( const typename _TypeMap::template Map< _idx >::Type &  init  )  [inline]

This function sets the variant to the given value of the type with _idx index.

const TypeMap::template Map<_idx>::Type& get (  )  const [inline]

Gets the current value of the type with _idx index.

_TypeMap::template Map<_idx>::Type& get (  )  [inline]

Gets the current value of the type with _idx index.

int state (  )  const [inline]

Returns the current state of the variant.


Generated on Thu Jun 4 04:06:43 2009 for LEMON by  doxygen 1.5.9