I have been working on this persistence library for a while now, and I think I’ve finally got it.
To use the object persistence, one needs to inherit from the persistent object and create a serialize method that persists the object (using the dual i/o ^ operator).
In the event that a difference between loading and saving exists, an class needs to create its own read and write operators.
It also needs to make the allow_persistence class a friend.
class sample : public calvin::persistence<std ::string> {
friend class calvin::allow_persistence;
public:
sample( std::string& );
template <typename stream>
stream& serialize( stream& s, unsigned int version );
// this isn't strictly necessary, but good practice
static const unsigned int version = 2;
};
Yes, the namespace for the library is calvin, after Calvin Coolidge, the former (now dead) president.
If you wish to create a versioned class, define an unsigned integer class variable called version. This will override the default one in persistence that is written out when an instance is saved. In the event you do this, you’ll mostly likely want to use the read and write operators rather than a single serialize method to allow for handling of loading instances of different versions.
const unsigned int sample::version = 2;
Not strictly necessary, but always a good idea is the definition of field names for the use of those streams that might require them, such as an XML stream.
fields<sample> = make_fields( "m_member1", "m_member2" ... );
It seems to be en vogue now for C++ to eschew inheritance as a requirement, perhaps rightly so as inheritance can bring ugly overhead (such as virtual tables) to otherwise small classes. On the other hand, inheritance can provide clues as to the intentions of the developer. This is the case of this persistence mix-in class. It adds no overhead to an instance and makes it plain that the class can be persisted.
That should be it for making a class persistent. There are other goodies not explained here yet. That will come soon. I believe I can have the first version of this library complete by the end of the month and posted with an explanatory article by the end of July.