XB Library: xb::value

The XB value class is used to represent value of a node in the XB trees. The xb::value class is not used as standalone but it is a public member of xb::node.


Interface:

namespace xb {
    class value {
        public:
            xb::element data;
            xb::element type;
            xb::element codec;

        public:
            value ();

            template <typename T>
            value (const T &, const xb::codec & = xb::null_codec);

            value (char *, const xb::codec & = xb::null_codec);
            value (const char *, const xb::codec & = xb::null_codec);
            value (wchar_t *, const xb::codec & = xb::null_codec);
            value (const wchar_t *, const xb::codec & = xb::null_codec);

        public:
            template <typename T> void to (T &, bool * = 0) const;
            template <typename T> bool to (T &, const T &, bool * = 0) const;

        public:
            bool recode (const xb::codec &, const xb::codec &, int * = 0);
            void decode_force (const xb::codec &, int * = 0);
            bool encode (const xb::codec &, int * = 0);
            bool decode (const xb::codec &, int * = 0);
    };

    bool operator == (const value &, const value &);
    bool operator != (const value &, const value &);
    bool operator <  (const value &, const value &);
    bool operator <= (const value &, const value &);
    bool operator >  (const value &, const value &);
    bool operator >= (const value &, const value &);
};

xb::value constructor

The xb::value is default-constructible thus can be constructed as empty, then assigned a value.

The xb::value can also be constructed from any particular object, using the templated constructor xb::value (const T &, const xb::codec &);. This initializes the .data memeber with the object. See xb::element for details.

The constructor is also overloaded for ANSI and wide-character strings. The content of this way created value is the string.

The xb::compact function template is used to determine the most compact representation for the data.
If codec other than xb::null_codec (default) is provided, the value (.data) is then encoded using the supplied codec.

The .type member is initialized using the xb::type template to describe (at least provide a hint to) the type of data. The template is specialized for all intrinsic C++ data types. For other types it provides an empty element. In order to describe the data type, you will need to specialize the template for your type. See xb::type for more details.

xb::value::to

There are two member functions named .to () that can be used to assign the data to a variable.

Use the first version (the .to (T &, bool *) member function) to assign the data to a variable through a reference to it (first parameter). The second bool parameter, if provided, is assigned a true if there was a minor problem during the assignment, false otherwise. This can be precission loss (floating point numbers) or higher bytes strip (a long is stored in the value, but is being assigned to a short, for example).

If the value cannot be assigned (due to a type mismatch) a std::bad_cast exception is thrown.

The second version (the .to (T &, const T & bool *) member function) is similar to the first one, but it does not throw the std::bad_cast exception. Instead a false is returned and the variable (first parameter) is assigned the default value (second parameter). The member function returns true when the conversion succeeded.

xb::value::recode

Use this member function to change encoding of the data. The first parameter specifies a codec that is used to decode the data, the second parameter specifies a codec to encode the data with. If third parameter is not a null pointer, the value that it points to is provided with an error code (if there was an error). The function returns true if successfull, false otherwise.

The .encode () and .decode () member functions are implemented in terms of this member function as small inline functions.

xb::value::decode_force

This member function issues a decode operation with provided codec without checking codec names (xb::codec::id ()) for equality.

Examples:

See xb::node for example.

Remarks:

None.


[index]