libmoost
/home/mhx/git/github/libmoost/include/moost/serialization/hashset_serializer.hpp
Go to the documentation of this file.
00001 /* vim:set ts=3 sw=3 sts=3 et: */
00028 #ifndef MOOST_SERIALIZATION_HASH_SET_SERIALISER_HPP__
00029 #define MOOST_SERIALIZATION_HASH_SET_SERIALISER_HPP__
00030 
00031 #include "../container/sparse_hash_set.hpp"
00032 #include "../container/dense_hash_set.hpp"
00033 
00034 #include <boost/mpl/if.hpp>
00035 #include <boost/cast.hpp>
00036 
00041 namespace moost { namespace serialization {
00042    struct hash_set_save__
00043    {
00044       template<typename archiveT, typename setT>
00045          static void serialize(archiveT & ar, setT const & set, const unsigned int /*version*/)
00046       {
00047          // force 64 bit for portability
00048          boost::int64_t size = set.size();
00049          ar.save_binary(&size , sizeof(size));
00050 
00051          for(typename setT::const_iterator itr = set.begin() ; itr != set.end() ; ++itr)
00052          {
00053             ar << *itr;
00054          }
00055       }
00056    };
00057 
00058    struct hash_set_load__
00059    {
00060       template<typename archiveT, typename setT>
00061          static void serialize(archiveT & ar, setT & set, const unsigned int /*version*/)
00062       {
00063          // force 64 bit for portability
00064          boost::int64_t size = 0;
00065          ar.load_binary(&size , sizeof(size));
00066          set.resize(boost::numeric_cast<size_t>(size)); // make sure the cast back to size_t is safe
00067 
00068          typename setT::value_type v;
00069 
00070          while(size-- > 0)
00071          {
00072             ar >> v;
00073             set.insert(v);
00074          }
00075       }
00076    };
00077 }}
00078 
00079 
00080 namespace boost { // this really does have to be in the boost namespace for ADL to work properly :(
00081    namespace serialization {
00082 
00083       template<typename archiveT, typename Value, typename HashFcn, typename EqualKey, typename Alloc>
00084       void serialize(archiveT & ar, moost::container::sparse_hash_set<Value, HashFcn, EqualKey, Alloc> & set, const unsigned int version)
00085       {
00086          boost::mpl::if_<
00087             typename archiveT::is_saving,
00088             moost::serialization::hash_set_save__,
00089             moost::serialization::hash_set_load__
00090          >::type::serialize(ar, set, version);
00091       }
00092 
00093       template<typename archiveT, typename Value, typename HashFcn, typename EqualKey, typename Alloc>
00094       void serialize(archiveT & ar, moost::container::dense_hash_set<Value, HashFcn, EqualKey, Alloc> & set, const unsigned int version)
00095       {
00096          boost::mpl::if_<
00097             typename archiveT::is_saving,
00098             moost::serialization::hash_set_save__,
00099             moost::serialization::hash_set_load__
00100          >::type::serialize(ar, set, version);
00101       }
00102    }
00103 }
00104 
00105 
00106 #endif /// MOOST_SERIALIZATION_HASH_SET_SERIALISER_HPP__