Public Types | Public Member Functions | Private Member Functions | Private Attributes | Friends

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)
 Assignment operator.
Variantoperator= (const Single &rhs)
 Assignment 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

friend::std::ostream & operator<< (::std::ostream &os, const Variant &variant)
 ostream operator<<() for 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 1-level-down on the value hierarchy.

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

Builds and sets content.

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

Parameters:
rhs : The content.
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  ) 

Assignment 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.
Variant& operator= ( const Single rhs  ) 

Assignment operator.

Assigns to the content of a Single object.

Parameters:
data : The content.
bool is ( frontend::LexicalToolKit::IO  io  )  const

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

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 behaviour 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 [private]

Gets content.

This private low-level method 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.

Friends And Related Function Documentation

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

ostream operator<<() for Variant.