c++iteratorboost-serialization

How to iterate over archive in boost::serialization


I loaded multiple data into boost::archive::text_oarchive, now I need to extract the data. But because the archive contains multiple records, I would need an iterator.

something like

//input archive
boost::archive::text_iarchive iarch(ifs);

//read until the end of file
while (!iarch.eof()){

//read current value
iarch >> temp;
...//do something with temp

}

is there any standard way to iterate over elements of the archive? I found only iarchive.iterator_type, but is it what I need and how do I use it?


Solution

  • The iterator type you are looking at actually comes from

    class shared_ptr_helper {
        ...
        typedef std::set<
            boost::shared_ptr<const void>,
            collection_type_compare
        > collection_type;
        typedef collection_type::const_iterator iterator_type;
    

    which is used during the load of the archive rather than being an iterator for external use I think.

    If you look at the link http://www.boost.org/doc/libs/1_53_0/libs/serialization/doc/index.html under tutorial -> STL Collection you will see the following example:

    #include <boost/serialization/list.hpp>
    
    class bus_route
    {
        friend class boost::serialization::access;
        std::list<bus_stop *> stops;
        template<class Archive>
        void serialize(Archive & ar, const unsigned int version)
        {
            ar & stops;
        }
    public:
        bus_route(){}
    };
    

    If that isn't quite what you need then you would probably need to look at overriding load and save as per http://www.boost.org/doc/libs/1_53_0/libs/serialization/doc/tutorial.html#splitting and adding handling as required.