map.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. //
  2. // MessagePack for C++ static resolution routine
  3. //
  4. // Copyright (C) 2008-2016 FURUHASHI Sadayuki and KONDO Takatoshi
  5. //
  6. // Distributed under the Boost Software License, Version 1.0.
  7. // (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef MSGPACK_V1_TYPE_MAP_HPP
  11. #define MSGPACK_V1_TYPE_MAP_HPP
  12. #include "msgpack/versioning.hpp"
  13. #include "msgpack/cpp_version.hpp"
  14. #include "msgpack/v1/adaptor/map_decl.hpp"
  15. #include "msgpack/adaptor/adaptor_base.hpp"
  16. #include <map>
  17. #include <vector>
  18. namespace msgpack {
  19. /// @cond
  20. MSGPACK_API_VERSION_NAMESPACE(v1) {
  21. /// @endcond
  22. namespace type {
  23. template <typename K, typename V, typename Compare, typename Alloc>
  24. class assoc_vector : public std::vector< std::pair<K, V>, Alloc > {
  25. #if !defined(MSGPACK_USE_CPP03)
  26. using std::vector<std::pair<K, V>, Alloc>::vector;
  27. #endif // !defined(MSGPACK_USE_CPP03)
  28. };
  29. namespace detail {
  30. template <typename K, typename V, typename Compare, typename Alloc>
  31. struct pair_first_less {
  32. bool operator() (const std::pair<K, V>& x, const std::pair<K, V>& y) const
  33. { return Compare()(x.first, y.first); }
  34. };
  35. }
  36. } //namespace type
  37. namespace adaptor {
  38. #if !defined(MSGPACK_USE_CPP03)
  39. template <typename K, typename V, typename Compare, typename Alloc>
  40. struct as<
  41. type::assoc_vector<K, V, Compare, Alloc>,
  42. typename std::enable_if<msgpack::has_as<K>::value || msgpack::has_as<V>::value>::type> {
  43. type::assoc_vector<K, V, Compare, Alloc> operator()(msgpack::object const& o) const {
  44. if (o.type != msgpack::type::MAP) { throw msgpack::type_error(); }
  45. type::assoc_vector<K, V, Compare, Alloc> v;
  46. v.reserve(o.via.map.size);
  47. msgpack::object_kv* p = o.via.map.ptr;
  48. msgpack::object_kv* const pend = o.via.map.ptr + o.via.map.size;
  49. for (; p < pend; ++p) {
  50. v.emplace_back(p->key.as<K>(), p->val.as<V>());
  51. }
  52. std::sort(v.begin(), v.end(), type::detail::pair_first_less<K, V, Compare, Alloc>());
  53. return v;
  54. }
  55. };
  56. #endif // !defined(MSGPACK_USE_CPP03)
  57. template <typename K, typename V, typename Compare, typename Alloc>
  58. struct convert<type::assoc_vector<K, V, Compare, Alloc> > {
  59. msgpack::object const& operator()(msgpack::object const& o, type::assoc_vector<K, V, Compare, Alloc>& v) const {
  60. if (o.type != msgpack::type::MAP) { throw msgpack::type_error(); }
  61. v.resize(o.via.map.size);
  62. if (o.via.map.size != 0) {
  63. msgpack::object_kv* p = o.via.map.ptr;
  64. msgpack::object_kv* const pend = o.via.map.ptr + o.via.map.size;
  65. std::pair<K, V>* it(&v.front());
  66. for (; p < pend; ++p, ++it) {
  67. p->key.convert(it->first);
  68. p->val.convert(it->second);
  69. }
  70. std::sort(v.begin(), v.end(), type::detail::pair_first_less<K, V, Compare, Alloc>());
  71. }
  72. return o;
  73. }
  74. };
  75. template <typename K, typename V, typename Compare, typename Alloc>
  76. struct pack<type::assoc_vector<K, V, Compare, Alloc> > {
  77. template <typename Stream>
  78. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const type::assoc_vector<K, V, Compare, Alloc>& v) const {
  79. uint32_t size = checked_get_container_size(v.size());
  80. o.pack_map(size);
  81. for (typename type::assoc_vector<K, V, Compare, Alloc>::const_iterator it(v.begin()), it_end(v.end());
  82. it != it_end; ++it) {
  83. o.pack(it->first);
  84. o.pack(it->second);
  85. }
  86. return o;
  87. }
  88. };
  89. template <typename K, typename V, typename Compare, typename Alloc>
  90. struct object_with_zone<type::assoc_vector<K, V, Compare, Alloc> > {
  91. void operator()(msgpack::object::with_zone& o, const type::assoc_vector<K, V, Compare, Alloc>& v) const {
  92. o.type = msgpack::type::MAP;
  93. if (v.empty()) {
  94. o.via.map.ptr = MSGPACK_NULLPTR;
  95. o.via.map.size = 0;
  96. }
  97. else {
  98. uint32_t size = checked_get_container_size(v.size());
  99. msgpack::object_kv* p = static_cast<msgpack::object_kv*>(o.zone.allocate_align(sizeof(msgpack::object_kv)*size, MSGPACK_ZONE_ALIGNOF(msgpack::object_kv)));
  100. msgpack::object_kv* const pend = p + size;
  101. o.via.map.ptr = p;
  102. o.via.map.size = size;
  103. typename type::assoc_vector<K, V, Compare, Alloc>::const_iterator it(v.begin());
  104. do {
  105. p->key = msgpack::object(it->first, o.zone);
  106. p->val = msgpack::object(it->second, o.zone);
  107. ++p;
  108. ++it;
  109. } while(p < pend);
  110. }
  111. }
  112. };
  113. #if !defined(MSGPACK_USE_CPP03)
  114. template <typename K, typename V, typename Compare, typename Alloc>
  115. struct as<
  116. std::map<K, V, Compare, Alloc>,
  117. typename std::enable_if<msgpack::has_as<K>::value || msgpack::has_as<V>::value>::type> {
  118. std::map<K, V, Compare, Alloc> operator()(msgpack::object const& o) const {
  119. if (o.type != msgpack::type::MAP) { throw msgpack::type_error(); }
  120. msgpack::object_kv* p(o.via.map.ptr);
  121. msgpack::object_kv* const pend(o.via.map.ptr + o.via.map.size);
  122. std::map<K, V, Compare, Alloc> v;
  123. for (; p != pend; ++p) {
  124. v.emplace(p->key.as<K>(), p->val.as<V>());
  125. }
  126. return v;
  127. }
  128. };
  129. #endif // !defined(MSGPACK_USE_CPP03)
  130. template <typename K, typename V, typename Compare, typename Alloc>
  131. struct convert<std::map<K, V, Compare, Alloc> > {
  132. msgpack::object const& operator()(msgpack::object const& o, std::map<K, V, Compare, Alloc>& v) const {
  133. if (o.type != msgpack::type::MAP) { throw msgpack::type_error(); }
  134. msgpack::object_kv* p(o.via.map.ptr);
  135. msgpack::object_kv* const pend(o.via.map.ptr + o.via.map.size);
  136. std::map<K, V, Compare, Alloc> tmp;
  137. for (; p != pend; ++p) {
  138. K key;
  139. p->key.convert(key);
  140. #if MSGPACK_CPP_VERSION >= 201103L
  141. p->val.convert(tmp[std::move(key)]);
  142. #else
  143. p->val.convert(tmp[key]);
  144. #endif
  145. }
  146. #if MSGPACK_CPP_VERSION >= 201103L
  147. v = std::move(tmp);
  148. #else
  149. tmp.swap(v);
  150. #endif
  151. return o;
  152. }
  153. };
  154. template <typename K, typename V, typename Compare, typename Alloc>
  155. struct pack<std::map<K, V, Compare, Alloc> > {
  156. template <typename Stream>
  157. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::map<K, V, Compare, Alloc>& v) const {
  158. uint32_t size = checked_get_container_size(v.size());
  159. o.pack_map(size);
  160. for (typename std::map<K, V, Compare, Alloc>::const_iterator it(v.begin()), it_end(v.end());
  161. it != it_end; ++it) {
  162. o.pack(it->first);
  163. o.pack(it->second);
  164. }
  165. return o;
  166. }
  167. };
  168. template <typename K, typename V, typename Compare, typename Alloc>
  169. struct object_with_zone<std::map<K, V, Compare, Alloc> > {
  170. void operator()(msgpack::object::with_zone& o, const std::map<K, V, Compare, Alloc>& v) const {
  171. o.type = msgpack::type::MAP;
  172. if (v.empty()) {
  173. o.via.map.ptr = MSGPACK_NULLPTR;
  174. o.via.map.size = 0;
  175. }
  176. else {
  177. uint32_t size = checked_get_container_size(v.size());
  178. msgpack::object_kv* p = static_cast<msgpack::object_kv*>(o.zone.allocate_align(sizeof(msgpack::object_kv)*size, MSGPACK_ZONE_ALIGNOF(msgpack::object_kv)));
  179. msgpack::object_kv* const pend = p + size;
  180. o.via.map.ptr = p;
  181. o.via.map.size = size;
  182. typename std::map<K, V, Compare, Alloc>::const_iterator it(v.begin());
  183. do {
  184. #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
  185. #pragma GCC diagnostic push
  186. #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
  187. #endif // defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
  188. p->key = msgpack::object(it->first, o.zone);
  189. p->val = msgpack::object(it->second, o.zone);
  190. #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
  191. #pragma GCC diagnostic pop
  192. #endif // defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
  193. ++p;
  194. ++it;
  195. } while(p < pend);
  196. }
  197. }
  198. };
  199. #if !defined(MSGPACK_USE_CPP03)
  200. template <typename K, typename V, typename Compare, typename Alloc>
  201. struct as<
  202. std::multimap<K, V, Compare, Alloc>,
  203. typename std::enable_if<msgpack::has_as<K>::value || msgpack::has_as<V>::value>::type> {
  204. std::multimap<K, V, Compare, Alloc> operator()(msgpack::object const& o) const {
  205. if (o.type != msgpack::type::MAP) { throw msgpack::type_error(); }
  206. msgpack::object_kv* p(o.via.map.ptr);
  207. msgpack::object_kv* const pend(o.via.map.ptr + o.via.map.size);
  208. std::multimap<K, V, Compare, Alloc> v;
  209. for (; p != pend; ++p) {
  210. v.emplace(p->key.as<K>(), p->val.as<V>());
  211. }
  212. return v;
  213. }
  214. };
  215. #endif // !defined(MSGPACK_USE_CPP03)
  216. template <typename K, typename V, typename Compare, typename Alloc>
  217. struct convert<std::multimap<K, V, Compare, Alloc> > {
  218. msgpack::object const& operator()(msgpack::object const& o, std::multimap<K, V, Compare, Alloc>& v) const {
  219. if (o.type != msgpack::type::MAP) { throw msgpack::type_error(); }
  220. msgpack::object_kv* p(o.via.map.ptr);
  221. msgpack::object_kv* const pend(o.via.map.ptr + o.via.map.size);
  222. std::multimap<K, V, Compare, Alloc> tmp;
  223. for (; p != pend; ++p) {
  224. std::pair<K, V> value;
  225. p->key.convert(value.first);
  226. p->val.convert(value.second);
  227. #if MSGPACK_CPP_VERSION >= 201103L
  228. tmp.insert(std::move(value));
  229. #else
  230. tmp.insert(value);
  231. #endif
  232. }
  233. #if MSGPACK_CPP_VERSION >= 201103L
  234. v = std::move(tmp);
  235. #else
  236. tmp.swap(v);
  237. #endif
  238. return o;
  239. }
  240. };
  241. template <typename K, typename V, typename Compare, typename Alloc>
  242. struct pack<std::multimap<K, V, Compare, Alloc> > {
  243. template <typename Stream>
  244. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::multimap<K, V, Compare, Alloc>& v) const {
  245. uint32_t size = checked_get_container_size(v.size());
  246. o.pack_map(size);
  247. for (typename std::multimap<K, V, Compare, Alloc>::const_iterator it(v.begin()), it_end(v.end());
  248. it != it_end; ++it) {
  249. o.pack(it->first);
  250. o.pack(it->second);
  251. }
  252. return o;
  253. }
  254. };
  255. template <typename K, typename V, typename Compare, typename Alloc>
  256. struct object_with_zone<std::multimap<K, V, Compare, Alloc> > {
  257. void operator()(msgpack::object::with_zone& o, const std::multimap<K, V, Compare, Alloc>& v) const {
  258. o.type = msgpack::type::MAP;
  259. if (v.empty()) {
  260. o.via.map.ptr = MSGPACK_NULLPTR;
  261. o.via.map.size = 0;
  262. }
  263. else {
  264. uint32_t size = checked_get_container_size(v.size());
  265. msgpack::object_kv* p = static_cast<msgpack::object_kv*>(o.zone.allocate_align(sizeof(msgpack::object_kv)*size, MSGPACK_ZONE_ALIGNOF(msgpack::object_kv)));
  266. msgpack::object_kv* const pend = p + size;
  267. o.via.map.ptr = p;
  268. o.via.map.size = size;
  269. typename std::multimap<K, V, Compare, Alloc>::const_iterator it(v.begin());
  270. do {
  271. p->key = msgpack::object(it->first, o.zone);
  272. p->val = msgpack::object(it->second, o.zone);
  273. ++p;
  274. ++it;
  275. } while(p < pend);
  276. }
  277. }
  278. };
  279. } // namespace adaptor
  280. /// @cond
  281. } // MSGPACK_API_VERSION_NAMESPACE(v1)
  282. /// @endcond
  283. } // namespace msgpack
  284. #endif // MSGPACK_V1_TYPE_MAP_HPP