DelegateMQ
Loading...
Searching...
No Matches
serialize Class Reference

The serialize class binary serializes and deserializes C++ objects. More...

#include <msg_serialize.h>

Classes

class  I
 Abstract interface that all serialized user defined classes inherit. More...
 

Public Types

enum class  Type {
  UNKNOWN = 0 , LITERAL = 1 , STRING = 8 , WSTRING = 9 ,
  VECTOR = 20 , MAP = 21 , LIST = 22 , SET = 23 ,
  ENDIAN = 30 , USER_DEFINED = 31
}
 
enum class  ParsingError {
  NONE , TYPE_MISMATCH , STREAM_ERROR , STRING_TOO_LONG ,
  CONTAINER_TOO_MANY , INVALID_INPUT , END_OF_FILE
}
 
typedef void(*) ErrorHandler(ParsingError error, int line, const char *file)
 
typedef void(*) ParseHandler(const std::type_info &typeId, size_t size)
 

Public Member Functions

 serialize ()=default
 
 ~serialize ()=default
 
bool LE ()
 
std::istream & readEndian (std::istream &is, bool &littleEndian)
 
void writeEndian (std::ostream &os)
 
std::istream & read (std::istream &is, I *t_)
 
std::istream & read (std::istream &is, std::string &s)
 
std::istream & read (std::istream &is, std::wstring &s)
 
std::istream & read (std::istream &is, char *str)
 
std::istream & read (std::istream &is, std::vector< bool > &container)
 
std::ostream & write (std::ostream &os, I *t_)
 
std::ostream & write (std::ostream &os, const std::string &s)
 
std::ostream & write (std::ostream &os, std::string &s)
 
std::ostream & write (std::ostream &os, const std::wstring &s)
 
std::ostream & write (std::ostream &os, std::wstring &s)
 
std::ostream & write (std::ostream &os, char *str)
 
std::ostream & write (std::ostream &os, const char *str)
 
std::ostream & write (std::ostream &os, std::vector< bool > &container)
 
template<typename T >
std::istream & read (std::istream &is, T &t_, bool readPrependedType=true)
 
template<typename T >
std::ostream & write (std::ostream &os, T &t_, bool prependType=true)
 
template<class T >
std::ostream & write (std::ostream &os, std::vector< T > &container)
 
template<class T >
std::istream & read (std::istream &is, std::vector< T > &container)
 
template<class T >
std::ostream & write (std::ostream &os, std::vector< T * > &container)
 
template<class T >
std::istream & read (std::istream &is, std::vector< T * > &container)
 
template<class K , class V , class P >
std::ostream & write (std::ostream &os, std::map< K, V, P > &container)
 
template<class K , class V , class P >
std::istream & read (std::istream &is, std::map< K, V, P > &container)
 
template<class K , class V , class P >
std::ostream & write (std::ostream &os, std::map< K, V *, P > &container)
 
template<class K , class V , class P >
std::istream & read (std::istream &is, std::map< K, V *, P > &container)
 
template<class T , class P >
std::ostream & write (std::ostream &os, std::set< T, P > &container)
 
template<class T , class P >
std::istream & read (std::istream &is, std::set< T, P > &container)
 
template<class T , class P >
std::ostream & write (std::ostream &os, std::set< T *, P > &container)
 
template<class T , class P >
std::istream & read (std::istream &is, std::set< T *, P > &container)
 
template<class T >
std::ostream & write (std::ostream &os, std::list< T > &container)
 
template<class T >
std::istream & read (std::istream &is, std::list< T > &container)
 
template<class T >
std::ostream & write (std::ostream &os, std::list< T * > &container)
 
template<class T >
std::istream & read (std::istream &is, std::list< T * > &container)
 
void setErrorHandler (ErrorHandler error_handler_)
 
ParsingError getLastError () const
 
void clearLastError ()
 
void setParseHandler (ParseHandler parse_handler_)
 

Detailed Description

The serialize class binary serializes and deserializes C++ objects.

Each class need to implement the serialize::I abstract interface to allow binary serialization to any stream. A default constructor is required for the serialized object. Most container elements can be stored by value or by pointer. C++ containers supported are:

vector
list
map
set
string
wstring
char[]

Always check the input stream or output stream to ensure no errors before using data. e.g.

if (ss.good()) // Do something with input or output data

This serialization class is not thread safe and a serialie instance should only be accessed from a single task.

The serialize class support receiving objects that have more or less data fields that what is currenting being parsed. If more data is received after parsing an object, the extra data is discard. If less data is received, the parsing of the extra data fields does not occur. This supports the protocol changing by adding new data elements to an object. Once the protocol is released at a particular version, new data elements can be added but existing ones cannot be removed/changed.

Member Typedef Documentation

◆ ErrorHandler

void(*) serialize::ErrorHandler(ParsingError error, int line, const char *file)

◆ ParseHandler

void(*) serialize::ParseHandler(const std::type_info &typeId, size_t size)

Member Enumeration Documentation

◆ ParsingError

enum class serialize::ParsingError
strong
Enumerator
NONE 
TYPE_MISMATCH 
STREAM_ERROR 
STRING_TOO_LONG 
CONTAINER_TOO_MANY 
INVALID_INPUT 
END_OF_FILE 

◆ Type

enum class serialize::Type
strong
Enumerator
UNKNOWN 
LITERAL 
STRING 
WSTRING 
VECTOR 
MAP 
LIST 
SET 
ENDIAN 
USER_DEFINED 

Constructor & Destructor Documentation

◆ serialize()

serialize::serialize ( )
default

◆ ~serialize()

serialize::~serialize ( )
default

Member Function Documentation

◆ clearLastError()

void serialize::clearLastError ( )
inline

◆ getLastError()

ParsingError serialize::getLastError ( ) const
inline

◆ LE()

bool serialize::LE ( )
inline

Returns true if little endian.

Returns
Returns true if little endian.

◆ read() [1/14]

std::istream & serialize::read ( std::istream & is,
char * str )
inline

Read a character string from a stream.

Parameters
[in]is- the input stream
[in]str- the character string to read into
Returns
The input stream

◆ read() [2/14]

std::istream & serialize::read ( std::istream & is,
I * t_ )
inline

Read a user defined object implementing the serialize:I interface from a stream. Normally the send and reciever object is the same size. However, if a newer version of the object is introduced on one side the sizes will differ. If received object is smaller than sent object, the extra data in the sent object is discarded.

Parameters
[in]is- the input stream
[in]t_- the object to read
Returns
The output stream

◆ read() [3/14]

template<class T >
std::istream & serialize::read ( std::istream & is,
std::list< T * > & container )
inline

Read into a list container from a stream. Items in list stored by pointer. Operator new called to create object instances.

Parameters
[in]is- the input stream
[in]container- the list container to read into
Returns
The input stream

◆ read() [4/14]

template<class T >
std::istream & serialize::read ( std::istream & is,
std::list< T > & container )
inline

Read into a list container from a stream. Items in list are stored by value.

Parameters
[in]is- the input stream
[in]container- the list container to read into
Returns
The input stream

◆ read() [5/14]

template<class K , class V , class P >
std::istream & serialize::read ( std::istream & is,
std::map< K, V *, P > & container )
inline

Read into a map container from a stream. Items in map stored by pointer. Operator new called to create object instances.

Parameters
[in]is- the input stream
[in]container- the map container to read into
Returns
The input stream

◆ read() [6/14]

template<class K , class V , class P >
std::istream & serialize::read ( std::istream & is,
std::map< K, V, P > & container )
inline

Read into a map container from a stream. Items in map are stored by value.

Parameters
[in]is- the input stream
[in]container- the map container to read into
Returns
The input stream

◆ read() [7/14]

template<class T , class P >
std::istream & serialize::read ( std::istream & is,
std::set< T *, P > & container )
inline

Read into a set container from a stream. Items in set stored by pointer. Operator new called to create object instances.

Parameters
[in]is- the input stream
[in]container- the set container to read into
Returns
The input stream

◆ read() [8/14]

template<class T , class P >
std::istream & serialize::read ( std::istream & is,
std::set< T, P > & container )
inline

Read into a set container from a stream. Items in set are stored by value.

Parameters
[in]is- the input stream
[in]container- the set container to read into
Returns
The input stream

◆ read() [9/14]

std::istream & serialize::read ( std::istream & is,
std::string & s )
inline

Read a str::string from a stream.

Parameters
[in]os- the input stream
[in]s- the string to read
Returns
The input stream

◆ read() [10/14]

std::istream & serialize::read ( std::istream & is,
std::vector< bool > & container )
inline

Read a vector<bool> container from a stream. The vector<bool> items are stored differently and therefore need special handling to serialize. Unlike other specializations of vector, std::vector<bool> does not manage a dynamic array of bool objects. Instead, it is supposed to pack the boolean values into a single bit each.

Parameters
[in]is- the input stream
[in]container- the vector container to read into
Returns
The input stream

◆ read() [11/14]

template<class T >
std::istream & serialize::read ( std::istream & is,
std::vector< T * > & container )
inline

Read into a vector container from a stream. Items in vector stored by pointer. Operator new called to create object instances.

Parameters
[in]is- the input stream
[in]container- the vector container to read into
Returns
The input stream

◆ read() [12/14]

template<class T >
std::istream & serialize::read ( std::istream & is,
std::vector< T > & container )
inline

Read into a vector container from a stream. Items in vector are stored by value.

Parameters
[in]is- the input stream
[in]container- the vector container to read into
Returns
The input stream

◆ read() [13/14]

std::istream & serialize::read ( std::istream & is,
std::wstring & s )
inline

Read a str::wstring from a stream.

Parameters
[in]os- the input stream
[in]s- the string to read
Returns
The input stream

◆ read() [14/14]

template<typename T >
std::istream & serialize::read ( std::istream & is,
T & t_,
bool readPrependedType = true )
inline

Read an object from a stream.

Parameters
[in]is- the input stream
[in]t_- the object to read into
Returns
The input stream

◆ readEndian()

std::istream & serialize::readEndian ( std::istream & is,
bool & littleEndian )
inline

Read endian from stream.

Parameters
[in]istream- input stream
Returns
Return true if little endian.

◆ setErrorHandler()

void serialize::setErrorHandler ( ErrorHandler error_handler_)
inline

◆ setParseHandler()

void serialize::setParseHandler ( ParseHandler parse_handler_)
inline

◆ write() [1/17]

std::ostream & serialize::write ( std::ostream & os,
char * str )
inline

Write a character string to a stream.

Parameters
[in]os- the output stream
[in]str- the character string to write.
Returns
The output stream

◆ write() [2/17]

std::ostream & serialize::write ( std::ostream & os,
const char * str )
inline

Write a const character string to a stream.

Parameters
[in]os- the output stream
[in]str- the character string to write.
Returns
The output stream

◆ write() [3/17]

std::ostream & serialize::write ( std::ostream & os,
const std::string & s )
inline

Write a const std::string to a stream.

Parameters
[in]os- the output stream
[in]s- the string to write
Returns
The output stream

◆ write() [4/17]

std::ostream & serialize::write ( std::ostream & os,
const std::wstring & s )
inline

Write a const str::wstring to a stream.

Parameters
[in]os- the output stream
[in]s- the string to write
Returns
The output stream

◆ write() [5/17]

std::ostream & serialize::write ( std::ostream & os,
I * t_ )
inline

Write a user defined object implementing the serialize:I interface to a stream.

Parameters
[in]os- the output stream
[in]t_- the object to write
Returns
The output stream

◆ write() [6/17]

template<class T >
std::ostream & serialize::write ( std::ostream & os,
std::list< T * > & container )
inline

Write a list container to a stream. The items in list are stored by pointer.

Parameters
[in]os- the output stream
[in]container- the list container to write
Returns
The output stream

◆ write() [7/17]

template<class T >
std::ostream & serialize::write ( std::ostream & os,
std::list< T > & container )
inline

Write a list container to a stream. The items in list are stored by value.

Parameters
[in]os- the output stream
[in]container- the list container to write
Returns
The output stream

◆ write() [8/17]

template<class K , class V , class P >
std::ostream & serialize::write ( std::ostream & os,
std::map< K, V *, P > & container )
inline

Write a map container to a stream. The items in map are stored by pointer.

Parameters
[in]os- the output stream
[in]container- the map container to write
Returns
The output stream

◆ write() [9/17]

template<class K , class V , class P >
std::ostream & serialize::write ( std::ostream & os,
std::map< K, V, P > & container )
inline

Write a map container to a stream. The items in map are stored by value.

Parameters
[in]os- the output stream
[in]container- the map container to write
Returns
The output stream

◆ write() [10/17]

template<class T , class P >
std::ostream & serialize::write ( std::ostream & os,
std::set< T *, P > & container )
inline

Write a set container to a stream. The items in set are stored by pointer.

Parameters
[in]os- the output stream
[in]container- the set container to write
Returns
The output stream

◆ write() [11/17]

template<class T , class P >
std::ostream & serialize::write ( std::ostream & os,
std::set< T, P > & container )
inline

Write a set container to a stream. The items in set are stored by value.

Parameters
[in]os- the output stream
[in]container- the set container to write
Returns
The output stream

◆ write() [12/17]

std::ostream & serialize::write ( std::ostream & os,
std::string & s )
inline

Write a std::string to a stream.

Parameters
[in]os- the output stream
[in]s- the string to write
Returns
The output stream

◆ write() [13/17]

std::ostream & serialize::write ( std::ostream & os,
std::vector< bool > & container )
inline

Write a vector<bool> container to a stream. The vector<bool> items are stored differently and therefore need special handling to serialize. Unlike other specialisations of vector, std::vector<bool> does not manage a dynamic array of bool objects.Instead, it is supposed to pack the boolean values into a single bit each.

Parameters
[in]os- the output stream
[in]container- the vector container to write
Returns
The output stream

◆ write() [14/17]

template<class T >
std::ostream & serialize::write ( std::ostream & os,
std::vector< T * > & container )
inline

Write a vector container to a stream. The items in vector are stored by pointer.

Parameters
[in]os- the output stream
[in]container- the vector container to write
Returns
The output stream

◆ write() [15/17]

template<class T >
std::ostream & serialize::write ( std::ostream & os,
std::vector< T > & container )
inline

Write a vector container to a stream. The items in vector are stored by value.

Parameters
[in]os- the output stream
[in]container- the vector container to write
Returns
The output stream

◆ write() [16/17]

std::ostream & serialize::write ( std::ostream & os,
std::wstring & s )
inline

Write a str::wstring to a stream.

Parameters
[in]os- the output stream
[in]s- the string to write
Returns
The output stream

◆ write() [17/17]

template<typename T >
std::ostream & serialize::write ( std::ostream & os,
T & t_,
bool prependType = true )
inline

Write an object to a stream.

Parameters
[in]os- the output stream
[in]t_- the object to write
Returns
The output stream

◆ writeEndian()

void serialize::writeEndian ( std::ostream & os)
inline

Write current CPU endian to stream.

Parameters
[in]ostream- output stream

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