Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Object serialization

An object of class Example can be serialized and deserialized using operators '>>' and '<<', on condition that:

Operators '>>' and '<<' also apply to file streams, as ifstream (resp. ifstream) derives from istream (resp. ostream).

See class qgar::GenQgarSegment for an example.

A serialized object conforms to the following pattern:

  CLASS-NAME(DATA_1) ... (DATA_N)

The source code of functions read and write, to respectively deserialize and serialize an object of class Example, should typically conform to the following patterns. Warning: Values of data members must be read and written in the same order.

  // DESERIALIZATION

  std::istream&
  Example::read(std::istream& anInStream)
  {
    qgReadObjName(anInStream, "Example");     // Get class name

    qgReadObjData(anInStream, this->_data_1); // Get first data member
    ...
    qgReadObjData(anInStream, this->_data_N); // Get last data member

    return anInStream;
  }

  // SERIALIZATION
  // data_1() is supposed to be the access function
  // of data member Example::data_1

  std::ostream&
  Example::write(std::ostream& anOutStream)
  {
    anOutStream << "Example"
                << '(' << this->_data_1() << ')'  // First data member
                ...
                << '(' << this->_data_N() << ')'; // Last data member

    return anOutStream;
  }