Home | Libraries | People | FAQ | More |
trial::dynamic::basic_variable — Dynamic variable.
// In header: <boost/libs/trial.protocol/include/trial/dynamic/variable.hpp> template<template< typename > class Allocator> class basic_variable { public: // types typedef basic_variable< Allocator > value_type; typedef value_type & reference; typedef const value_type & const_reference; typedef unspecified difference_type; // signed integer type typedef unspecified size_type; // unsigned integer type typedef std::basic_string< char, std::char_traits< char >, Allocator > string_type; // std::string by default typedef std::basic_string< wchar_t, std::char_traits< wchar_t >, Allocator > wstring_type; // std::wstring by default typedef std::basic_string< char16_t, std::char_traits< char16_t >, Allocator > u16string_type; // std::u16string by default typedef std::basic_string< char32_t, std::char_traits< char32_t >, Allocator > u32string_type; // std::u32string by default typedef unspecified array_type; // SequenceContainer. typedef unspecified map_type; // AssociativeContainer. typedef unspecified pair_type; typedef std::reverse_iterator< iterator > reverse_iterator; typedef std::reverse_iterator< const_iterator > const_reverse_iterator; // member classes/structs/unions class const_iterator : public iterator_base< const_iterator, const basic_variable::value_type > { public: // construct/copy/destruct const_iterator(); const_iterator(const const_iterator &); const_iterator(const_iterator &&); const_iterator(const iterator &); explicit const_iterator(pointer, bool = true); // public member functions const_reference key() const; const_reference value() const; const_reference operator*() const; }; class iterator : public iterator_base< iterator, basic_variable::value_type > { public: // construct/copy/destruct iterator(); iterator(const iterator &); iterator(iterator &&); explicit iterator(const const_iterator &); explicit iterator(pointer, bool = true); explicit iterator(pointer, typename super::array_iterator); explicit iterator(pointer, typename super::map_iterator); iterator & operator=(const iterator &); iterator & operator=(iterator &&); // public member functions const_reference key() const; reference value(); const_reference value() const; reference operator*(); const_reference operator*() const; }; class key_iterator : public iterator_base< key_iterator, const basic_variable::value_type > { public: // construct/copy/destruct key_iterator(); key_iterator(const key_iterator &); key_iterator(key_iterator &&); explicit key_iterator(pointer, bool = true); key_iterator & operator=(const key_iterator &); key_iterator & operator=(key_iterator &&); // public member functions const_reference key() const; const_reference value() const; const_reference operator*(); key_iterator & operator++(); }; template<typename T, typename = void> struct tag_traits { }; template<typename T> struct traits { }; // construct/copy/destruct basic_variable(); basic_variable(const basic_variable &); basic_variable(basic_variable &&) noexcept; template<typename T> basic_variable(T); basic_variable(const std::initializer_list< value_type > &); basic_variable(const nullable &); basic_variable(const char *); basic_variable(const wchar_t *); basic_variable(const char16_t *); basic_variable(const char32_t *); basic_variable(const typename basic_variable::array_type &); basic_variable(typename basic_variable::array_type &&); basic_variable(const typename basic_variable::map_type &); basic_variable(typename basic_variable::map_type &&); basic_variable & operator=(const basic_variable &); basic_variable & operator=(basic_variable &&); template<typename T> basic_variable & operator=(T); basic_variable & operator=(nullable); basic_variable & operator=(const char *); basic_variable & operator=(const wchar_t *); basic_variable & operator=(const char16_t *); basic_variable & operator=(const char32_t *); // public member functions basic_variable & operator+=(const basic_variable &); template<typename T> basic_variable & operator+=(const T &); basic_variable & operator+=(const char *); basic_variable & operator+=(const wchar_t *); basic_variable & operator+=(const char16_t *); basic_variable & operator+=(const char32_t *); template<typename T> T value() const; template<typename T> T value(std::error_code &) const noexcept; template<typename R> explicit operator R() const; template<typename R> R & assume_value(); template<typename R> const R & assume_value() const; basic_variable & operator[](size_type); const basic_variable & operator[](size_type) const; basic_variable & operator[](const typename map_type::key_type &); const basic_variable & operator[](const typename map_type::key_type &) const; template<typename T> bool is() const noexcept; template<typename T> bool same() const noexcept; dynamic::code::value code() const noexcept; dynamic::symbol::value symbol() const noexcept; bool empty() const noexcept; size_type size() const noexcept; size_type max_size() const noexcept; void clear() noexcept; iterator insert(const basic_variable &); template<typename InputIterator> void insert(InputIterator, InputIterator); iterator insert(const_iterator, const basic_variable &); template<typename InputIterator> void insert(const_iterator, InputIterator, InputIterator); iterator erase(const_iterator); iterator erase(const_iterator, const_iterator); void swap(basic_variable &) noexcept; iterator begin(); const_iterator begin() const; const_iterator cbegin() const; iterator end(); const_iterator end() const; const_iterator cend() const; reverse_iterator rbegin(); const_reverse_iterator rbegin() const; const_reverse_iterator crbegin() const; reverse_iterator rend(); const_reverse_iterator rend() const; const_reverse_iterator crend() const; key_iterator key_begin() const; key_iterator key_end() const; };
Dynamic variable is a tagged union that can change both its type and value during program execution.
Dynamic variable is a heterogenous hierarchical data structure with a pre-determined list of supported types: fundamental data types, strings, arrays, and associative arrays.
Type | Tag |
dynamic::nullable |
dynamic::nullable |
bool |
dynamic::boolean |
signed char |
dynamic::integer |
signed short int |
dynamic::integer |
signed int |
dynamic::integer |
signed long int |
dynamic::integer |
signed long long int |
dynamic::integer |
unsigned char |
dynamic::integer |
unsigned short int |
dynamic::integer |
unsigned int |
dynamic::integer |
unsigned long int |
dynamic::integer |
unsigned long long int |
dynamic::integer |
float |
dynamic::real |
double |
dynamic::real |
long double |
dynamic::real |
dynamic::string_type |
dynamic::string |
dynamic::wstring_type |
dynamic::wstring |
dynamic::u16string_type |
dynamic::u16string |
dynamic::u32string_type |
dynamic::u32string |
dynamic::array_type |
dynamic::array |
dynamic::map_type |
dynamic::map |
template< typename > class Allocator
Allocator type (defaults to std::allocator
)
basic_variable
public
construct/copy/destructbasic_variable();Constructs a default variable of type
nullable
. A default constructed variable contains null
, which means it has no value.
basic_variable(const basic_variable & element);Copy-constructs a copy of
element
. basic_variable(basic_variable && element) noexcept;Move-constructs a copy of
element
. template<typename T> basic_variable(T);Constructs a variable of type
T
. T
must be a supported type, otherwise a compilation error occurs.
basic_variable(const std::initializer_list< value_type > & elements);Constructs a variable from initializer list.
If the initializer list elements
contains an array of pairs then the variable will be of type map
, otherwise of type array
.
basic_variable(const nullable &);Constructs a variable of type
nullable
. basic_variable(const char *);Constructs a variable of type
string
. basic_variable(const wchar_t *);Constructs a variable of type
wstring
. basic_variable(const char16_t *);Constructs a variable of type
u16string
. basic_variable(const char32_t *);Constructs a variable of type
u32string
. basic_variable(const typename basic_variable::array_type &);Construct a variable from array type.
Re-construct a variable from the value returned by assume_value
.
Prefer to use factory or initializer-lists when creating arrays.
basic_variable(typename basic_variable::array_type &&);Construct a variable from array type.
Re-construct a variable from the value returned by assume_value
.
Prefer to use factory or initializer-lists when creating arrays.
basic_variable(const typename basic_variable::map_type &);Construct a variable from map type.
Re-construct a variable from the value returned by assume_value
.
Prefer to use factory or initializer-lists when creating associative arrays.
basic_variable(typename basic_variable::map_type &&);Construct a variable from map type.
Re-construct a variable from the value returned by assume_value
.
Prefer to use factory or initializer-lists when creating associative arrays.
basic_variable & operator=(const basic_variable &);Copy-asigns one variable to another.
basic_variable & operator=(basic_variable &&);Move-assigns one variable to another.
template<typename T> basic_variable & operator=(T);Assigns content to a variable.
basic_variable & operator=(nullable);Assigns null to variable.
basic_variable & operator=(const char *);Assigns a string literal to a variable.
basic_variable & operator=(const wchar_t *);Assigns a wide-string literal to a variable.
basic_variable & operator=(const char16_t *);Assigns a UTF-16 encoded string literal to a variable.
basic_variable & operator=(const char32_t *);Assigns a UTF-32 encoded string literal to a variable.
basic_variable
public member functionsbasic_variable & operator+=(const basic_variable &);Appends values.
The append operator has different meanings depending on the types.
Appending null to any other type has no effect.
Appending any value to null overwrites the variable.
Appending an integral value with another integral value adds the two.
Appending a string to another string concatenates the two.
Appending a wide string to another wide string concatenates the two.
Appending a UTF-16 encoded string to another UTF-16 encoded string concatenates the two.
Appending a UTF-32 encoded string to another UTF-32 encoded string concatenates the two.
Appending an array to an array concatenates the two.
Appending any type to an array inserts the value at the end of the array.
Appending a map to a map merges the two.
Tag | nullable | boolean | integer | real | string | wstring | u16string |
u32string |
array | map |
nullable | No effect | Assigns | Assigns | Assigns | Assigns | Assigns | Assigns | Assigns | Assigns | Assigns |
boolean | No effect | Adds | Adds | Adds | Fails | Fails | Fails | Fails | Fails | Fails |
integer | No effect | Adds | Adds | Adds | Fails | Fails | Fails | Fails | Fails | Fails |
real | No effect | Adds | Adds | Adds | Fails | Fails | Fails | Fails | Fails | Fails |
string | No effect | Fails | Fails | Fails | Concats | Fails | Fails | Fails | Fails | Fails |
wstring | No effect | Fails | Fails | Fails | Fails | Concats | Fails | Fails | Fails | Fails |
u16string |
No effect | Fails | Fails | Fails | Fails | Fails | Concats | Fails | Fails | Fails |
u32string |
No effect | Fails | Fails | Fails | Fails | Fails | Fails | Concats | Fails | Fails |
array | No effect | Inserts | Inserts | Inserts | Inserts | Inserts | Inserts | Inserts | Concats | Inserts |
map | No effect | Fails | Fails | Fails | Fails | Fails | Fails | Fails | Fails | Merges |
template<typename T> basic_variable & operator+=(const T &);
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
basic_variable & operator+=(const char *);
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
basic_variable & operator+=(const wchar_t *);
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
basic_variable & operator+=(const char16_t *);
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
basic_variable & operator+=(const char32_t *);
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template<typename T> T value() const;Returns current value as type T.
Calls
and throws an exception if an error occurred.basic_variable
<Allocator>::value<T>(std::error_code&)
See Also:
basic_variable<Allocator>::value<T>(std::error_code&)
Template Parameters: |
|
||
Throws: |
dynamic::error with dynamic::incompatible_type if T is an unsupported or incompatible type. |
template<typename T> T value(std::error_code & error) const noexcept;Returns current value as type T.
T
can either be a type or a tag.
If current tag is
or dynamic::integer
, then dynamic::real
T
can be any arithmetic type. Such a conversion may result in loss of precision.
If current tag is any other type, then T
must be the associated type.
If T
is a tag, then the following types are used.
Tag | Type |
nullable |
dynamic::nullable |
boolean |
bool |
integer |
int |
real |
float |
string |
variable::string_type |
wstring |
variable::wstring_type |
u16string |
variable::u16string_type |
u32string |
variable::u32string_type |
array |
variable::array_type |
map |
variable::map_type |
Parameters: |
|
||
Template Parameters: |
|
template<typename R> explicit operator R() const;Returns current value as type
R
. Calls basic_variable
<Allocator>::value<R>()
See Also:
basic_variable<Allocator>::value<T>()
template<typename R> R & assume_value();Returns reference to stored value.
assume_value()
has a narrow contract. Using a type R
that does not match the type of the stored value results in undefined behavior.
Template Parameters: |
|
||
Requires: |
basic_variable<Allocator>::same<R>() is true. |
template<typename R> const R & assume_value() const;Returns constant reference to stored value.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
basic_variable & operator[](size_type position);Returns reference to element at specified position.
The stored value must be an array.
No bounds-checking is performed.
Parameters: |
|
||
Requires: |
basic_variable<Allocator>::is<array>() is true. |
const basic_variable & operator[](size_type position) const;Returns constant reference to element at specified position.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
basic_variable & operator[](const typename map_type::key_type & key);Returns reference to element indexed by key.
Returns a reference to the value associated with key
in an associative array, or inserts a key of none exists.
If the current tag is nullable
, then the current value is changed into an associative array and key
is inserted.
Requires: |
basic_variable<Allocator>::is<map>() is true. |
Throws: |
dynamic::error with dynamic::incompatible_type is thrown if current tag is not dynamic::map . |
const basic_variable & operator[](const typename map_type::key_type & key) const;Returns constant reference to element indexed by key.
Returns a reference to the value associated with key
in an associative array.
Requires: |
basic_variable<Allocator>::is<map>() is true. |
Throws: |
dynamic::error with dynamic::incompatible_type is thrown if key does not exist in associative array. with dynamic::incompatible_type is thrown if current tag is not dynamic::map . |
template<typename T> bool is() const noexcept;Checks if variable has a given tag.
Converts T
into a tag, and returns true if variable has the same tag, otherwise returns false.
If T
is a tag, then this tag is used directly: variable v = true; // Boolean
assert(v.is<boolean>());
If T
is a C++ type, then this is converted a tag: variable v = true; // Boolean
assert(v.is<bool>()); // Same as is<boolean>()
The type-to-tag conversion means that any type from the same category can be used: variable v = 2; // Integer assert(v.is<int>()); // Same as is<integer>() assert(v.is<long int>()); // Same as is<integer>()
Checking for an array or a map can either be done by a tag: variable v = { 1, 2, 3}; // Array assert(v.is<array>());
or by the unspecified variable::array_type
or variable::map_type
type aliases: variable v = { 1, 2, 3}; // Array
assert(v.is<variable::array_type>());
Using unsupported types will result in a compilation error.
Template Parameters: |
|
template<typename T> bool same() const noexcept;Checks if variable has exactly a given type.
Returns true if variable has the exact same type as T
, otherwise returns false. variable v = int(2); // int
assert(v.same<int>() == true); // Same as int
assert(v.same<long int>() == false); // Not same as long int
Template Parameters: |
|
dynamic::code::value code() const noexcept;Returns the current code.
Code is an enumerator that represents the exact type that is currently stored.
Type | Code |
dynamic::nullable |
dynamic::code::null |
bool |
dynamic::code::boolean |
signed char |
dynamic::code::signed_char |
signed short int |
dynamic::code::signed_short_integer |
signed int |
dynamic::code::signed_integer |
signed long int |
dynamic::code::signed_long_integer |
signed long long int |
dynamic::code::signed_long_long_integer |
unsigned char |
dynamic::code::unsigned_char |
unsigned short int |
dynamic::code::unsigned_short_integer |
unsigned int |
dynamic::code::unsigned_integer |
unsigned long int |
dynamic::code::unsigned_long_integer |
unsigned long long int |
dynamic::code::unsigned_long_long_integer |
float |
dynamic::code::real |
double |
dynamic::code::long_real |
long double |
dynamic::code::long_long_real |
dynamic::string_type |
dynamic::code::string |
dynamic::wstring_type |
dynamic::code::wstring |
dynamic::u16string_type |
dynamic::code::u16string |
dynamic::u32string_type |
dynamic::code::u32string |
dynamic::array_type |
dynamic::code::array |
dynamic::map_type |
dynamic::code::map |
dynamic::symbol::value symbol() const noexcept;Returns the current symbol.
Symbol is an enumerator that represents the tag of the type currently stored.
Type | Tag | Symbol |
dynamic::nullable |
nullable |
dynamic::symbol::null |
bool |
boolean |
dynamic::symbol::boolean |
signed char |
integer |
dynamic::symbol::integer |
signed short int |
integer |
dynamic::symbol::integer |
signed int |
integer |
dynamic::symbol::integer |
signed long int |
integer |
dynamic::symbol::integer |
signed long long int |
integer |
dynamic::symbol::integer |
unsigned char |
integer |
dynamic::symbol::integer |
unsigned short int |
integer |
dynamic::symbol::integer |
unsigned int |
integer |
dynamic::symbol::integer |
unsigned long int |
integer |
dynamic::symbol::integer |
unsigned long long int |
integer |
dynamic::symbol::integer |
float |
real |
dynamic::symbol::real |
double |
real |
dynamic::symbol::real |
long double |
real |
dynamic::symbol::real |
dynamic::string_type |
string |
dynamic::symbol::string |
dynamic::wstring_type |
wstring |
dynamic::symbol::wstring |
dynamic::u16string_type |
u16string |
dynamic::symbol::u16string |
dynamic::u32string_type |
u32string |
dynamic::symbol::u32string |
dynamic::array_type |
array |
dynamic::symbol::array |
dynamic::map_type |
map |
dynamic::symbol::map |
bool empty() const noexcept;Checks if variable is empty.
Same as size()
== 0
size_type size() const noexcept;Returns the number of elements in variable.
The number of elements stored in a variable corresponds to the iteration count, that is v.size() == std::distance(v.begin(), v.end())
.
Notice that strings are regarded as single elements, rather than a sequence of characters.
Current tag | Size | Description |
nullable | 0 | null is an empty variable. |
boolean | 1 | boolean is a single element. |
integer | 1 | integer is a single element. |
real | 1 | real is a single element. |
string | 1 | string is a single element, so variable::string_type::size() is not used. |
wstring | 1 | wstring is a single element, so variable::wstring_type::size() is not used. |
u16string |
1 |
u16string is a single element, so variable::u16string_type::size() is not used. |
u32string |
1 |
u32string is a single element, so variable::u32string_type::size() is not used. |
array |
variable::array_type::size() |
The number of elements in the array. |
map |
variable::map_type::size() |
The number of elements (pairs) in the map. |
size_type max_size() const noexcept;Returns the maximum possible number of elements.
Current tag | Size | Description |
nullable | 0 | null cannot contain element. |
boolean | 1 | Can contain a single boolean element. |
integer | 1 | Can contain a single integer element. |
real | 1 | Can contain a single real element. |
string | 1 | Can contain a single string element, so variable::string_type::max_size() is not used. |
wstring | 1 | Can contain a single wstring element, so variable::wstring_type::max_size() is not used. |
u16string |
1 | Can contain a single u16string element, so variable::u16string_type::max_size() is not used. |
u32string |
1 | Can contain a single u32string element, so variable::u32string_type::max_size() is not used. |
array |
variable::array_type::max_size() |
The maximum possible number of elements in the array. |
map |
variable::map_type::max_size() |
The maximum possible number of elements (pairs) in the map. |
void clear() noexcept;Clears the content of the variable.
Only the value is cleared. The type is not modified.
Current tag | Behavior |
nullable | No effect. |
boolean | Sets the default constructed value. |
integer | Sets the default constructed value. |
real | Sets the default constructed value. |
string | Calls variable::string_type::clear() . |
wstring | Calls variable::wstring_type::clear() . |
u16string |
Calls variable::u16string_type::clear() . |
u32string |
Calls variable::u32string_type::clear() . |
array | Calls variable::array_type::clear() . |
map | Calls variable::map_type::clear() . |
iterator insert(const basic_variable & element);Inserts element into variable.
Current tag | Behavior |
nullable | Create array and insert element . |
boolean | Fails. |
integer | Fails. |
real | Fails. |
string | Fails. |
wstring | Fails. |
u16string |
Fails. |
u32string |
Fails. |
array | Insert element at end. |
map | Insert element , otherwise fails if element is not pair. |
template<typename InputIterator> void insert(InputIterator begin, InputIterator end);Inserts range into variable.
Current tag | Behavior |
nullable | Create array and insert range. |
boolean | Fails. |
integer | Fails. |
real | Fails. |
string | Fails. |
wstring | Fails. |
u16string |
Fails. |
u32string |
Fails. |
array | Insert range at end. |
map | Insert range, otherwise fails if any element in range is not pair. |
iterator insert(const_iterator position, const basic_variable & element);Inserts element into variable.
Current tag | Behavior |
nullable | Fails. |
boolean | Fails. |
integer | Fails. |
real | Fails. |
string | Fails. |
wstring | Fails. |
u16string |
Fails. |
u32string |
Fails. |
array | Insert element before position . |
map | Insert element with position as hint, otherwise fails if element is not pair. |
template<typename InputIterator> void insert(const_iterator, InputIterator begin, InputIterator end);Inserts range into variable.
Current tag | Behavior |
nullable | Fails. |
boolean | Fails. |
integer | Fails. |
real | Fails. |
string | Fails. |
wstring | Fails. |
u16string |
Fails. |
u32string |
Fails. |
array | Insert range before position . |
map | Insert range with position as hint, otherwise fails if any element in range is not pair. |
iterator erase(const_iterator position);Erases an element from variable.
Returns iterator following last erased element.
Current tag | Behavior |
nullable | No effect. |
boolean | No effect. |
integer | No effect. |
real | No effect. |
string | No effect. |
wstring | No effect. |
u16string |
No effect. |
u32string |
No effect. |
array | Erases element at position . |
map | Erases element at position . |
iterator erase(const_iterator begin, const_iterator end);Erases range from variable.
Returns iterator following last erased element.
Current tag | Behavior |
nullable | No effect. |
boolean | No effect. |
integer | No effect. |
real | No effect. |
string | No effect. |
wstring | No effect. |
u16string |
No effect. |
u32string |
No effect. |
array | Erases elements in range. |
map | Erases elements in range. |
void swap(basic_variable & other) noexcept;Exchange contents of variable and
other
. iterator begin();Returns an iterator to the beginning.
const_iterator begin() const;Returns an iterator to the beginning.
const_iterator cbegin() const;Returns an iterator to the beginning.
iterator end();Returns an iterator to the end.
const_iterator end() const;Returns an iterator to the end.
const_iterator cend() const;Returns an iterator to the end.
reverse_iterator rbegin();Returns a reverse iterator to the beginning.
const_reverse_iterator rbegin() const;Returns a reverse iterator to the beginning.
const_reverse_iterator crbegin() const;Returns a reverse iterator to the beginning.
reverse_iterator rend();Returns a reverse iterator to the end.
const_reverse_iterator rend() const;Returns a reverse iterator to the end.
const_reverse_iterator crend() const;Returns a reverse iterator to the end.
key_iterator key_begin() const;Returns a key iterator to the beginning.
key_iterator key_end() const;Returns a key iterator to the end.