Single-value multi-type container. More...
#include <keyvalue/value/Variant.h>
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 > | |
Variant & | operator= (const Rhs &rhs) |
Assignment operator. | |
Variant & | operator= (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. |
Single-value multi-type container.
A Variant is a single-value container for one of the following types:
bool
; double
; ptime
; string
. Variant | ( | const DataType_ & | data | ) |
Builds and sets content.
Takes type on 1-level-down on the value hierarchy.
data | : The content. |
Variant | ( | const Rhs & | rhs | ) |
Builds and sets content.
Takes type on 2-or-more-levels-down on the value hierarchy.
rhs | : The content. |
Builds and sets content.
Takes type on the 1-level-up on the value hierarchy.
data | : The content. |
Variant & operator= | ( | const Rhs & | rhs | ) |
Assignment operator.
Takes type on a 2-or-more-levels-below of the value hierarchy.
rhs | : The data to be assigned. |
this
. 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.
ElementType | : (template parameter) The type to be checked; | |
io | : Direction for required conversion (see frontend::LexicalToolKit). |
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:
double
to ElementType; string
to ElementType; bool
to ElementType; 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.
ElementType | : (template parameter) The target type; | |
io | : Direction of required conversion (see frontend::LexicalToolKit). |
LogicError | : (Debug built only) if content is not convertible to ElementType. | |
frontend::LexicalToolKit::Failure& | : If conversion fails. |
const ElementType* get | ( | ) | const [private] |
::std::ostream& operator<< | ( | ::std::ostream & | os, | |
const Variant & | variant | |||
) | [friend] |
ostream operator<<() for Variant.