Thread: Boost Serialization unordered_multimap

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    40

    Boost Serialization unordered_multimap

    Hi.
    How can I serialize an unordered_multimap with boost.Serialization library? I have tried this:
    Code:
    namespace boost {
    namespace serialization {
    
    
    template<class Archive, typename X, typename Y>
    void load(Archive & ar, boost::unordered_multimap<X,Y> & g, const unsigned int version)
    {
    
        std::size_t size;
        ar & size;
        while(size--)
        {
            std::pair<X,Y> m;
            ar & m;
            g.insert(m);
        }
    }
    template<class Archive, typename X, typename Y>
    void save(Archive & ar, const boost::unordered_multimap<X,Y> & g, const unsigned int version)
    {
        std::size_t size = g.size();
        ar & size;
        for(boost::unordered_multimap<X,Y>::iterator it = g.begin(); it != g.end(); ++it)
        {
            ar & (*it);
        }
    }
    
    }
    }
    But it didn't work. The compiler complains about a serialize method is missing in boost::unordered_multimap but as I know should it work if I define a save and load function in the serialization namespace, but it doesn't.

    Any clue what I am doing wrong?

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    40
    My solution:
    Code:
    namespace boost
    {
        namespace serialization
        {
            template<class Archive, class Key, class Mapped, class Hash, class Pred, class Alloc >
            inline void save(Archive & ar, const boost::unordered_multimap<Key, Mapped, Hash, Pred, Alloc> &t, const unsigned int)
            {
                boost::serialization::stl::save_collection<Archive, boost::unordered_multimap<Key, Mapped, Hash, Pred, Alloc> >(ar, t);
            }
    
            template<class Archive, class Key, class Mapped, class Hash, class Pred, class Alloc >
            inline void load(Archive & ar, boost::unordered_multimap<Key, Mapped, Hash, Pred, Alloc> &t, const unsigned int)
            {
                boost::serialization::stl::load_collection<Archive, boost::unordered_multimap<Key, Mapped, Hash, Pred, Alloc>,
                boost::serialization::stl::archive_input_multimap<Archive, boost::unordered_multimap<Key, Mapped, Hash, Pred, Alloc> >,
                boost::serialization::stl::no_reserve_imp< boost::unordered_multimap<Key, Mapped, Hash, Pred, Alloc> > >(ar, t);
            }
    
            // split non-intrusive serialization function member into separate
            // non intrusive save/load member functions
            template<class Archive, class Key, class Mapped, class Hash, class Pred, class Alloc >
            inline void serialize(Archive & ar, boost::unordered_multimap<Key, Mapped, Hash, Pred, Alloc> &t, const unsigned int file_version)
            {
                boost::serialization::split_free(ar, t, file_version);
            }
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. boost serialization question
    By l2u in forum C++ Programming
    Replies: 1
    Last Post: 02-26-2008, 01:53 PM
  2. Boost Auto-Linking
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 12-30-2007, 06:11 AM
  3. Boost Serialization: shared_ptr
    By KessiMC in forum C++ Programming
    Replies: 0
    Last Post: 12-26-2007, 08:17 PM
  4. Replies: 3
    Last Post: 06-12-2007, 11:21 AM
  5. Integrating Boost with STLPort
    By Mario F. in forum Tech Board
    Replies: 1
    Last Post: 11-11-2006, 06:49 AM