Reputation: 675
How do I create a class which derives from boost::archive::polymorphic_xml_oarchive
? I'm assuming this is the whole purpose of the polymorphic archives? Perhaps I'm wrong.
Anyway, I want something like
class MyArchive: public boost::serialization::polymorphic_xml_oarchive
{
public:
// custom behavior for select types
void save(int const & t){}
};
I have found examples for creating custom boost archives but none using the polymorphic archive types. I have no idea what classes to derive from and what member function signatures to use.
Upvotes: 2
Views: 344
Reputation: 393769
Actually, the whole idea of polymorphic_archive
is to implement that interface (so clients do not have to know about particular (future) implementations at compile time).
So no, you wouldn't (usually/necessarily) derive from an implementation. You would implement the interface using your own means1.
This therefore assumes you have some kind of (non-polymorphic) archive implementation already lying around. If this happens to be an archive implementation that satisfies the requirements for the Boost Serialization archive concept, you can use the ready-made adaptors from boost.
There is an extensive class diagram with prose descriptions of the responsibilities of each class in the hierarchy.
Again, the relevant bits:
As can be seen in the class diagram and the header files, this implementation is just a composition of the polymorphic interface and the standard template driven implementation. This composition is accomplished by the templates
polymorphic_iarchive_route.hpp
andpolymorphic_oarchive_route.hpp
. As these contain no code specific to the particular implementation archive, they can be used to create a polymorphic archive implementation from any functioning templated archive implementation.
The Archive concept, straight from the documentation:
The Archive concept specifies the functions that a class must implement to in order to be used to serialize Serializable types. The library implements a family of archives appropriate for different purposes. This section describes how they have been implemented and one way one can implement his own archive class. Our discussion will focus on archives used for loading as the hierarchy is exactly analogous for archives used for saving data.
Our archives have been factored in to a tree of classes in order to minimize repetition of code. This is shown in the accompanying class diagram. Any class which fullfills the following requirements will function as a loading archive.
Minimum Requirements
The archive class is derived from:
template<class Archive> detail::common_iarchive;
An instance of the this template handles all the "bookkeeping" associated with serialization. In order to be a functional only the following additional functions must be declared:
void load(T &t);
This function must be implemented for all primitive data types. This can be accomplished through the use of a member template or explicit declarations for all primitive types.
void load_binary(void *address, std::size_t size);
This function should size bytes from the archive, and copy them in to memory starting at address address.
friend class boost::archive::load_access;
In addition, such a class must provide a the following friend declaration grant access to the core serialization library to the functions of this class.
So, the most trivial implementation of an input archive would look like this:
#include <boost/archive/detail/common_iarchive.hpp> ///////////////////////////////////////////////////////////////////////// // class trivial_iarchive - read serialized objects from a input text stream class trivial_iarchive : public boost::archive::detail::common_iarchive<trivial_iarchive> { // permit serialization system privileged access to permit // implementation of inline templates for maximum speed. friend class boost::archive::load_access; // member template for loading primitive types. // Override for any types/templates that special treatment template<class T> void load(T & t); public: ////////////////////////////////////////////////////////// // public interface used by programs that use the // serialization library // archives are expected to support this function void load_binary(void *address, std::size_t count); };
1 Of course, there could be reasons to derive the implementation of your archive from an existing archive, but
polymorphic_[io]archive_route
classes discussed aboveUpvotes: 1