serialization of vector of pointers
Hi, What is the right way to serialize/deserialize a container of pointers, e.g. std::vector< int* > ? I want to store the values, not the addresses in this case. Should I just flatten my data and store it in a different fashion, then read the flat data and build my objects with a builder? I ended up storing the size of the container in a separate variable before the data, when serializing. Then when deserializing, I use this size value to scale the container, create new ints, read their individual values via the deserialized data values and finally store them back to the new object. Can you give an example of how to do this better? cheers, --stathis
On Sat, 01 Dec 2007 08:21:47 +0100, Stathis wrote:
Hi,
What is the right way to serialize/deserialize a container of pointers, e.g. std::vector< int* > ? I want to store the values, not the addresses in this case. Should I just flatten my data and store it in a different fashion, then read the flat data and build my objects with a builder?
I think the main reason is that Boost Serialization doesn't track pointers to the primitives. So I have a feeling you want to use BOOST_STRONG_TYPEDEF (or whatever it is) and use a vector of those things. Then #include <boost/serialization/vector.hpp> and ar & vec_of_ptrs If I am not mistaken, I think the magic should take over from there. -- Sohail Somani http://uint32t.blogspot.com
Good call - two issues a) using built-in serialization of collections b) saving a pointer to a primitive will result in untracked pointers. The proposed solution below addresses this second consideration. Robert Ramey Sohail Somani wrote:
On Sat, 01 Dec 2007 08:21:47 +0100, Stathis wrote:
Hi,
What is the right way to serialize/deserialize a container of pointers, e.g. std::vector< int* > ? I want to store the values, not the addresses in this case. Should I just flatten my data and store it in a different fashion, then read the flat data and build my objects with a builder?
I think the main reason is that Boost Serialization doesn't track pointers to the primitives. So I have a feeling you want to use BOOST_STRONG_TYPEDEF (or whatever it is) and use a vector of those things.
Then #include <boost/serialization/vector.hpp> and ar & vec_of_ptrs
If I am not mistaken, I think the magic should take over from there.
Robert Ramey wrote:
Good call - two issues
a) using built-in serialization of collections b) saving a pointer to a primitive will result in untracked pointers. The proposed solution below addresses this second consideration.
Robert Ramey
Sohail Somani wrote:
On Sat, 01 Dec 2007 08:21:47 +0100, Stathis wrote:
Hi,
What is the right way to serialize/deserialize a container of pointers, e.g. std::vector< int* > ? I want to store the values, not the addresses in this case. Should I just flatten my data and store it in a different fashion, then read the flat data and build my objects with a builder? I think the main reason is that Boost Serialization doesn't track pointers to the primitives. So I have a feeling you want to use BOOST_STRONG_TYPEDEF (or whatever it is) and use a vector of those things.
Then #include <boost/serialization/vector.hpp> and ar & vec_of_ptrs
If I am not mistaken, I think the magic should take over from there.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Hi again, Thanks for the information. I did look at what you suggested, but it doesn't work when I reduce it to my case. If instead of a std::list<A*> somelist, I have std::list<int*> it doesn't handle the serialization of the list (it doesn't compile). Am I not supposed to use it like this? Even when I use BOOST_STRONG_TYPEDEF to define a new type of int and try to serialize a list of that type, it still doesn't work. Here is two things I have tried: <snip> #include <fstream> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/serialization/list.hpp> #define TESTFILE "test.txt" int main( int /* argc */, char* /* argv */[] ) { std::list<int*> alist; alist.push_back(new int); { std::ofstream os( TESTFILE ); boost::archive::text_oarchive oa(os); oa << alist; } std::list<int*> alist1; { std::ifstream is( TESTFILE ); boost::archive::text_iarchive ia(is); ia >> alist1; } return 0; } </snip> The one above doesn't compile. I also wrote a different example case: <snip> #include <fstream> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/serialization/list.hpp> #define TESTFILE "test.txt" class B { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & myVar; } int myVar; public: B(){}; }; class A { friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & stuff; } std::list<B *> stuff; public: A(){} }; int main( int /* argc */, char* /* argv */[] ) { const A objA; { std::ofstream os( TESTFILE ); boost::archive::text_oarchive oa(os); oa << objA; } A objB; { std::ifstream is( TESTFILE ); boost::archive::text_iarchive ia(is); ia >> objB; } return 0; } </snip> which works, but if I change: std::list<B *> stuff; // in class A to std::list<int *> stuff; it fails to compile. Can you explain what is wrong with my intended use case and if possible how to do it properly? Thank you for the help. regards, --stathis
On Sun, 02 Dec 2007 01:33:49 +0100, Stathis wrote:
which works, but if I change: std::list<B *> stuff; // in class A to std::list<int *> stuff;
it fails to compile.
Can you explain what is wrong with my intended use case and if possible how to do it properly? Thank you for the help.
Hi. This is why I suggested you use BOOST_STRONG_TYPEDEF. What you have done with class B is essentially what BOOST_STRONG_TYPEDEF would do. So try doing BOOST_STRONG_TYPEDEF(int,trackable_int) and use a list of *these* objects. Another option is to specialize some trait whose name escapes me. Good luck! -- Sohail Somani http://uint32t.blogspot.com
This is already built into the library. See test_list_ptr for an example how its done with std::list. std::vector should be the same. Robert Ramey Stathis wrote:
Hi,
What is the right way to serialize/deserialize a container of pointers, e.g. std::vector< int* > ? I want to store the values, not the addresses in this case. Should I just flatten my data and store it in a different fashion, then read the flat data and build my objects with a builder?
I ended up storing the size of the container in a separate variable before the data, when serializing. Then when deserializing, I use this size value to scale the container, create new ints, read their individual values via the deserialized data values and finally store them back to the new object. Can you give an example of how to do this better?
cheers, --stathis
participants (3)
-
Robert Ramey
-
Sohail Somani
-
Stathis