1

I need to serialize an object that include an hash_map with another object as key. The object that is used as key is a base class for other objects. I have implemented the serialize() method in the base class and in derived classes, and each derived class inherits the serialization method of the base class. The situation is similar to this:

#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/hash_map.hpp> 
#include <boost/serialization/base_object.hpp>

class Item {
protected:

    unsigned int _refc;
    static unsigned int _total_alloc;

//other code

private:
    friend class boost::serialization::access;

    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
    ar & _refc;
    ar & _total_alloc;
    }
};

class StringItem : public Item {
private:
    string _s;

    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
    ar & boost::serialization::base_object<Item>(*this);    
    ar & _s;
    }

};

This is the class that i need to serialize:

class TokenFinder : public Model {
public:

     TokenFinder(void);

     virtual ~TokenFinder(void);

     virtual void insert_item(Item *item);

private:
/** Map to store tokens together with their number of occurrences. 
*/
     __gnu_cxx::hash_map<Item *, unsigned long> _elements;
     unsigned long _element_count;

     friend class boost::serialization::access;
     template<class Archive>
     void serialize(Archive & ar, const unsigned int version)
     {
       ar & _elements; //Error when saved
       ar & _element_count;
 }
};

When I try to serialize a TokenFinder object the error is: terminate called after throwing an instance of 'boost::archive::archive_exception' what(): unregistered class - derived class not registered or exported

Any suggestions? Thanks in advance!

ziosam
  • 11
  • 1

1 Answers1

1

Try registering the subclasses of Item with the Archive prior to use:

template<class Archive>
    void serialize(Archive & ar, unsigned)
    {
        ar.template register_type<StringItem>(); // THIS

        ar & boost::serialization::base_object<Model>(*this);    
        ar & _elements;
        ar & _element_count;
    }

See a Live demo On Coliru

Output

22 serialization::archive 10 0 0 0 0 0 0 6 0 0 0 1 1 0
0 1 0
1 0 10 cow-jumped 6 1
2
3 0 4 moon 5 1
4
5 0 4 lazy 4 1
6
7 0 3 the 3 1
8
9 0 5 world 2 1
10
11 0 5 hello 1 0

Notes

  • I defined serialization for std::unordered_map so you no longer have to use deprecated GNU library extensions (see also this bug-report/patch)
  • I commented out the _total_alloc because, surely, you don't want this number de-serialized
  • I left managing the lifetime/allocation of Items as an exercise (I don't know how you wanted ownership semantics organized)

Full Code

#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

#include <unordered_map>
#include <boost/serialization/collections_save_imp.hpp>
#include <boost/serialization/collections_load_imp.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/serialization/split_free.hpp>

namespace boost { namespace serialization {

    template<class Archive, typename... TArgs >
        inline void save(Archive & ar, std::unordered_map<TArgs...> const&t, unsigned) {
            boost::serialization::stl::save_collection<Archive, std::unordered_map<TArgs...> >(ar, t);
        }

    template<class Archive, typename... TArgs >
        inline void load(Archive & ar, std::unordered_map<TArgs...> &t, unsigned) {
            boost::serialization::stl::load_collection<Archive,
                std::unordered_map<TArgs...>,
                boost::serialization::stl::archive_input_map<
                    Archive, std::unordered_map<TArgs...> >,
                boost::serialization::stl::no_reserve_imp<std::unordered_map<TArgs...> >
                    >(ar, t);
        }

    // split non-intrusive serialization function member into separate
    // non intrusive save/load member functions
    template <class Archive, typename... TArgs>
        inline void serialize(Archive & ar, std::unordered_map<TArgs...> &t, unsigned file_version) {
            boost::serialization::split_free(ar, t, file_version);
        }
} }

#include <boost/serialization/base_object.hpp>

class StringItem;

class Item {
  protected:
    unsigned int _refc;
    static unsigned int _total_alloc;

    //other code
    Item() : _refc(0) { }
    virtual ~Item() {}

  private:
    friend class boost::serialization::access;

    template<class Archive>
        void serialize(Archive & ar, unsigned)
    {
        ar & _refc;
        //ar & _total_alloc; // wut? a static?!
    }
};

/*static*/ unsigned int Item::_total_alloc;

class StringItem : public Item {
  public:
    StringItem(std::string s = "") : _s(std::move(s)) { }
  private:
    std::string _s;

    friend class boost::serialization::access;
    template<class Archive>
        void serialize(Archive & ar, unsigned)
        {
            ar & boost::serialization::base_object<Item>(*this);    
            ar & _s;
        }
};


struct Model {
    virtual ~Model() {}
    template<class Archive> void serialize(Archive&r, unsigned) { }
};

class TokenFinder : public Model
{
  public:
    TokenFinder(void) : _element_count(0) {}

    virtual ~TokenFinder(void) {}

    virtual void insert_item(Item *item) { _elements[item] = _elements.size()+1; }

  private:
    /** Map to store tokens together with their number of occurrences. */
    std::unordered_map<Item*, unsigned long> _elements;
    unsigned long _element_count;

    friend class boost::serialization::access;
    template<class Archive>
        void serialize(Archive & ar, unsigned)
        {
            ar.template register_type<StringItem>();
            ar & boost::serialization::base_object<Model>(*this);    
            ar & _elements;
            ar & _element_count;
        }
};

int main()
{
    boost::archive::text_oarchive oa(std::cout);

    std::vector<StringItem> seed_data {
        {"hello"},{"world"},{"the"},{"lazy"},{"moon"}, {"cow-jumped"} 
    };



    TokenFinder tf;
    for(auto& si : seed_data)
        tf.insert_item(&si);

    oa << tf;

}
sehe
  • 374,641
  • 47
  • 450
  • 633