Make All Necessary Class Data Available
All class data that will be isomorphically persisted must be accessible to the global functions, rwSaveGuts() and rwRestoreGuts(), used to implement persistence for the class. Note that only the information necessary to recreate an object of that class must be accessible to rwSaveGuts() and rwRestoreGuts(). Other data can be kept protected or private.
There are several ways to make protected and private data members of classes accessible. First, your class could make friends with rwSaveGuts() and rwRestoreGuts():
class Friendly {
// These global functions access private members.
friend void rwSaveGuts(RWvostream&, const Friendly&);
friend void rwRestoreGuts(RWFile&, Friendly&);
friend void rwSaveGuts(RWFile&, const Friendly&);
friend void rwRestoreGuts(RWvistream&, Friendly&);
//...
};
Or your class could have accessor functions to the restricted but necessary members:
class Accessible {
public:
int secret(){return secret_}
void secret(const int s){secret_ = s}
//...
private:
int secret_;
};
If you can not change the source code for the class to which you want to add isomorphic persistence, then you could consider deriving a new class that provides access via public methods or friendship:
class Unfriendly{
protected:
int secret_;
// ...
};
class Friendlier : public Unfriendly {
public:
int secret(){return secret_}
void secret(const int s){secret_ = s}
//...
};
If you can not change the source code for a class, you will be unable to isomorphically persist private members of that class. But remember: you only need access to the data necessary to recreate the class object, not to all the members of the class. For example, if your class has a private cache that is created at run time, you probably do not need to save and restore the cache. Thus, even though that cache is private, you do not need access to it in order to persist the class object.