Skip to content

iox::cxx::Serialization🔗

Simple serializer which serials every given type into the following format: (The type needs to be convertable into a string via cxx::convert::toString) LENGTH:DATALENGTH:DATA... Example: Serializes "hello", 123, 123.01 into 5:hello3:1236:123.01. More...

#include <serialization.hpp>

Public Functions🔗

Name
Serialization(const std::string & value)
Creates a serialization object from a given raw serialization.
std::string toString() const
string conversion operator, returns the raw serialized string
operator std::string() const
string conversion operator, returns the raw serialized string
template \<typename T ,typename... Targs>
bool
extract(T & t, Targs &... args) const
Extracts the values from the serialization and writes them into the the given args, if one value is not convertable it returns false (e.g. convert "hello" to an integer) It also returns false if the underlying serialization string has a wrong syntax.
template \<typename T >
bool
getNth(const unsigned int index, T & t) const
Extracts the value at index and writes it into t. If the conversion failed it returns false It also returns false if the underlying serialization string has a wrong syntax.
template \<typename... Targs>
Serialization
create(const Targs &... args)
Create Serialization if every arguments is convertable to string via cxx::convert::toString, this means if the argument is either a pod (plain old data) type or is convertable to string (operator std::string())

Detailed Description🔗

class iox::cxx::Serialization;

Simple serializer which serials every given type into the following format: (The type needs to be convertable into a string via cxx::convert::toString) LENGTH:DATALENGTH:DATA... Example: Serializes "hello", 123, 123.01 into 5:hello3:1236:123.01.

auto serial = cxx::Serialization::create("fuu", 123, 12.12f, 'c');
std::cout << serial.toString() << std::endl;

std::string v1;
int v2;
float v3;
char v4;

if ( serial.extract(v1, v2, v3, v4) ) {} // succeeds since every value is convertable

if ( serial.getNth(0, v2) ) {} // fails since "fuu" is not an integer

// if you'd like to write a serializable class they need to have a CTor
// with a cxx::Serialization argument and an operator cxx::Serialization
class Fuu {
    public:
        Fuu(const cxx::Serialization & s) {
            if ( !s.Extract(v1, v2, v3) ) {} // error handling
        }
        operator cxx::Serialization() const {
            return cxx::Serialization::Create(v1, v2, v3);
        }
    private:
        int v1 = 123;
        char v2 = 'c';
        std::string v3 = "hello world";

};

Public Functions Documentation🔗

function Serialization🔗

inline explicit Serialization(
    const std::string & value
)

Creates a serialization object from a given raw serialization.

Parameters:

  • value string of serialized data

function toString🔗

inline std::string toString() const

string conversion operator, returns the raw serialized string

Return: serialized string

function operator std::string🔗

inline operator std::string() const

string conversion operator, returns the raw serialized string

Return: serialized string

function extract🔗

template <typename T ,
typename... Targs>
inline bool extract(
    T & t,
    Targs &... args
) const

Extracts the values from the serialization and writes them into the the given args, if one value is not convertable it returns false (e.g. convert "hello" to an integer) It also returns false if the underlying serialization string has a wrong syntax.

Parameters:

  • t reference where the first value in the serialization will be stored in
  • args reference where the remainding values in the serialization will be stored in

Return: true if extraction of all values was successfull, otherwise false

function getNth🔗

template <typename T >
inline bool getNth(
    const unsigned int index,
    T & t
) const

Extracts the value at index and writes it into t. If the conversion failed it returns false It also returns false if the underlying serialization string has a wrong syntax.

Parameters:

  • index index to the value which should be extracted
  • t variable where the value should be stored

Return: true if extraction was successful, otherwise false

function create🔗

template <typename... Targs>
static inline Serialization create(
    const Targs &... args
)

Create Serialization if every arguments is convertable to string via cxx::convert::toString, this means if the argument is either a pod (plain old data) type or is convertable to string (operator std::string())

Parameters:

  • args list of string convertable data

Return: Serialization object which contains the serialized data


Updated on 17 June 2021 at 11:15:26 CEST