Manager of all lexical converters needed for front-end input/output. More...
#include <keyvalue/frontend/LexicalToolKit.h>
Classes | |
class | Failure |
This class is thrown when LexicalToolKit::convert() fails. More... | |
struct | Helper |
Helper class of LexicalToolKit. More... | |
Public Types | |
enum | IO { none = 0x0, input = 0x1, output = 0x2, both = 0x3 } |
Direction of conversion. More... | |
Public Member Functions | |
LexicalToolKit () | |
Default constructor. | |
template<typename From , typename To > | |
void | set (IO io, typename Helper< From, To >::Type_ converter=util::Lexical) |
Sets a converter function pointer. | |
template<typename From , typename To > | |
bool | isEnabled (IO io) const |
Checks if a specific conversion is enabled. | |
template<typename To , typename From > | |
To | convert (From input) const |
Performs a conversion. | |
Private Attributes | |
char | endInitList |
Manager of all lexical converters needed for front-end input/output.
This class
stores pointers to several lexical conversion functions from/to different types. Possible conversions are:
bool
<--> double
; (*) bool
<--> string
; (*) double
<--> ptime
; double
<--> string
; (*) ptime
<--> string
.Those pointers can be changed to customized converters which must have the following signature:
To function(
From)
where From and To are the input and output types, resp.
Some conversions might fail (e.g. from string
"foo" to any other type) in which case the converter should throw an exception.
Conversions marked with (*) have a quite natural meaning (e.g from string
"True" to bool
true
). For such conversions this class
provides default converter (see util::Lexical) that can (if the user wishes so), be chosen (through set()) as the official converter.
LexicalToolKit | ( | ) |
Default constructor.
By default, all conversion function pointers are null.
Sets a converter function pointer.
Parameters From and To specify, resp., the input and output types of the conversion function pointer to be changed.
A lexical conversion may be need for KeyValue's input or output. The parameter io defines if the corresponding conversion is set for input, output or both.
This function takes a pointer, converter, to a conversion function. If no pointer is provided, then the default converter (see util::Lexical) is used. If a null pointer is passed, then the corresponding conversion is disabled.
From | : (template parameter) The input type; | |
To | : (template parameter) The output type; | |
io | : Direction the conversion is set to; | |
converter | : Pointer to the conversion function. |
bool isEnabled | ( | IO | io | ) | const |
Checks if a specific conversion is enabled.
This template
function checks if the From to To conversion is enabled in the io direction.
From | : (template parameter) The input type; | |
To | : (template parameter) The output type; | |
io | : Conversion direction. |
true
if the conversion is enabled. Otherwise it returns false
.LogicError | : (Debug build only) If io is invalid. |
To convert | ( | From | input | ) | const |
Performs a conversion.
Parameters From and To specify, resp., the input and output types of the required conversion. Notice that, in general, compilers are able to deduce From type and, hence it can be omitted. Therefore a typical use for this method looks like
LexicalToolKit lt; double x = 3.141592654; string s = lt.convert<string>(x);
From | : (template parameter) The input type; | |
To | : (template parameter) The output type; | |
input | : Value to be converted. |
LogicError | : (Debug build only) If the required conversion is disabled. | |
Failure | : If required conversion fails. |
char endInitList [private] |
Using the X Macro technique in the constructor initialisation list will produce a list of members ending in a comma. It cannot be the end of the list, otherwise, together with the following constructor's body open brace will get a syntax error. We need at least one extra member following by an space (not comma).