Boost logo

Boost Users :

From: Dave Nay (dave.nay_at_[hidden])
Date: 2007-02-01 14:39:32


 

-----Original Message-----
From: Anders Sundman [mailto:anders.sundman_at_[hidden]]
Sent: Thursday, February 01, 2007 8:55 AM
To: boost-users_at_[hidden]
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


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net