Really should have been implemented completely as templates, it would have been much cleaner, but support is not good enough on all platforms.
Here are the steps for creating a set of serialization formats.
Create a base class for each type of serialization format which uses the first set of macros to set up its interface and then create your derived classes which implement specific formats.
Here's a full example with a class "MyClass" to be serialized in XML and JSON format. For simplicity they can be assumed to be in a single header and source file but it's not necessary.
Class declarations look like this:
The implementation source file starts with this:
If you wish automatic registration/deregistration include the lines below, otherwise allocate and destroy an object of these class types when you wish the registration/deregistration to happen.
This provides a common static interface to the class types. The most common examples are to access the implementation of a particular format by name:
And to iterate over all available formats of a given type:
Note that in the case of dynamically loaded code (e.g. plug-ins) you may not get the static object destructor called in the individual formats so instead you will have to dynamically allocate and destroy your SerializerInitializer<> objects on load and unload of your code.