iceoryx_hoofs 2.0.5
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | List of all members
iox::cxx::Serialization Class Reference

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 <iceoryx_hoofs/cxx/serialization.hpp>

Public Types

enum class  Error { DESERIALIZATION_FAILED }
 This is an error which can be used for cxx::expected on a custom deserialization when extract fails. More...
 

Public Member Functions

 Serialization (const std::string &value) noexcept
 Creates a serialization object from a given raw serialization.
 
std::string toString () const noexcept
 string conversion operator, returns the raw serialized string
 
 operator std::string () const noexcept
 string conversion operator, returns the raw serialized string
 
template<typename T , typename... Targs>
bool extract (T &t, Targs &... args) const noexcept
 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 noexcept
 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.
 

Static Public Member Functions

template<typename... Targs>
static Serialization create (const Targs &... args) noexcept
 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

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";
};
Simple serializer which serials every given type into the following format: (The type needs to be con...
Definition serialization.hpp:66
static Serialization create(const Targs &... args) noexcept
Create Serialization if every arguments is convertable to string via cxx::convert::toString,...

Member Enumeration Documentation

◆ Error

enum class iox::cxx::Serialization::Error
strong

This is an error which can be used for cxx::expected on a custom deserialization when extract fails.

Enumerator
DESERIALIZATION_FAILED 

indicates a failed deserialization

Constructor & Destructor Documentation

◆ Serialization()

iox::cxx::Serialization::Serialization ( const std::string &  value)
explicitnoexcept

Creates a serialization object from a given raw serialization.

Parameters
[in]valuestring of serialized data

Member Function Documentation

◆ create()

template<typename... Targs>
static Serialization iox::cxx::Serialization::create ( const Targs &...  args)
staticnoexcept

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
[in]argslist of string convertable data
Returns
Serialization object which contains the serialized data

◆ extract()

template<typename T , typename... Targs>
bool iox::cxx::Serialization::extract ( T &  t,
Targs &...  args 
) const
noexcept

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
[in]treference where the first value in the serialization will be stored in
[in]argsreference where the remainding values in the serialization will be stored in
Returns
true if extraction of all values was successfull, otherwise false

◆ getNth()

template<typename T >
bool iox::cxx::Serialization::getNth ( const unsigned int  index,
T &  t 
) const
noexcept

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
[in]indexindex to the value which should be extracted
[in]tvariable where the value should be stored
Returns
true if extraction was successful, otherwise false

◆ operator std::string()

iox::cxx::Serialization::operator std::string ( ) const
noexcept

string conversion operator, returns the raw serialized string

Returns
serialized string

◆ toString()

std::string iox::cxx::Serialization::toString ( ) const
noexcept

string conversion operator, returns the raw serialized string

Returns
serialized string

The documentation for this class was generated from the following file: