XB Library: xb::codec

The xb::codec is abstract base class that provides interface needed to implement codecs (compression, encryption, ...) for XB node values.


Interface:

namespace xb {
    class codec {
        protected:
            virtual ~codec () throw ();

        public:
            virtual xb::element encode (const xb::element &, int * = 0) const = 0;
            virtual xb::element decode (const xb::element &, int * = 0) const = 0;
            virtual xb::element id () const = 0;
};

xb::codec::encode

The member function returns encoded version of the element (passed as parameter). If the second parameter is passed (not a NULL pointer), the function should set it to an error code or 0 when successfull.

xb::codec::decode

The member function returns original of an encoded element (first parameter). If the second parameter is passed (not a NULL pointer), the function should set it to an error code or 0 when successfull.

xb::codec::id

This member function must return a xb::element uniquely naming the codec used to encode the data. The empty element is reserved for null-codec that does not alter the data in any way.

Examples:

// ...

class xy_cipher : public xb::codec {
    public:
        virtual xb::element encode (const xb::element & in, int * err = 0) const {
            xb::element out;
            // ...
            *err = 0;
            return out;
        };
        virtual xb::element decode (const xb::element & in, int * err = 0) const {
            xb::element out;
            // ...
            *err = 0;
            return out;
        };
       
        virtual xb::element id () const {
            return xb::element ("xy_cipher");
        };
} my_xy_cipher;

// ...

xb::value v;
int error = 0;

if (v.encode (my_xy_cipher, &error)) {
    // ok, proceed
} else {
    // failed, error
};

Remarks:

Note that a xb::null_codec ...


[index]