Boost logo

Boost Users :

From: Matthias Troyer (troyer_at_[hidden])
Date: 2007-05-16 02:16:39


Be careful that this will not work if your base* is a pointer to a
base class, and you actually want to (de)serialize the derived object.

Matthias

On 16 May 2007, at 13:44, Ng Pan wrote:

> Thank you very much.
> I have successfully overloaded the serialization function. Here is
> the code
> as the reference for other ppl doing the similar modification.
>
> base.h
> #ifndef BASE_H
> #define BASE_H
> ...
> #include<boost\serialization\vector.hpp>
> ...
> class base
> {
> friend class boost::serialization::access;
> template<class Archive>
> void serialize(Archive &ar, const unsigned int /*
> file_version */){
> ar & BOOST_SERIALIZATION_NVP(m_nID);
> }
> .....
> }
> ...
>
> #if defined(__SGI_STL_PORT) || defined(_STLPORT_VERSION)
> #define STD _STLP_STD
> #else
> #define STD std
> #endif
>
> namespace boost {
> namespace serialization {
>
> //vector<Base *>
> template<class Archive, class Allocator>
> inline void save(
> Archive & ar,
> const STD::vector<base *, Allocator> &t,
> const unsigned int /* file_version */
> ){
> // record number of elements
> unsigned int count = t.size();
> ar << BOOST_SERIALIZATION_NVP(count);
> for(unsigned int i=0; i< count; i++)
> ar<<boost::serialization::make_nvp("loaditem", t.at(i));
> }
>
> template<class Archive, class Allocator>
> inline void load(
> Archive & ar,
> STD::vector<base *, Allocator> &t,
> const unsigned int /*file_version*/
> ){
> // // retrieve number of elements
> unsigned int count;
> ar >> BOOST_SERIALIZATION_NVP(count);
> for(unsigned int i=0; i<count; i++)
> ar >> boost::serialization::make_nvp("loaditem", *t.at(i));
> }
> }
> }
> ...
> #endif
>
>
> Barr Ng
>
>
>> From: Matthias Troyer <troyer_at_[hidden]>
>> Reply-To: boost-users_at_[hidden]
>> To: boost-users_at_[hidden]
>> Subject: Re: [Boost-users] DeSerialization to update content of
>> objectinsteadof construct new object
>> Date: Wed, 16 May 2007 07:22:28 +1000
>>
>> Another, maybe easier option, could be to overload the serialization
>> functions for std::vector<base *> .
>>
>> Matthias Troyer
>>
>> On 16 May 2007, at 01:02, Robert Ramey wrote:
>>
>>> The code for serialization of a vector is found in serialization/
>>> vector.hpp.
>>>
>>> I implemented this code to reconstuct the vector items rather than
>>> just reload them. I did this for a number of reasons - which for
>>> this discussion are not important.
>>>
>>> If this doesn't suit your needs, I would suggest you make your own
>>> version of serialization.hpp which operates differently. Starting
>>> with the current version it wouldn't be at all hard to do.
>>>
>>> If you want to use both current ("standard") method in some
>>> places in addition to your "custom" method, you might consider
>>> making some sort of wrapper which tags your special
>>> vectors with a type which can be distinguished for special
>>> serialization treatment.
>>>
>>> Robert Ramey
>>>
>>> Ng Pan wrote:
>>>> Hi
>>>>
>>>> I am using boost serialization to synchronise objects between
>>>> different computers (network). The objects would be serializated
>>>> as a
>>>> string instead of a file by using the class stringstream(instead of
>>>> fstream). The string would be transferred via TCP/IP layer.
>>>>
>>>> Let simpify the situation as only two computers (Master and Slave)
>>>> ,both of two computers have N objects and I wanna synchronise their
>>>> content. I create a vector of pointer ( std::vector<base *>
>>>> m_vpBase
>>>> ) which points to the dirty objects. Both of two computers have
>>>> this
>>>> vector. Let assume the vectors are already pointing to the
>>>> corresponding objects. For Tx side, it would serialize the
>>>> objects as
>>>> a string. For Rx side, I want to update the content of the objects
>>>> that its vector is pointing to.
>>>>
>>>> Tx side:
>>>> oa << BOOST_SERIALIZATION_NVP(m_vpBase);
>>>>
>>>> Rx side:
>>>> oa >> BOOST_SERIALIZATION_NVP(m_vpBase );
>>>>
>>>> However, the above code would construct new objects.
>>>>
>>>> What i want is just update the content of the objects , not
>>>> construct
>>>> new objects.
>>>>
>>>> I know how to update the content for one object by using pointer (
>>>> base * m_pBase).
>>>> Tx side:
>>>> oa << BOOST_SERIALIZATION_NVP(m_pBase);
>>>>
>>>> Rx side:
>>>> oa >> BOOST_SERIALIZATION_NVP(*m_pBase);
>>>>
>>>>
>>>> But, I don't know how to do for a vector of pointer. If you know,
>>>> please give me some hints. Thank you very much.
>>>>
>>>> Regards
>>>> Barr Ng
>>>> [I found Boost serialization is useful and easy to use. ]
>>>>
>>>> _________________________________________________________________
>>>> No masks required! Use MSN Messenger to chat with friends and
>>>> family.
>>>> http://go.msnserver.com/HK/25382.asp
>>>
>>>
>>>
>>> _______________________________________________
>>> Boost-users mailing list
>>> Boost-users_at_[hidden]
>>> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>>
>> _______________________________________________
>> Boost-users mailing list
>> Boost-users_at_[hidden]
>> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
> _________________________________________________________________
> Get 10Mb extra storage for MSN Hotmail. Subscribe Now!
> http://join.msn.com/?pgmarket=en-hk
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users


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