This library allows to serialize standard C++ data types to binary form.
Serialization and deserialization of the following data types is supported:
boolcharint8_tuint8_tint16_tuint16_tint32_tuint32_tint64_tuint64_tlong long(not the same asint64_t)unsigned long long(not the same asuint64_t)std::arraystd::basic_stringstd::liststd::mapstd::pairstd::setstd::vector
Serialization of the following data types is supported:
- fixed-size arrays
const char *(C-strings)- any classes that have
serialize()method (see below)
Deserialization of the following data types is supported:
- any custom classes which have deserialization constructor (see below)
Serialization and deserialization of float, double and long double are implemented, but data format will be changed in future.
The following include statement is necessary:
#include <rohan/Reader.hpp>
Optional using directives:
using rohan::Reader;
using rohan::Writer;
Data is written using the | operator:
const unsigned magic=0x4698CD67;
std::vector<std::string> vec {"alpha", "beta", "gamma"};
writer | magic | vec | "string";
To write fields of a class, add constant method serialize():
class Color {
public:
explicit Color(uint8_t r, uint8_t g, uint8_t b) : r(r), g(g), b(b) {}
void serialize(Writer &writer) const {
writer | r | g | b;
}
private:
uint8_t r, g, b;
};
Data is read from the stream using type conversion operator:
unsigned nCows=unsigned(reader);
std::vector<std::string> vec=std::vector<std::string>(reader);
To read fields of a class, create a unserialization constructor:
class Color {
public:
explicit Color(Reader &reader) :
r(uint8_t(reader)),
g(uint8_t(reader)),
b(uint8_t(reader)) {}
private:
uint8_t r, g, b;
};