
Great! Thanks a bunch. This is exactly what I would like to do. Perfect. // Anders Sundman Dave Nay skrev:
-----Original Message----- From: Anders Sundman [mailto:anders.sundman@optonova.se] Sent: Thursday, February 01, 2007 8:55 AM To: boost-users@lists.boost.org Subject: [Boost-users] Serialization of dynamic data
Hi!
About the boost::serialization library, what is the best way to handle dynamic data in classes? Any idéas, suggestions or caveats? I didn't manage to find anything about this in the docs.
<snip>
What to do with the data pointer?
Is there any way to determine (in the serialize function) if a serialization or "de-serialization" is taking place? And have different behaviours - either allocating a new char[x] or copying data to the archive?
Is there any way to store dynamic data in an archive?
Hi Anders,
I had to do exactly this same thing in our application. Below is a snip of code from my archivable class.
// Serializes this object. friend class boost::serialization::access;
template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & m_lId; ar & m_timestamp; ar & m_elements; ar & m_unLength;
if(m_unLength > 0) { if (Archive::is_loading::value) { m_pData = malloc(m_unLength); }
ar & boost::serialization::make_binary_object(m_pData, m_unLength); } }
You class will need to keep track of the size of the dynamic data to properly create the temporary object.
This is covered in the serialization documentation under Serialization Wrappers. http://www.boost.org/libs/serialization/doc/wrappers.html#binaryobjects
Dave