Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Class template basic_variable

trial::dynamic::basic_variable — Dynamic variable.

Synopsis

// 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;
};

Description

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 Parameters

  1. template< typename > class Allocator

    Allocator type (defaults to std::allocator)

basic_variable public construct/copy/destruct

  1. basic_variable();
    Constructs a default variable of type nullable.

    A default constructed variable contains null, which means it has no value.

  2. basic_variable(const basic_variable & element);
    Copy-constructs a copy of element.
  3. basic_variable(basic_variable && element) noexcept;
    Move-constructs a copy of element.
  4. template<typename T> basic_variable(T);
    Constructs a variable of type T.

    T must be a supported type, otherwise a compilation error occurs.

  5. 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.

  6. basic_variable(const nullable &);
    Constructs a variable of type nullable.
  7. basic_variable(const char *);
    Constructs a variable of type string.
  8. basic_variable(const wchar_t *);
    Constructs a variable of type wstring.
  9. basic_variable(const char16_t *);
    Constructs a variable of type u16string.
  10. basic_variable(const char32_t *);
    Constructs a variable of type u32string.
  11. 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.

  12. 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.

  13. 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.

  14. 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.

  15. basic_variable & operator=(const basic_variable &);
    Copy-asigns one variable to another.
  16. basic_variable & operator=(basic_variable &&);
    Move-assigns one variable to another.
  17. template<typename T> basic_variable & operator=(T);
    Assigns content to a variable.
  18. basic_variable & operator=(nullable);
    Assigns null to variable.
  19. basic_variable & operator=(const char *);
    Assigns a string literal to a variable.
  20. basic_variable & operator=(const wchar_t *);
    Assigns a wide-string literal to a variable.
  21. basic_variable & operator=(const char16_t *);
    Assigns a UTF-16 encoded string literal to a variable.
  22. basic_variable & operator=(const char32_t *);
    Assigns a UTF-32 encoded string literal to a variable.

basic_variable public member functions

  1. basic_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

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. template<typename T> T value() const;
    Returns current value as type T.

    Calls basic_variable<Allocator>::value<T>(std::error_code&) and throws an exception if an error occurred.

    See Also:

    basic_variable<Allocator>::value<T>(std::error_code&)

    Template Parameters:

    T

    Supported type

    Throws:

    dynamic::error with dynamic::incompatible_type if T is an unsupported or incompatible type.
  8. 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 dynamic::integer or dynamic::real, then 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:

    error

    Set to dynamic::incompatible_type if T is an unsupported or incompatible type, otherwise unchanged.

    Template Parameters:

    T

    Supported type

  9. 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>()

  10. 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:

    R

    Supported type.

    Requires:

    basic_variable<Allocator>::same<R>() is true.

  11. 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.

  12. 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:

    position

    Index position.

    Requires:

    basic_variable<Allocator>::is<array>() is true.

  13. 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.

  14. 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.
  15. 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.
  16. 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:

    T

    Type or tag to be checked.

  17. 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:

    T

    Type to be checked.

  18. 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

  19. 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

  20. bool empty() const noexcept;
    Checks if variable is empty.

    Same as size() == 0

  21. 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.

  22. 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.

  23. 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().

  24. 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.

  25. 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.

  26. 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.

  27. 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.

  28. 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.

  29. 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.

  30. void swap(basic_variable & other) noexcept;
    Exchange contents of variable and other.
  31. iterator begin();
    Returns an iterator to the beginning.
  32. const_iterator begin() const;
    Returns an iterator to the beginning.
  33. const_iterator cbegin() const;
    Returns an iterator to the beginning.
  34. iterator end();
    Returns an iterator to the end.
  35. const_iterator end() const;
    Returns an iterator to the end.
  36. const_iterator cend() const;
    Returns an iterator to the end.
  37. reverse_iterator rbegin();
    Returns a reverse iterator to the beginning.
  38. const_reverse_iterator rbegin() const;
    Returns a reverse iterator to the beginning.
  39. const_reverse_iterator crbegin() const;
    Returns a reverse iterator to the beginning.
  40. reverse_iterator rend();
    Returns a reverse iterator to the end.
  41. const_reverse_iterator rend() const;
    Returns a reverse iterator to the end.
  42. const_reverse_iterator crend() const;
    Returns a reverse iterator to the end.
  43. key_iterator key_begin() const;
    Returns a key iterator to the beginning.
  44. key_iterator key_end() const;
    Returns a key iterator to the end.

PrevUpHomeNext