set.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // MessagePack for C++ static resolution routine
  3. //
  4. // Copyright (C) 2008-2015 FURUHASHI Sadayuki
  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_SET_HPP
  11. #define MSGPACK_V1_TYPE_SET_HPP
  12. #include "msgpack/versioning.hpp"
  13. #include "msgpack/cpp_version.hpp"
  14. #include "msgpack/adaptor/adaptor_base.hpp"
  15. #include "msgpack/adaptor/check_container_size.hpp"
  16. #include <set>
  17. namespace msgpack {
  18. /// @cond
  19. MSGPACK_API_VERSION_NAMESPACE(v1) {
  20. /// @endcond
  21. namespace adaptor {
  22. #if !defined(MSGPACK_USE_CPP03)
  23. template <typename T, typename Compare, typename Alloc>
  24. struct as<std::set<T, Compare, Alloc>, typename std::enable_if<msgpack::has_as<T>::value>::type> {
  25. std::set<T, Compare, Alloc> operator()(msgpack::object const& o) const {
  26. if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
  27. msgpack::object* p = o.via.array.ptr + o.via.array.size;
  28. msgpack::object* const pbegin = o.via.array.ptr;
  29. std::set<T, Compare, Alloc> v;
  30. while (p > pbegin) {
  31. --p;
  32. v.insert(p->as<T>());
  33. }
  34. return v;
  35. }
  36. };
  37. #endif // !defined(MSGPACK_USE_CPP03)
  38. template <typename T, typename Compare, typename Alloc>
  39. struct convert<std::set<T, Compare, Alloc> > {
  40. msgpack::object const& operator()(msgpack::object const& o, std::set<T, Compare, Alloc>& v) const {
  41. if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
  42. msgpack::object* p = o.via.array.ptr + o.via.array.size;
  43. msgpack::object* const pbegin = o.via.array.ptr;
  44. std::set<T, Compare, Alloc> tmp;
  45. while (p > pbegin) {
  46. --p;
  47. tmp.insert(p->as<T>());
  48. }
  49. #if MSGPACK_CPP_VERSION >= 201103L
  50. v = std::move(tmp);
  51. #else
  52. tmp.swap(v);
  53. #endif
  54. return o;
  55. }
  56. };
  57. template <typename T, typename Compare, typename Alloc>
  58. struct pack<std::set<T, Compare, Alloc> > {
  59. template <typename Stream>
  60. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::set<T, Compare, Alloc>& v) const {
  61. uint32_t size = checked_get_container_size(v.size());
  62. o.pack_array(size);
  63. for (typename std::set<T, Compare, Alloc>::const_iterator it(v.begin()), it_end(v.end());
  64. it != it_end; ++it) {
  65. o.pack(*it);
  66. }
  67. return o;
  68. }
  69. };
  70. template <typename T, typename Compare, typename Alloc>
  71. struct object_with_zone<std::set<T, Compare, Alloc> > {
  72. void operator()(msgpack::object::with_zone& o, const std::set<T, Compare, Alloc>& v) const {
  73. o.type = msgpack::type::ARRAY;
  74. if (v.empty()) {
  75. o.via.array.ptr = MSGPACK_NULLPTR;
  76. o.via.array.size = 0;
  77. }
  78. else {
  79. uint32_t size = checked_get_container_size(v.size());
  80. msgpack::object* p = static_cast<msgpack::object*>(o.zone.allocate_align(sizeof(msgpack::object)*size, MSGPACK_ZONE_ALIGNOF(msgpack::object)));
  81. msgpack::object* const pend = p + size;
  82. o.via.array.ptr = p;
  83. o.via.array.size = size;
  84. typename std::set<T, Compare, Alloc>::const_iterator it(v.begin());
  85. do {
  86. *p = msgpack::object(*it, o.zone);
  87. ++p;
  88. ++it;
  89. } while(p < pend);
  90. }
  91. }
  92. };
  93. #if !defined(MSGPACK_USE_CPP03)
  94. template <typename T, typename Compare, typename Alloc>
  95. struct as<std::multiset<T, Compare, Alloc>, typename std::enable_if<msgpack::has_as<T>::value>::type> {
  96. std::multiset<T, Compare, Alloc> operator()(msgpack::object const& o) const {
  97. if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
  98. msgpack::object* p = o.via.array.ptr + o.via.array.size;
  99. msgpack::object* const pbegin = o.via.array.ptr;
  100. std::multiset<T, Compare, Alloc> v;
  101. while (p > pbegin) {
  102. --p;
  103. v.insert(p->as<T>());
  104. }
  105. return v;
  106. }
  107. };
  108. #endif // !defined(MSGPACK_USE_CPP03)
  109. template <typename T, typename Compare, typename Alloc>
  110. struct convert<std::multiset<T, Compare, Alloc> > {
  111. msgpack::object const& operator()(msgpack::object const& o, std::multiset<T, Compare, Alloc>& v) const {
  112. if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
  113. msgpack::object* p = o.via.array.ptr + o.via.array.size;
  114. msgpack::object* const pbegin = o.via.array.ptr;
  115. std::multiset<T, Compare, Alloc> tmp;
  116. while (p > pbegin) {
  117. --p;
  118. tmp.insert(p->as<T>());
  119. }
  120. #if MSGPACK_CPP_VERSION >= 201103L
  121. v = std::move(tmp);
  122. #else
  123. tmp.swap(v);
  124. #endif
  125. return o;
  126. }
  127. };
  128. template <typename T, typename Compare, typename Alloc>
  129. struct pack<std::multiset<T, Compare, Alloc> > {
  130. template <typename Stream>
  131. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::multiset<T, Compare, Alloc>& v) const {
  132. uint32_t size = checked_get_container_size(v.size());
  133. o.pack_array(size);
  134. for (typename std::multiset<T, Compare, Alloc>::const_iterator it(v.begin()), it_end(v.end());
  135. it != it_end; ++it) {
  136. o.pack(*it);
  137. }
  138. return o;
  139. }
  140. };
  141. template <typename T, typename Compare, typename Alloc>
  142. struct object_with_zone<std::multiset<T, Compare, Alloc> > {
  143. void operator()(msgpack::object::with_zone& o, const std::multiset<T, Compare, Alloc>& v) const {
  144. o.type = msgpack::type::ARRAY;
  145. if (v.empty()) {
  146. o.via.array.ptr = MSGPACK_NULLPTR;
  147. o.via.array.size = 0;
  148. } else {
  149. uint32_t size = checked_get_container_size(v.size());
  150. msgpack::object* p = static_cast<msgpack::object*>(o.zone.allocate_align(sizeof(msgpack::object)*size, MSGPACK_ZONE_ALIGNOF(msgpack::object)));
  151. msgpack::object* const pend = p + size;
  152. o.via.array.ptr = p;
  153. o.via.array.size = size;
  154. typename std::multiset<T, Compare, Alloc>::const_iterator it(v.begin());
  155. do {
  156. *p = msgpack::object(*it, o.zone);
  157. ++p;
  158. ++it;
  159. } while(p < pend);
  160. }
  161. }
  162. };
  163. } // namespace adaptor
  164. /// @cond
  165. } // MSGPACK_API_VERSION_NAMESPACE(v1)
  166. /// @endcond
  167. } // namespace msgpack
  168. #endif // MSGPACK_V1_TYPE_SET_HPP