hash_map.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #ifndef BOOST_SERIALIZATION_HASH_MAP_HPP
  2. #define BOOST_SERIALIZATION_HASH_MAP_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER)
  5. # pragma once
  6. #endif
  7. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  8. // hash_map.hpp: serialization for stl hash_map templates
  9. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  10. // Use, modification and distribution is subject to the Boost Software
  11. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. // See http://www.boost.org for updates, documentation, and revision history.
  14. #include <boost/config.hpp>
  15. #ifdef BOOST_HAS_HASH
  16. #include BOOST_HASH_MAP_HEADER
  17. #include <boost/serialization/utility.hpp> // pair
  18. #include <boost/serialization/hash_collections_save_imp.hpp>
  19. #include <boost/serialization/hash_collections_load_imp.hpp>
  20. #include <boost/serialization/split_free.hpp>
  21. #include <boost/serialization/detail/stack_constructor.hpp>
  22. #include <boost/move/utility_core.hpp>
  23. namespace boost {
  24. namespace serialization {
  25. namespace stl {
  26. // hash_map input
  27. template<class Archive, class Container>
  28. struct archive_input_hash_map
  29. {
  30. inline void operator()(
  31. Archive &ar,
  32. Container &s,
  33. const unsigned int v
  34. ){
  35. typedef typename Container::value_type type;
  36. detail::stack_construct<Archive, type> t(ar, v);
  37. // borland fails silently w/o full namespace
  38. ar >> boost::serialization::make_nvp("item", t.reference());
  39. std::pair<typename Container::const_iterator, bool> result =
  40. s.insert(boost::move(t.reference()));
  41. // note: the following presumes that the map::value_type was NOT tracked
  42. // in the archive. This is the usual case, but here there is no way
  43. // to determine that.
  44. if(result.second){
  45. ar.reset_object_address(
  46. & (result.first->second),
  47. & t.reference().second
  48. );
  49. }
  50. }
  51. };
  52. // hash_multimap input
  53. template<class Archive, class Container>
  54. struct archive_input_hash_multimap
  55. {
  56. inline void operator()(
  57. Archive &ar,
  58. Container &s,
  59. const unsigned int v
  60. ){
  61. typedef typename Container::value_type type;
  62. detail::stack_construct<Archive, type> t(ar, v);
  63. // borland fails silently w/o full namespace
  64. ar >> boost::serialization::make_nvp("item", t.reference());
  65. typename Container::const_iterator result
  66. = s.insert(boost::move(t.reference()));
  67. // note: the following presumes that the map::value_type was NOT tracked
  68. // in the archive. This is the usual case, but here there is no way
  69. // to determine that.
  70. ar.reset_object_address(
  71. & result->second,
  72. & t.reference()
  73. );
  74. }
  75. };
  76. } // stl
  77. template<
  78. class Archive,
  79. class Key,
  80. class T,
  81. class HashFcn,
  82. class EqualKey,
  83. class Allocator
  84. >
  85. inline void save(
  86. Archive & ar,
  87. const BOOST_STD_EXTENSION_NAMESPACE::hash_map<
  88. Key, T, HashFcn, EqualKey, Allocator
  89. > &t,
  90. const unsigned int file_version
  91. ){
  92. boost::serialization::stl::save_hash_collection<
  93. Archive,
  94. BOOST_STD_EXTENSION_NAMESPACE::hash_map<
  95. Key, T, HashFcn, EqualKey, Allocator
  96. >
  97. >(ar, t);
  98. }
  99. template<
  100. class Archive,
  101. class Key,
  102. class T,
  103. class HashFcn,
  104. class EqualKey,
  105. class Allocator
  106. >
  107. inline void load(
  108. Archive & ar,
  109. BOOST_STD_EXTENSION_NAMESPACE::hash_map<
  110. Key, T, HashFcn, EqualKey, Allocator
  111. > &t,
  112. const unsigned int file_version
  113. ){
  114. boost::serialization::stl::load_hash_collection<
  115. Archive,
  116. BOOST_STD_EXTENSION_NAMESPACE::hash_map<
  117. Key, T, HashFcn, EqualKey, Allocator
  118. >,
  119. boost::serialization::stl::archive_input_hash_map<
  120. Archive,
  121. BOOST_STD_EXTENSION_NAMESPACE::hash_map<
  122. Key, T, HashFcn, EqualKey, Allocator
  123. >
  124. >
  125. >(ar, t);
  126. }
  127. // split non-intrusive serialization function member into separate
  128. // non intrusive save/load member functions
  129. template<
  130. class Archive,
  131. class Key,
  132. class T,
  133. class HashFcn,
  134. class EqualKey,
  135. class Allocator
  136. >
  137. inline void serialize(
  138. Archive & ar,
  139. BOOST_STD_EXTENSION_NAMESPACE::hash_map<
  140. Key, T, HashFcn, EqualKey, Allocator
  141. > &t,
  142. const unsigned int file_version
  143. ){
  144. boost::serialization::split_free(ar, t, file_version);
  145. }
  146. // hash_multimap
  147. template<
  148. class Archive,
  149. class Key,
  150. class T,
  151. class HashFcn,
  152. class EqualKey,
  153. class Allocator
  154. >
  155. inline void save(
  156. Archive & ar,
  157. const BOOST_STD_EXTENSION_NAMESPACE::hash_multimap<
  158. Key, T, HashFcn, EqualKey, Allocator
  159. > &t,
  160. const unsigned int file_version
  161. ){
  162. boost::serialization::stl::save_hash_collection<
  163. Archive,
  164. BOOST_STD_EXTENSION_NAMESPACE::hash_multimap<
  165. Key, T, HashFcn, EqualKey, Allocator
  166. >
  167. >(ar, t);
  168. }
  169. template<
  170. class Archive,
  171. class Key,
  172. class T,
  173. class HashFcn,
  174. class EqualKey,
  175. class Allocator
  176. >
  177. inline void load(
  178. Archive & ar,
  179. BOOST_STD_EXTENSION_NAMESPACE::hash_multimap<
  180. Key, T, HashFcn, EqualKey, Allocator
  181. > &t,
  182. const unsigned int file_version
  183. ){
  184. boost::serialization::stl::load_hash_collection<
  185. Archive,
  186. BOOST_STD_EXTENSION_NAMESPACE::hash_multimap<
  187. Key, T, HashFcn, EqualKey, Allocator
  188. >,
  189. boost::serialization::stl::archive_input_hash_multimap<
  190. Archive,
  191. BOOST_STD_EXTENSION_NAMESPACE::hash_multimap<
  192. Key, T, HashFcn, EqualKey, Allocator
  193. >
  194. >
  195. >(ar, t);
  196. }
  197. // split non-intrusive serialization function member into separate
  198. // non intrusive save/load member functions
  199. template<
  200. class Archive,
  201. class Key,
  202. class T,
  203. class HashFcn,
  204. class EqualKey,
  205. class Allocator
  206. >
  207. inline void serialize(
  208. Archive & ar,
  209. BOOST_STD_EXTENSION_NAMESPACE::hash_multimap<
  210. Key, T, HashFcn, EqualKey, Allocator
  211. > &t,
  212. const unsigned int file_version
  213. ){
  214. boost::serialization::split_free(ar, t, file_version);
  215. }
  216. } // namespace serialization
  217. } // namespace boost
  218. #endif // BOOST_HAS_HASH
  219. #endif // BOOST_SERIALIZATION_HASH_MAP_HPP