QxSerialize_unordered_map.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /****************************************************************************
  2. **
  3. ** https://www.qxorm.com/
  4. ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
  5. **
  6. ** This file is part of the QxOrm library
  7. **
  8. ** This software is provided 'as-is', without any express or implied
  9. ** warranty. In no event will the authors be held liable for any
  10. ** damages arising from the use of this software
  11. **
  12. ** Commercial Usage
  13. ** Licensees holding valid commercial QxOrm licenses may use this file in
  14. ** accordance with the commercial license agreement provided with the
  15. ** Software or, alternatively, in accordance with the terms contained in
  16. ** a written agreement between you and Lionel Marty
  17. **
  18. ** GNU General Public License Usage
  19. ** Alternatively, this file may be used under the terms of the GNU
  20. ** General Public License version 3.0 as published by the Free Software
  21. ** Foundation and appearing in the file 'license.gpl3.txt' included in the
  22. ** packaging of this file. Please review the following information to
  23. ** ensure the GNU General Public License version 3.0 requirements will be
  24. ** met : http://www.gnu.org/copyleft/gpl.html
  25. **
  26. ** If you are unsure which license is appropriate for your use, or
  27. ** if you have questions regarding the use of this file, please contact :
  28. ** contact@qxorm.com
  29. **
  30. ****************************************************************************/
  31. #ifdef _QX_ENABLE_BOOST_SERIALIZATION
  32. #ifndef _QX_SERIALIZATION_BOOST_UNORDERED_MAP_H_
  33. #define _QX_SERIALIZATION_BOOST_UNORDERED_MAP_H_
  34. #ifdef _MSC_VER
  35. #pragma once
  36. #endif
  37. #include <boost/serialization/serialization.hpp>
  38. #include <boost/serialization/collections_save_imp.hpp>
  39. #include <boost/serialization/collections_load_imp.hpp>
  40. #include <boost/serialization/split_free.hpp>
  41. #include <boost/serialization/utility.hpp>
  42. #include <boost/serialization/nvp.hpp>
  43. namespace boost {
  44. namespace serialization {
  45. #if (BOOST_VERSION > 105700)
  46. template <class Archive, class Key, class Value>
  47. inline void save(Archive & ar, const boost::unordered_map<Key, Value> & t, const unsigned int /* file_version */)
  48. {
  49. long lSize = static_cast<long>(t.size());
  50. ar << boost::serialization::make_nvp("size", lSize);
  51. typedef typename boost::unordered_map<Key, Value>::const_iterator type_itr;
  52. for (type_itr itr = t.begin(); itr != t.end(); ++itr)
  53. {
  54. std::pair<Key, Value> pair_key_value = std::make_pair(itr->first, itr->second);
  55. ar << boost::serialization::make_nvp("item", pair_key_value);
  56. }
  57. }
  58. template <class Archive, class Key, class Value>
  59. inline void load(Archive & ar, boost::unordered_map<Key, Value> & t, const unsigned int /* file_version */)
  60. {
  61. long lSize = 0;
  62. ar >> boost::serialization::make_nvp("size", lSize);
  63. t.clear();
  64. t.reserve(lSize);
  65. std::pair<Key, Value> pair_key_value;
  66. for (long l = 0; l < lSize; l++)
  67. {
  68. ar >> boost::serialization::make_nvp("item", pair_key_value);
  69. t.insert(pair_key_value);
  70. }
  71. }
  72. #else // (BOOST_VERSION > 105700)
  73. template <class Archive, class Key, class Value>
  74. inline void save(Archive & ar, const boost::unordered_map<Key, Value> & t, const unsigned int /* file_version */)
  75. {
  76. boost::serialization::stl::save_collection< Archive, boost::unordered_map<Key, Value> >(ar, t);
  77. }
  78. template <class Archive, class Key, class Value>
  79. inline void load(Archive & ar, boost::unordered_map<Key, Value> & t, const unsigned int /* file_version */)
  80. {
  81. boost::serialization::stl::load_collection< Archive, boost::unordered_map<Key, Value>,
  82. boost::serialization::stl::archive_input_map< Archive, boost::unordered_map<Key, Value> >,
  83. boost::serialization::stl::no_reserve_imp< boost::unordered_map<Key, Value> > >(ar, t);
  84. }
  85. #endif // (BOOST_VERSION > 105700)
  86. template <class Archive, class Key, class Value>
  87. inline void serialize(Archive & ar, boost::unordered_map<Key, Value> & t, const unsigned int file_version)
  88. {
  89. boost::serialization::split_free(ar, t, file_version);
  90. }
  91. #if (BOOST_VERSION > 105700)
  92. template <class Archive, class Key, class Value>
  93. inline void save(Archive & ar, const boost::unordered_multimap<Key, Value> & t, const unsigned int /* file_version */)
  94. {
  95. long lSize = static_cast<long>(t.size());
  96. ar << boost::serialization::make_nvp("size", lSize);
  97. typedef typename boost::unordered_multimap<Key, Value>::const_iterator type_itr;
  98. for (type_itr itr = t.begin(); itr != t.end(); ++itr)
  99. {
  100. std::pair<Key, Value> pair_key_value = std::make_pair(itr->first, itr->second);
  101. ar << boost::serialization::make_nvp("item", pair_key_value);
  102. }
  103. }
  104. template <class Archive, class Key, class Value>
  105. inline void load(Archive & ar, boost::unordered_multimap<Key, Value> & t, const unsigned int /* file_version */)
  106. {
  107. long lSize = 0;
  108. ar >> boost::serialization::make_nvp("size", lSize);
  109. t.clear();
  110. t.reserve(lSize);
  111. std::pair<Key, Value> pair_key_value;
  112. for (long l = 0; l < lSize; l++)
  113. {
  114. ar >> boost::serialization::make_nvp("item", pair_key_value);
  115. t.insert(pair_key_value);
  116. }
  117. }
  118. #else // (BOOST_VERSION > 105700)
  119. template <class Archive, class Key, class Value>
  120. inline void save(Archive & ar, const boost::unordered_multimap<Key, Value> & t, const unsigned int /* file_version */)
  121. {
  122. boost::serialization::stl::save_collection< Archive, boost::unordered_multimap<Key, Value> >(ar, t);
  123. }
  124. template <class Archive, class Key, class Value>
  125. inline void load(Archive & ar, boost::unordered_multimap<Key, Value> & t, const unsigned int /* file_version */)
  126. {
  127. #if (BOOST_VERSION >= 104200)
  128. boost::serialization::stl::load_collection< Archive, boost::unordered_multimap<Key, Value>,
  129. boost::serialization::stl::archive_input_map< Archive, boost::unordered_multimap<Key, Value> >,
  130. boost::serialization::stl::no_reserve_imp< boost::unordered_multimap<Key, Value> > >(ar, t);
  131. #else // (BOOST_VERSION >= 104200)
  132. boost::serialization::stl::load_collection< Archive, boost::unordered_multimap<Key, Value>,
  133. boost::serialization::stl::archive_input_multimap< Archive, boost::unordered_multimap<Key, Value> >,
  134. boost::serialization::stl::no_reserve_imp< boost::unordered_multimap<Key, Value> > >(ar, t);
  135. #endif // (BOOST_VERSION >= 104200)
  136. }
  137. #endif // (BOOST_VERSION > 105700)
  138. template <class Archive, class Key, class Value>
  139. inline void serialize(Archive & ar, boost::unordered_multimap<Key, Value> & t, const unsigned int file_version)
  140. {
  141. boost::serialization::split_free(ar, t, file_version);
  142. }
  143. } // namespace serialization
  144. } // namespace boost
  145. #endif // _QX_SERIALIZATION_BOOST_UNORDERED_MAP_H_
  146. #endif // _QX_ENABLE_BOOST_SERIALIZATION