[Q] reading back serialized Collection of shared_ptr
I made may be a little more headways since my previous question and now have an xml archive that appears to 'denote' the shared pointers (see below) To do this I found and used boost template function specifically for shared pointers -- a data member of myclass inheritin from std::multiset -- TRow is the Value type of the multiset that implements 'serialize' method -- -- struct { template <typename Archive> void serialize (Archive& ar, const unsigned int version)const { for(tThisClassConstIterator it=this->begin(); it!=this->end(); ++it) { boost::serialization::save<Archive,typename TRow>(ar,*it,version); } } tTEST_TB3 now however I do not understand how to 'deserialize this' The basic call to void load_table (tTEST_TB3& t, const char * filename) { // open the archive std::ifstream ifs(filename); assert(ifs.good()); boost::archive::xml_iarchive ia(ifs); ia >> BOOST_SERIALIZATION_NVP(t); } does not even compile, I suspect it is because when this 'serialize' function on tTEST_TB3 is called, now the boost::serialization::save<Archive,typename TRow>(ar,*it,version); is NOT valid, because the ar is of type iarchive, and save function on the shared_ptr is not defined for 'iarchive', only for oarchive. So there is another function in the same boost header file ( #include <boost/serialization/shared_ptr.hpp> ) that defines a 'load' function for the shared ptr However -- what I do not undestand is how to invoke this function it seems like I have to iterate through every entry in the archive and then call load to read it into a shared ptr? That seems to be contrary to all the other mechanism supported by the Serialization library. The basic examples just say that I can take a container and just shift it in (or out) of Archiver -- but that does not work if the container is of shared pointers. That's why I went the route of iterating through every element of the container to serialize, and not do not undestand how to load it back. So I need some help (I can provide the complete example if needed) thank you in advance ---------- <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <!DOCTYPE boost_serialization> <boost_serialization signature="serialization::archive" version="5"> <t class_id="0" tracking_level="0" version="0"> <px class_id="1" tracking_level="1" version="0" object_id="_0"> <otrq_prov_id>1245439719</otrq_prov_id> <clnt_dbid>1245439719</clnt_dbid> <mydate>6/19/2009 15:28:39</mydate> <amount>33222112121.212000</amount> </px> <px class_id_reference="1" object_id="_1"> <otrq_prov_id>1245439719</otrq_prov_id> <clnt_dbid>1245439719</clnt_dbid> <mydate>6/19/2009 15:28:39</mydate> <amount>33222112121.212000</amount> </px> <px class_id_reference="1" object_id="_2"> <otrq_prov_id>1245439719</otrq_prov_id> <clnt_dbid>1245439719</clnt_dbid> <mydate>6/19/2009 15:28:39</mydate> <amount>33222112121.212000</amount> </px> <px class_id_reference="1" object_id="_3"> <otrq_prov_id>1245439719</otrq_prov_id> <clnt_dbid>1245439719</clnt_dbid> <mydate>6/19/2009 15:28:39</mydate> <amount>33222112121.212000</amount> </px> <px class_id_reference="1" object_id="_4"> <otrq_prov_id>1245439719</otrq_prov_id> <clnt_dbid>1245439719</clnt_dbid> <mydate>6/19/2009 15:28:39</mydate> <amount>33222112121.212000</amount> </px> <px class_id_reference="1" object_id="_5"> <otrq_prov_id>1245439719</otrq_prov_id> <clnt_dbid>1245439719</clnt_dbid> <mydate>6/19/2009 15:28:39</mydate> <amount>33222112121.212000</amount> </px> <px class_id_reference="1" object_id="_6"> <otrq_prov_id>1245439719</otrq_prov_id> <clnt_dbid>1245439719</clnt_dbid> <mydate>6/19/2009 15:28:39</mydate> <amount>33222112121.212000</amount> </px> <px class_id_reference="1" object_id="_7"> <otrq_prov_id>1245439719</otrq_prov_id> <clnt_dbid>1245439719</clnt_dbid> <mydate>6/19/2009 15:28:39</mydate> <amount>33222112121.212000</amount> </px> <px class_id_reference="1" object_id="_8"> <otrq_prov_id>1245439719</otrq_prov_id> <clnt_dbid>1245439719</clnt_dbid> <mydate>6/19/2009 15:28:39</mydate> <amount>33222112121.212000</amount> </px> <px class_id_reference="1" object_id="_9"> <otrq_prov_id>1245439719</otrq_prov_id> <clnt_dbid>1245439719</clnt_dbid> <mydate>6/19/2009 15:28:39</mydate> <amount>33222112121.212000</amount> </px> </t> </boost_serialization> -- V S P toreason@fastmail.fm -- http://www.fastmail.fm - Send your email first class
Ok, I got it to work.. I think... So to serialize a collection (multiset in my case) of shared ptrs such that the collection inherited from std::multiset the following steps need to be taken (and may be I have more here than needed) 1) Include appropriate header file from #include <boost/serialization/set.hpp> #include <boost/serialization/vector.hpp> #include <boost/serialization/list.hpp> #include <boost/serialization/deque.hpp> 2) Do NOT make a serialize method within your class (the child fo the std::multiset) 3) you will need to typecast up to the parent your class object instance before calling the serialization/deserialization (otherwise you will get a compile time error that your class does not have a serialize method) Therefore I had to use the make_nvp function directly (and not that NVP macro) ia >> boost::serialization::make_nvp("MYTABLE",(tTB::ThisParent&)mytb); my two major mistakes were not including the header file for collection and the adding a method called serialize within my child (and that made the serialize library use my version which did not make sense) Vlad On Fri, 19 Jun 2009 16:18 -0400, "V S P" <toreason@fastmail.fm> wrote:
I made may be a little more headways since my previous question
and now have an xml archive that appears to 'denote' the shared pointers (see below)
To do this I found and used boost template function specifically for shared pointers
-- a data member of myclass inheritin from std::multiset -- TRow is the Value type of the multiset that implements 'serialize' method -- -- struct { template <typename Archive> void serialize (Archive& ar, const unsigned int version)const { for(tThisClassConstIterator it=this->begin(); it!=this->end(); ++it) { boost::serialization::save<Archive,typename TRow>(ar,*it,version); }
}
tTEST_TB3
now however I do not understand how to 'deserialize this'
The basic call to
void load_table (tTEST_TB3& t, const char * filename) { // open the archive std::ifstream ifs(filename); assert(ifs.good()); boost::archive::xml_iarchive ia(ifs);
ia >> BOOST_SERIALIZATION_NVP(t); }
does not even compile, I suspect it is because
when this 'serialize' function on tTEST_TB3 is called, now the boost::serialization::save<Archive,typename TRow>(ar,*it,version);
is NOT valid, because the ar is of type iarchive, and save function on the shared_ptr is not defined for 'iarchive', only for oarchive.
So there is another function in the same boost header file ( #include <boost/serialization/shared_ptr.hpp> ) that defines a 'load' function for the shared ptr
However -- what I do not undestand is how to invoke this function
it seems like I have to iterate through every entry in the archive and then call load to read it into a shared ptr? That seems to be contrary to all the other mechanism supported by the Serialization library.
The basic examples just say that I can take a container and just shift it in (or out) of Archiver -- but that does not work if the container is of shared pointers. That's why I went the route of iterating through every element of the container to serialize, and not do not undestand how to load it back.
So I need some help (I can provide the complete example if needed)
thank you in advance
----------
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <!DOCTYPE boost_serialization> <boost_serialization signature="serialization::archive" version="5"> <t class_id="0" tracking_level="0" version="0"> <px class_id="1" tracking_level="1" version="0" object_id="_0"> <otrq_prov_id>1245439719</otrq_prov_id> <clnt_dbid>1245439719</clnt_dbid> <mydate>6/19/2009 15:28:39</mydate> <amount>33222112121.212000</amount> </px> <px class_id_reference="1" object_id="_1"> <otrq_prov_id>1245439719</otrq_prov_id> <clnt_dbid>1245439719</clnt_dbid> <mydate>6/19/2009 15:28:39</mydate> <amount>33222112121.212000</amount> </px> <px class_id_reference="1" object_id="_2"> <otrq_prov_id>1245439719</otrq_prov_id> <clnt_dbid>1245439719</clnt_dbid> <mydate>6/19/2009 15:28:39</mydate> <amount>33222112121.212000</amount> </px> <px class_id_reference="1" object_id="_3"> <otrq_prov_id>1245439719</otrq_prov_id> <clnt_dbid>1245439719</clnt_dbid> <mydate>6/19/2009 15:28:39</mydate> <amount>33222112121.212000</amount> </px> <px class_id_reference="1" object_id="_4"> <otrq_prov_id>1245439719</otrq_prov_id> <clnt_dbid>1245439719</clnt_dbid> <mydate>6/19/2009 15:28:39</mydate> <amount>33222112121.212000</amount> </px> <px class_id_reference="1" object_id="_5"> <otrq_prov_id>1245439719</otrq_prov_id> <clnt_dbid>1245439719</clnt_dbid> <mydate>6/19/2009 15:28:39</mydate> <amount>33222112121.212000</amount> </px> <px class_id_reference="1" object_id="_6"> <otrq_prov_id>1245439719</otrq_prov_id> <clnt_dbid>1245439719</clnt_dbid> <mydate>6/19/2009 15:28:39</mydate> <amount>33222112121.212000</amount> </px> <px class_id_reference="1" object_id="_7"> <otrq_prov_id>1245439719</otrq_prov_id> <clnt_dbid>1245439719</clnt_dbid> <mydate>6/19/2009 15:28:39</mydate> <amount>33222112121.212000</amount> </px> <px class_id_reference="1" object_id="_8"> <otrq_prov_id>1245439719</otrq_prov_id> <clnt_dbid>1245439719</clnt_dbid> <mydate>6/19/2009 15:28:39</mydate> <amount>33222112121.212000</amount> </px> <px class_id_reference="1" object_id="_9"> <otrq_prov_id>1245439719</otrq_prov_id> <clnt_dbid>1245439719</clnt_dbid> <mydate>6/19/2009 15:28:39</mydate> <amount>33222112121.212000</amount> </px> </t> </boost_serialization>
-- V S P toreason@fastmail.fm
-- http://www.fastmail.fm - Send your email first class
-- V S P toreason@fastmail.fm -- http://www.fastmail.fm - Does exactly what it says on the tin
participants (1)
-
V S P