Re: [Boost-users] DeSerialization to update content of objectinsteadof construct new object

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@phys.ethz.ch> Reply-To: boost-users@lists.boost.org To: boost-users@lists.boost.org 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@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org 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

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@phys.ethz.ch> Reply-To: boost-users@lists.boost.org To: boost-users@lists.boost.org 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@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org 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@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Hi Matthias It is so strange that all the derived objects are treated as base objects during serialization and deserialization. From the xml file, I found that all the class_id_reference are same within the vector. <m_vpBase class_id="3" tracking_level="0" version="0"> <count>7</count> <loaditem class_id_reference="1" object_id="_2"> <ComponentID>4</ComponentID> <BaseInt>11478</BaseInt> </loaditem> <loaditem class_id_reference="1" object_id="_3"> <ComponentID>3</ComponentID> <BaseInt>19169</BaseInt> </loaditem> <loaditem class_id_reference="1" object_id="_4"> <ComponentID>6</ComponentID> <BaseInt>5705</BaseInt> </loaditem> <loaditem class_id_reference="1" object_id="_5"> <ComponentID>7</ComponentID> <BaseInt>23281</BaseInt> </loaditem> <loaditem class_id_reference="1" object_id="_6"> <ComponentID>5</ComponentID> <BaseInt>26962</BaseInt> </loaditem> <loaditem class_id_reference="1" object_id="_7"> <ComponentID>0</ComponentID> <BaseInt>41</BaseInt> </loaditem> <loaditem class_id_reference="1" object_id="_8"> <ComponentID>1</ComponentID> <BaseInt>18467</BaseInt> </loaditem> </m_vpBase> Is it correct? And It works properly when some of the pointers are pointing to class base objects. One more question, I wonder could I add virtual to the function "void serialize(Archive &ar, const unsigned int /* file_version */)" ? Regards Barr Ng
From: Matthias Troyer <troyer@phys.ethz.ch> Reply-To: boost-users@lists.boost.org To: boost-users@lists.boost.org Subject: Re: [Boost-users] DeSerialization to update content ofobjectinsteadof construct new object Date: Wed, 16 May 2007 16:16:39 +1000
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@phys.ethz.ch> Reply-To: boost-users@lists.boost.org To: boost-users@lists.boost.org 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@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org 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@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_________________________________________________________________ Learn English via Shopping Game, FREE! http://www.linguaphonenet.com/BannerTrack.asp?EMSCode=MSN06-03ETFJ-0211E

T On 16 May 2007, at 18:30, Ng Pan wrote:
Hi Matthias
It is so strange that all the derived objects are treated as base objects during serialization and deserialization. From the xml file, I found that all the class_id_reference are same within the vector.
That is expected since you serialize a base object by dereferencing a pointer to the base class. The cast to the derived class is done only when you serialize through the pointer, which is what you wanted to avoid. Now you will need to implement the dispatch to the serialization function of the derived class yourself. You can do this either by implementing a similar mechanism as used in Boost.Serialization - or - if you use only one archive type - you could also write polymorphic (virtual) save and load functions for your classes. No matter what you do, you need to be extremely careful: there will be serious problems if upon loading the type of derived class is different than what it was when you had saved it. Suppose you have two derived classes A and B. There will be problems if you serialized an A and now try to deserialize a B through the base pointer. That is why the serialization library has to construct the object pointed to. You thus proceed at your own risk if you implement your idea. Matthias
participants (3)
-
Matthias Troyer
-
Ng Pan
-
Robert Ramey