Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
alvar::Serialization Class Reference

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

#include <Util.h>

Public Member Functions

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

Protected Member Functions

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

Protected Attributes

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

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;
}

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;
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 352 of file Util.h.

Constructor & Destructor Documentation

alvar::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>> .

Definition at line 422 of file Util.cpp.

alvar::Serialization::Serialization ( std::basic_iostream< char > &  _stream)

Constructor for serializing any iostream (e.g. std::stringstream)

Definition at line 429 of file Util.cpp.

alvar::Serialization::Serialization ( std::basic_istream< char > &  _stream)

Constructor for serializing any istream (e.g. std::cin)

Definition at line 436 of file Util.cpp.

alvar::Serialization::Serialization ( std::basic_ostream< char > &  _stream)

Constructor for serializing any ostream (e.g. std::cout)

Definition at line 442 of file Util.cpp.

alvar::Serialization::~Serialization ( )

Destructor.

Definition at line 448 of file Util.cpp.

Member Function Documentation

bool alvar::Serialization::Ascend ( )
protected

Definition at line 416 of file Util.cpp.

bool alvar::Serialization::Descend ( const char *  id)
protected

Definition at line 395 of file Util.cpp.

bool alvar::Serialization::Input ( )
protected

Definition at line 378 of file Util.cpp.

bool alvar::Serialization::IsInput ( )
inline

Method for checking if we are inputting or outputting. Can be used from your serializable class.

Definition at line 448 of file Util.h.

template<class C >
Serialization& alvar::Serialization::operator<< ( C &  serializable)
inline

Operator for outputting a serializable class into the defined filename or std::iostream.

Definition at line 406 of file Util.h.

template<class C >
Serialization& alvar::Serialization::operator>> ( C &  serializable)
inline

Operator for reading a serializable class from the defined filename or std::iostream.

Definition at line 415 of file Util.h.

bool alvar::Serialization::Output ( )
protected

Definition at line 363 of file Util.cpp.

bool alvar::Serialization::Serialize ( int &  data,
const std::string &  name 
)

Method for serializing 'int' data element. Used from your serializable class.

Definition at line 453 of file Util.cpp.

bool alvar::Serialization::Serialize ( unsigned short &  data,
const std::string &  name 
)

Method for serializing 'int' data element. Used from your serializable class.

Definition at line 462 of file Util.cpp.

bool alvar::Serialization::Serialize ( unsigned long &  data,
const std::string &  name 
)

Method for serializing 'int' data element. Used from your serializable class.

Definition at line 469 of file Util.cpp.

bool alvar::Serialization::Serialize ( double &  data,
const std::string &  name 
)

Method for serializing 'double' data element. Used from your serializable class.

Definition at line 477 of file Util.cpp.

bool alvar::Serialization::Serialize ( std::string &  data,
const std::string &  name 
)

Method for serializing 'std::string' data element. Used from your serializable class.

Definition at line 485 of file Util.cpp.

bool alvar::Serialization::Serialize ( CvMat &  data,
const std::string &  name 
)

Method for serializing 'CvMat' data element. Used from your serializable class.

Definition at line 497 of file Util.cpp.

template<class C >
bool alvar::Serialization::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 428 of file Util.h.

Member Data Documentation

std::string alvar::Serialization::filename
protected

Definition at line 355 of file Util.h.

void* alvar::Serialization::formatter_handle
protected

Definition at line 358 of file Util.h.

bool alvar::Serialization::input
protected

Definition at line 354 of file Util.h.

std::ios* alvar::Serialization::stream
protected

Definition at line 357 of file Util.h.


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


ar_track_alvar
Author(s): Scott Niekum
autogenerated on Thu Jun 6 2019 19:27:24