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