[serialization] free load/save
Dear all, I'd like to serialize a number of classes using the free-standing version of the serialize function. As I do not want to access internals of the classes directly, I have to work with set/get functions, which in turn implies having to write load/save functions. This seems to work for simple types, like in the following example. But will it circumvent any internal mechanisms of Boost.Serialization , if I do not give the library direct access to class data ? Will this work for complex types, e.g. vectors of shared_ptr objects ? /*************************************************************************/ class base{ public: base(void){ setSecret(1); } base(int val){ setSecret(val); } virtual ~base(){ /* nothing */ } virtual int getSecret(void) const{ return secret; } virtual void printInfo(void) = 0; void setSecret(int val){ secret = val; } private: int secret; }; BOOST_IS_ABSTRACT(base); namespace boost { namespace serialization { template<class Archive> void save(Archive & ar, const base & t, unsigned int version) { int secret = t.getSecret(); ar & make_nvp("secret", secret); } template<class Archive> void load(Archive & ar, base & t, unsigned int version) { int secret; ar & make_nvp("secret", secret); t.setSecret(secret); } template<class Archive> inline void serialize(Archive & ar, base & t; const unsigned int version) { split_free(ar, t, version); } } } /*************************************************************************/ Thanks and have a good day, Ruediger
Ruediger Berlich wrote:
Dear all,
I'd like to serialize a number of classes using the free-standing version of the serialize function. As I do not want to access internals of the classes directly, I have to work with set/get functions, which in turn implies having to write load/save functions.
This seems to work for simple types, like in the following example. But will it circumvent any internal mechanisms of Boost.Serialization , if I do not give the library direct access to class data ? Will this work for complex types, e.g. vectors of shared_ptr objects ?
yes The only problem will be with "secret" objects which are pointed to. When you de-serialize them to the stack, the internal address is wrong so after moving them using "get" you'll have to use "reset_object_address" if they are trackable. Robert Ramey
/*************************************************************************/
class base{ public: base(void){ setSecret(1); } base(int val){ setSecret(val); } virtual ~base(){ /* nothing */ }
virtual int getSecret(void) const{ return secret; }
virtual void printInfo(void) = 0;
void setSecret(int val){ secret = val; }
private: int secret; };
BOOST_IS_ABSTRACT(base);
namespace boost { namespace serialization { template<class Archive> void save(Archive & ar, const base & t, unsigned int version) { int secret = t.getSecret(); ar & make_nvp("secret", secret); }
template<class Archive> void load(Archive & ar, base & t, unsigned int version) { int secret; ar & make_nvp("secret", secret); t.setSecret(secret); }
template<class Archive> inline void serialize(Archive & ar, base & t; const unsigned int version) { split_free(ar, t, version); } } }
/*************************************************************************/
Thanks and have a good day, Ruediger
Robert, thanks again for the quick and comprehensive reply ! Ruediger Robert Ramey wrote:
yes
The only problem will be with "secret" objects which are pointed to.
When you de-serialize them to the stack, the internal address is wrong so after moving them using "get" you'll have to use "reset_object_address" if they are trackable.
Robert Ramey
participants (2)
-
Robert Ramey
-
Ruediger Berlich