libmoost
|
00001 /* vim:set ts=3 sw=3 sts=3 et: */ 00028 #ifndef MOOST_SERIALIZATION_HASH_MAP_SERIALISER_HPP__ 00029 #define MOOST_SERIALIZATION_HASH_MAP_SERIALISER_HPP__ 00030 00035 #include "../container/sparse_hash_map.hpp" 00036 #include "../container/dense_hash_map.hpp" 00037 00038 #include <boost/mpl/if.hpp> 00039 00040 namespace moost { namespace serialization { 00041 struct hash_map_save__ 00042 { 00043 template<typename archiveT, typename mapT> 00044 static void serialize(archiveT & ar, mapT const & map, const unsigned int /*version*/) 00045 { 00046 // force 64 bit for portability 00047 boost::int64_t size = map.size(); 00048 ar.save_binary(&size , sizeof(size)); 00049 00050 for(typename mapT::const_iterator itr = map.begin() ; itr != map.end() ; ++itr) 00051 { 00052 ar << itr->first; 00053 ar << itr->second; 00054 } 00055 } 00056 }; 00057 00058 struct hash_map_load__ 00059 { 00060 template<typename archiveT, typename mapT> 00061 static void serialize(archiveT & ar, mapT & map, 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 map.resize(boost::numeric_cast<size_t>(size)); // make sure the cast back to size_t is safe 00067 00068 typename mapT::key_type k; 00069 00070 while(size-- > 0) 00071 { 00072 ar >> k; 00073 ar >> map[k]; 00074 } 00075 } 00076 }; 00077 }} 00078 00079 namespace boost { // this really does have to be in the boost namespace for ADL to work properly :( 00080 namespace serialization { 00081 00082 template<typename archiveT, typename Key, typename Data, typename HashFcn, typename EqualKey, typename Alloc> 00083 void serialize(archiveT & ar, moost::container::sparse_hash_map<Key, Data, HashFcn, EqualKey, Alloc> & map, const unsigned int version) 00084 { 00085 boost::mpl::if_< 00086 typename archiveT::is_saving, 00087 moost::serialization::hash_map_save__, 00088 moost::serialization::hash_map_load__ 00089 >::type::serialize(ar, map, version); 00090 } 00091 00092 template<typename archiveT, typename Key, typename Data, typename HashFcn, typename EqualKey, typename Alloc> 00093 void serialize(archiveT & ar, moost::container::dense_hash_map<Key, Data, HashFcn, EqualKey, Alloc> & map, const unsigned int version) 00094 { 00095 boost::mpl::if_< 00096 typename archiveT::is_saving, 00097 moost::serialization::hash_map_save__, 00098 moost::serialization::hash_map_load__ 00099 >::type::serialize(ar, map, version); 00100 } 00101 } 00102 } 00103 00104 #endif /// MOOST_SERIALIZATION_HASH_MAP_SERIALISER_HPP__