My Project
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Serialization Class Reference

Class for serializing class content to/from file or std::iostream. More...

#include <Util.h>

Public Member Functions

 Serialization (std::string _filename)
 Constructor for serializing to/from specified filename. More...
 
 Serialization (std::basic_iostream< char > &_stream)
 Constructor for serializing any iostream (e.g. std::stringstream)
 
 Serialization (std::basic_istream< char > &_stream)
 Constructor for serializing any istream (e.g. std::cin)
 
 Serialization (std::basic_ostream< char > &_stream)
 Constructor for serializing any ostream (e.g. std::cout)
 
 ~Serialization ()
 Destructor.
 
template<class C >
Serializationoperator<< (C &serializable)
 Operator for outputting a serializable class into the defined filename or std::iostream.
 
template<class C >
Serializationoperator>> (C &serializable)
 Operator for reading a serializable class from the defined filename or std::iostream.
 
template<class C >
bool SerializeClass (C &serializable)
 Method for serializing a serializable class. Used by operators << and >> . More...
 
bool Serialize (int &data, const std::string &name)
 Method for serializing 'int' data element. Used from your serializable class.
 
bool Serialize (unsigned short &data, const std::string &name)
 Method for serializing 'int' data element. Used from your serializable class.
 
bool Serialize (unsigned long &data, const std::string &name)
 Method for serializing 'int' data element. Used from your serializable class.
 
bool Serialize (double &data, const std::string &name)
 Method for serializing 'double' data element. Used from your serializable class.
 
bool Serialize (std::string &data, const std::string &name)
 Method for serializing 'std::string' data element. Used from your serializable class.
 
bool Serialize (cv::Mat &data, const std::string &name)
 Method for serializing 'cv::Mat' data element. Used from your serializable class.
 
bool IsInput ()
 Method for checking if we are inputting or outputting. Can be used from your serializable class.
 

Protected Member Functions

bool Output ()
 
bool Input ()
 
bool Descend (const char *id)
 
bool Ascend ()
 

Protected Attributes

bool input
 
std::string filename
 
std::ios * stream
 
void * formatter_handle
 

Detailed Description

Class for serializing class content to/from file or std::iostream.

The class is mainly meant to serialize classes that implement two required methods SerializeId and Serialize . For example alvar::Camera implements the following to make it serializable:

const char *SerializeId { return "camera"; };
if (!ser->Serialize(calib_x_res, "width")) return false;
if (!ser->Serialize(calib_y_res, "height")) return false;
if (!ser->Serialize(calib_K, "intrinsic_matrix")) return false;
if (!ser->Serialize(calib_D, "distortion")) return false;
return true;
}
Serialization(std::string _filename)
Constructor for serializing to/from specified filename.
bool Serialize(int &data, const std::string &name)
Method for serializing 'int' data element. Used from your serializable class.

In your classes Serialize -method you can use the overloaded Serialize method of the Serialization class to serialize data or data arrays. In addition you can use SerializeClass to serialize inner serializable classes.

After the class is serializable i.e. it implements the above two methods you can serialize it as follows (some examples):

cam.SetCalib("calib.xml", 320, 240);
Serialization sero(std::cout);
sero<<cam;
Simple Camera class for calculating distortions, orientation or projections with pre-calibrated camer...
Definition: Camera.h:82
bool SetCalib(const char *calibfile, int _x_res, int _y_res, FILE_FORMAT format=FILE_FORMAT_DEFAULT)
Set the calibration file and the current resolution for which the calibration is adjusted to.
std::stringstream ss;
Serialization sero(ss);
sero<<cam;
std::cout<<ss.str()<<std::endl;
// ...
Serialization seri(ss);
seri>>cam;

See the constructor Serialization::Serialization documentation for further use examples.

Definition at line 370 of file Util.h.

Constructor & Destructor Documentation

◆ Serialization()

Serialization ( std::string  _filename)

Constructor for serializing to/from specified filename.

Serialization sero("test1.xml");
sero<<cam;
Serialization seri("test1.xml");
seri>>cam;

Note that this is not identical to:

ofstream ofs("test1.xml");
Serialization sero(ofs);
sero<<cam;
ifstream ifs("test1.xml");
Serialization seri(ifs);
sero>>cam;

There are differences with these approaches. When using the constructor with 'filename', we use the tinyxml Save and Load methods, while with iostream we use tinyxml operators for << and >> . The prior approach uses properly indented xml-files with XML declaration <?...?>. In the latter approach the indentations and the XML declaration are left out. The XML declaration <?...?> is left out because for some reason tinyxml doesn't parse it correctly when using operator>> .

Member Function Documentation

◆ SerializeClass()

bool SerializeClass ( C &  serializable)
inline

Method for serializing a serializable class. Used by operators << and >> .

Note, in the future this should be usable also from your serializable class for adding nested serializable classes.

Definition at line 453 of file Util.h.


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