set.hpp 6.3 KB

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