To create a serializeable class you need to use the following macro's to define the members.
The TRL_BEGIN_MEMBERS macro defines a default constructor so you can't define it yourself. You can put these macro's anywhere in the class and not every line between TRL_BEGIN_MEMBERS and TRL_END_MEMBERS needs to contain a call to TRL_MEMBER. So a TRL_MEMBER call can take up multiple lines. You can use all the basic types and std::string and std::wstring as members. You can also use smart pointers and standard containers, these are explained later on. First lets look at the serialize and deserialize functions.
As you can see the serialize function takes a const reference to the object and a reference to a long as input parameters. This long is filled in to give the length of the serialized data. The deserialize class returns a pointer to an on heap allocated object. It's up to you to do the memory management of this object. If the amount of data that is consumed by deserialize doesn't match the length that is passed in, an exception is thrown.
You can also use composition.
As smart pointers you can use std::auto_ptr, boost::shared_ptr and boost::weak_ptr. If you are using the c++0x version these become std::unique_ptr, std::shared_ptr and std::weak_ptr.
All standard containers are supported. If you are using the c++0x version, the newly added containers are also supported.
As you can see the example for the map is a bit more involving, you need to define a typedef first. This is always the case if you want to use types that have multiple template parameters. You can also combine smart pointers and containers, so you can have a vector of shared_ptr.
Furthermore std::pair, std::complex and std::valarray are also supported.
If you want to use inheritance you need to add an extra macro to define the base class.
If you want to use a derived class in a polymorphic way, eg. store pointers to the base class, you need an extra macro. This one has to be put in the *.cpp file. You need to call this macro for the derived class and all the base classes that can be constructed by users of the class.
You can also use multiple inheritance. For this you simply need to add an extra TRL_BASE_CLASS macro.
If you want to use multiple inheritance in a polymorphic way, it gets a bit more complicated. You still need to call TRL_POLYMORPH for all the base classes (that can be constructed by the users), but for the derived class you need to call a special macro TRL_POLYMORPH_MULTIPLE_X where X is the number of direct and indirect base classes. The first argument of this macro is the derived class, the following arguments are all the base classes in any order.
If you need to do some initialization after an object is deserialized, you can do this by adding a static initialize member function to the class.
This initialize function is called after the object is completely constructed. It is called for all the base classes in the correct order.
You can also set the memory alignment and endian type when you call a serialize or deserialize function. Using the platform memory alignment (the default) gives the best performance but results in larger files. For the smallest file use an alignment of 1. For endian support you can use the macro's TRL_LITTLE_ENDIAN and TRL_BIG_ENDIAN.
You can also use versioning with TRL. For this you use the macro TRL_MEMBER_V.
In this example the float doesn't get serialized or deserialized because it has version 2. You can still initialize it afterwards with an initialize function.