#include <Serialization.h>
Inheritance diagram for util::Serialization:
Whenever a dynamic type (i.e. a data pointed to by a base class pointer) is to be deserialized, this class does the task. All classes that can be read are added to the associative array prior to calling the readObject method.
To make a class "dynamic type" serializable, you must do the following:
namespace mynamespace { class MyBase : public Serializable { ... }; class MyDerived : public MyBase { ... }; //These could also be inner classes class MyBaseSerializer : public Serializer { ... }; class MyDerivedSerializer : public Serializer { ... }; ... Serialization s; MyBaseSerializer baseSerializer; MyDerivedSerializer derivedSerializer; s.put("mynamespace::MyBase",&baseSerializer); s.put("mynamespace::MyDerived",&derivedSerializer); //Now, cin must start with either mynamespace::MyBase or //mynamespace::MyDerived. The actual type can be unknown //to us. MyBase* m = (MyBase*)s.readObject(cin); MyDerived d; MyBase* m2 = &d; //Now, the contents of d are written to cout. //The Serializer must write the fully-qualified class name //of the object (mynamespace::MyDerived) first. s.writeObject(cout,*m2); ... }
Public Methods | |
Serialization () | |
The default constructor. | |
Serializable * | readObject (std::istream &in) throw (io::IOException&) |
Read an object from a stream. | |
void | writeObject (std::ostream &out, const Serializable &obj) throw (io::IOException&) |
Write an object to a stream. | |
void | setStripTemplateParams (bool strip) |
The Serialization can be set to ignore template parameters. | |
bool | getStripTemplateParams (void) const |
Check if this Serialization is currently stripping template parameters. |
|
The default constructor. Turns template parameter stripping on. |
|
Read an object from a stream. Serializer first reads a class name, then consults the internal associative array for a Serializer that is able to read the class and gives it the turn. The Serializer reads the object from a stream, and a newly allocated object is returned to the caller.
|
|
The Serialization can be set to ignore template parameters. That is, all instances of a template class are treated with the same Serializer. If you have MyClass<int> and MyClass<double>, the class name written to and read from a stream must be "MyClass".
|
|
Write an object to a stream. The referenced object may be any type inherited from Serializable. Its class name is fetched on-line, and a corresponding Serializer is searched. If a Serializer for the class is found, it will be responsible for creating a new object and reading it from a stream.
|