Variant Class Reference

Single-value multi-type container. More...

#include "keyvalue/value/Variant.h"

List of all members.

Public Types

typedef boost::variant
< Nothing, bool, double, ptime,
string > 
DataType_

Public Member Functions

 Variant ()
 Default constructor sets content to Nothing.
 Variant (const DataType_ &data)
 Builds and sets content.
template<typename Rhs >
 Variant (const Rhs &rhs)
 Builds and sets content.
 Variant (const Single &data)
 Builds and sets content.
template<typename Rhs >
Variantoperator= (const Rhs &rhs)
 Assingment operator.
template<typename ElementType >
bool is (frontend::LexicalToolKit::IO io) const
 Checks if content is convertible to ElementType.
template<typename ElementType >
ElementType get (frontend::LexicalToolKit::IO io) const
 Gets content converted ElementType.

Private Member Functions

template<typename ElementType >
const ElementType * get () const
 Gets content.

Private Attributes

DataType_ data_

Friends

std::ostream & operator<< (std::ostream &os, const Variant &variant)

Detailed Description

Single-value multi-type container.

A Variant is a single-value container for one of the following types:


Constructor & Destructor Documentation

Variant (  ) 

Default constructor sets content to Nothing.

Variant ( const DataType_ &  data  ) 

Builds and sets content.

Takes type on the 1-level-down on the value hierarchy.

Parameters:
data : The content.
Variant ( const Rhs &  rhs  )  [inline]

Builds and sets content.

Takes type on a 2-or-more-levels-down on the value hierarchy.

Parameters:
rhs : The content.

If you get an error here about 'keyvaluevalue::Parent<Rhs>', this means that Rhs isn't in a lower level in the hierarchy.

Variant ( const Single data  ) 

Builds and sets content.

Takes type on the 1-level-up on the value hierarchy.

Parameters:
data : The content.

Member Function Documentation

Variant & operator= ( const Rhs &  rhs  )  [inline]

Assingment operator.

Takes type on a 2-or-more-levels-below of the value hierarchy.

Parameters:
rhs : The data to be assigned.
Returns:
A reference to this.

If you get an error here about 'keyvaluevalue::Parent<P>', this means that Rhs isn't in a lower level in the hierarchy.

bool is ( frontend::LexicalToolKit::IO  io  )  const [inline]

Checks if content is convertible to ElementType.

If ElementType and content's type are the same, then this method returns true. If not, this method also considers lexical conversions (see documentation of get()) but only partially: Indeed, it returns true if the current frontend::FrontEnd enables lexical conversion from content's type to ElementType. However, it does not guarantee that the conversion will succeed when performed. For instance, suppose *this stores the string "foo". Assume also ElementType is double and conversion from string to double is enabled by frontend::FrontEnd. Then this method will return true but the method get<double>() will (probably) fail because "foo" is not convertible to a double (nevertheless, remind that what happens depends on the lexical converter which can do whatever it wants!)

ElementType can be any Variant allowed type. Additionally, this method allows for ElementType to be unsigned int. In this case, it checks if content type is a double whose value is a positive integer.

See get() for more details on lexical conversions.

Parameters:
ElementType : (template parameter) the type to be checked;
io : Direction for required conversion (see frontend::LexicalToolKit).
Returns:
If content's type is convertible to ElementType, then this method returns true. Otherwise, it returns false.
ElementType get ( frontend::LexicalToolKit::IO  io  )  const [inline]

Gets content converted ElementType.

Debug build firstly checks whether content is convertible to ElementType (through is<ElementType>()). If not, an exception is thrown. Release build does not check and has undefined behavior if content is not convertible to ElementType.

Conversion follows these steps: First it checks if content's type and ElementType coincide, in which case, it returns a copy of content. Otherwise, it calls enabled and meaningful frontend::FrontEnd lexical converter functions (see frontend::FrontEnd and frontend::LexicalToolKit) in the following order:

  • from double to ElementType;
  • from string to ElementType;
  • from bool to ElementType;
  • from ptime to ElementType.

ElementType can be any Variant allowed type. Additionally, this method allows for the special ElementType unsigned int. In this case, it checks if content type is a double whose value is a positive integer.

Parameters:
ElementType : (template parameter) the target type;
io : Direction of required conversion (see frontend::LexicalToolKit).
Returns:
The converted content if conversion succeeds.
Exceptions:
LogicError : (Debug built only) if content is not convertible to ElementType.
frontend::LexicalToolKit::Failure& : if conversion fails.
const ElementType* get (  )  const [inline, private]

Gets content.

This private low-level method which is a helper to public methods is() and get(). No lexical conversions are considered.

Parameters:
ElementType : (template parameter) expected content's type.
Returns:
A const pointer to the content, if it is of type ElementType. Otherwise, a null pointer.

Generated on Sat Mar 20 15:08:31 2010 for KeyValue by  doxygen 1.6.1