ext.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //
  2. // MessagePack for C++ static resolution routine
  3. //
  4. // Copyright (C) 2015-2016 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_EXT_HPP
  11. #define MSGPACK_V1_TYPE_EXT_HPP
  12. #include "msgpack/v1/adaptor/ext_decl.hpp"
  13. #include "msgpack/adaptor/check_container_size.hpp"
  14. #include <cstring>
  15. #include <string>
  16. #include <cassert>
  17. namespace msgpack {
  18. /// @cond
  19. MSGPACK_API_VERSION_NAMESPACE(v1) {
  20. /// @endcond
  21. namespace type {
  22. class ext {
  23. public:
  24. ext() : m_data(1, 0) {}
  25. ext(int8_t t, const char* p, uint32_t s) {
  26. msgpack::detail::check_container_size_for_ext<sizeof(std::size_t)>(s);
  27. m_data.reserve(static_cast<std::size_t>(s) + 1);
  28. m_data.push_back(static_cast<char>(t));
  29. m_data.insert(m_data.end(), p, p + s);
  30. }
  31. ext(int8_t t, uint32_t s) {
  32. msgpack::detail::check_container_size_for_ext<sizeof(std::size_t)>(s);
  33. m_data.resize(static_cast<std::size_t>(s) + 1);
  34. m_data[0] = static_cast<char>(t);
  35. }
  36. ext(ext_ref const&);
  37. int8_t type() const {
  38. return static_cast<int8_t>(m_data[0]);
  39. }
  40. const char* data() const {
  41. return &m_data[0] + 1;
  42. }
  43. char* data() {
  44. return &m_data[0] + 1;
  45. }
  46. uint32_t size() const {
  47. return static_cast<uint32_t>(m_data.size()) - 1;
  48. }
  49. bool operator== (const ext& x) const {
  50. return m_data == x.m_data;
  51. }
  52. bool operator!= (const ext& x) const {
  53. return !(*this == x);
  54. }
  55. bool operator< (const ext& x) const {
  56. return m_data < x.m_data;
  57. }
  58. bool operator> (const ext& x) const {
  59. return m_data > x.m_data;
  60. }
  61. private:
  62. std::vector<char> m_data;
  63. friend class ext_ref;
  64. };
  65. } // namespace type
  66. namespace adaptor {
  67. template <>
  68. struct convert<msgpack::type::ext> {
  69. msgpack::object const& operator()(msgpack::object const& o, msgpack::type::ext& v) const {
  70. if(o.type != msgpack::type::EXT) {
  71. throw msgpack::type_error();
  72. }
  73. v = msgpack::type::ext(o.via.ext.type(), o.via.ext.data(), o.via.ext.size);
  74. return o;
  75. }
  76. };
  77. template <>
  78. struct pack<msgpack::type::ext> {
  79. template <typename Stream>
  80. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const msgpack::type::ext& v) const {
  81. // size limit has already been checked at ext's constructor
  82. uint32_t size = v.size();
  83. o.pack_ext(size, v.type());
  84. o.pack_ext_body(v.data(), size);
  85. return o;
  86. }
  87. };
  88. template <>
  89. struct object_with_zone<msgpack::type::ext> {
  90. void operator()(msgpack::object::with_zone& o, const msgpack::type::ext& v) const {
  91. // size limit has already been checked at ext's constructor
  92. uint32_t size = v.size();
  93. o.type = msgpack::type::EXT;
  94. char* ptr = static_cast<char*>(o.zone.allocate_align(size + 1, MSGPACK_ZONE_ALIGNOF(char)));
  95. o.via.ext.ptr = ptr;
  96. o.via.ext.size = size;
  97. ptr[0] = static_cast<char>(v.type());
  98. std::memcpy(ptr + 1, v.data(), size);
  99. }
  100. };
  101. } // namespace adaptor
  102. namespace type {
  103. class ext_ref {
  104. public:
  105. // ext_ref should be default constructible to support 'convert'.
  106. // A default constructed ext_ref object::m_ptr doesn't have the buffer to point to.
  107. // In order to avoid nullptr checking branches, m_ptr points to m_size.
  108. // So type() returns unspecified but valid value. It might be a zero because m_size
  109. // is initialized as zero, but shouldn't assume that.
  110. ext_ref() : m_ptr(static_cast<char*>(static_cast<void*>(&m_size))), m_size(0) {}
  111. ext_ref(const char* p, uint32_t s) :
  112. m_ptr(s == 0 ? static_cast<char*>(static_cast<void*>(&m_size)) : p),
  113. m_size(s == 0 ? 0 : s - 1) {
  114. msgpack::detail::check_container_size_for_ext<sizeof(std::size_t)>(s);
  115. }
  116. // size limit has already been checked at ext's constructor
  117. ext_ref(ext const& x) : m_ptr(&x.m_data[0]), m_size(x.size()) {}
  118. const char* data() const {
  119. return m_ptr + 1;
  120. }
  121. uint32_t size() const {
  122. return m_size;
  123. }
  124. int8_t type() const {
  125. return static_cast<int8_t>(m_ptr[0]);
  126. }
  127. std::string str() const {
  128. return std::string(m_ptr + 1, m_size);
  129. }
  130. bool operator== (const ext_ref& x) const {
  131. return m_size == x.m_size && std::memcmp(m_ptr, x.m_ptr, m_size) == 0;
  132. }
  133. bool operator!= (const ext_ref& x) const {
  134. return !(*this == x);
  135. }
  136. bool operator< (const ext_ref& x) const {
  137. if (m_size < x.m_size) return true;
  138. if (m_size > x.m_size) return false;
  139. return std::memcmp(m_ptr, x.m_ptr, m_size) < 0;
  140. }
  141. bool operator> (const ext_ref& x) const {
  142. if (m_size > x.m_size) return true;
  143. if (m_size < x.m_size) return false;
  144. return std::memcmp(m_ptr, x.m_ptr, m_size) > 0;
  145. }
  146. private:
  147. const char* m_ptr;
  148. uint32_t m_size;
  149. friend struct adaptor::object<msgpack::type::ext_ref>;
  150. };
  151. inline ext::ext(ext_ref const& x) {
  152. // size limit has already been checked at ext_ref's constructor
  153. m_data.reserve(x.size() + 1);
  154. m_data.push_back(x.type());
  155. m_data.insert(m_data.end(), x.data(), x.data() + x.size());
  156. }
  157. } // namespace type
  158. namespace adaptor {
  159. template <>
  160. struct convert<msgpack::type::ext_ref> {
  161. msgpack::object const& operator()(msgpack::object const& o, msgpack::type::ext_ref& v) const {
  162. if(o.type != msgpack::type::EXT) { throw msgpack::type_error(); }
  163. v = msgpack::type::ext_ref(o.via.ext.ptr, o.via.ext.size + 1);
  164. return o;
  165. }
  166. };
  167. template <>
  168. struct pack<msgpack::type::ext_ref> {
  169. template <typename Stream>
  170. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const msgpack::type::ext_ref& v) const {
  171. // size limit has already been checked at ext_ref's constructor
  172. uint32_t size = v.size();
  173. o.pack_ext(size, v.type());
  174. o.pack_ext_body(v.data(), size);
  175. return o;
  176. }
  177. };
  178. template <>
  179. struct object<msgpack::type::ext_ref> {
  180. void operator()(msgpack::object& o, const msgpack::type::ext_ref& v) const {
  181. // size limit has already been checked at ext_ref's constructor
  182. uint32_t size = v.size();
  183. o.type = msgpack::type::EXT;
  184. o.via.ext.ptr = v.m_ptr;
  185. o.via.ext.size = size;
  186. }
  187. };
  188. template <>
  189. struct object_with_zone<msgpack::type::ext_ref> {
  190. void operator()(msgpack::object::with_zone& o, const msgpack::type::ext_ref& v) const {
  191. static_cast<msgpack::object&>(o) << v;
  192. }
  193. };
  194. } // namespace adaptor
  195. /// @cond
  196. } // MSGPACK_API_VERSION_NAMESPACE(v1)
  197. /// @endcond
  198. } // namespace msgpack
  199. #endif // MSGPACK_V1_TYPE_EXT_HPP