carray.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //
  2. // MessagePack for C++ static resolution routine
  3. //
  4. // Copyright (C) 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_CARRAY_HPP
  11. #define MSGPACK_V1_TYPE_CARRAY_HPP
  12. #include "msgpack/versioning.hpp"
  13. #include "msgpack/object_fwd.hpp"
  14. #include "msgpack/adaptor/adaptor_base.hpp"
  15. #include "msgpack/adaptor/check_container_size.hpp"
  16. namespace msgpack {
  17. /// @cond
  18. MSGPACK_API_VERSION_NAMESPACE(v1) {
  19. /// @endcond
  20. namespace adaptor {
  21. template <typename T, std::size_t N>
  22. struct convert<T[N]> {
  23. msgpack::object const& operator()(msgpack::object const& o, T* v) const {
  24. if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
  25. if (o.via.array.size > N) { throw msgpack::type_error(); }
  26. msgpack::object* p = o.via.array.ptr;
  27. msgpack::object* const pend = o.via.array.ptr + o.via.array.size;
  28. do {
  29. p->convert(*v);
  30. ++p;
  31. ++v;
  32. } while(p < pend);
  33. return o;
  34. }
  35. };
  36. template <std::size_t N>
  37. struct convert<char[N]> {
  38. msgpack::object const& operator()(msgpack::object const& o, char(&v)[N]) const {
  39. switch (o.type) {
  40. case msgpack::type::BIN:
  41. if (o.via.bin.size > N) { throw msgpack::type_error(); }
  42. std::memcpy(v, o.via.bin.ptr, o.via.bin.size);
  43. break;
  44. case msgpack::type::STR:
  45. if (o.via.str.size > N) { throw msgpack::type_error(); }
  46. std::memcpy(v, o.via.str.ptr, o.via.str.size);
  47. if (o.via.str.size < N) v[o.via.str.size] = '\0';
  48. break;
  49. default:
  50. throw msgpack::type_error();
  51. break;
  52. }
  53. return o;
  54. }
  55. };
  56. template <std::size_t N>
  57. struct convert<unsigned char[N]> {
  58. msgpack::object const& operator()(msgpack::object const& o, unsigned char(&v)[N]) const {
  59. switch (o.type) {
  60. case msgpack::type::BIN:
  61. if (o.via.bin.size > N) { throw msgpack::type_error(); }
  62. std::memcpy(v, o.via.bin.ptr, o.via.bin.size);
  63. break;
  64. case msgpack::type::STR:
  65. if (o.via.str.size > N) { throw msgpack::type_error(); }
  66. std::memcpy(v, o.via.str.ptr, o.via.str.size);
  67. if (o.via.str.size < N) v[o.via.str.size] = '\0';
  68. break;
  69. default:
  70. throw msgpack::type_error();
  71. break;
  72. }
  73. return o;
  74. }
  75. };
  76. template <typename T, std::size_t N>
  77. struct pack<T[N]> {
  78. template <typename Stream>
  79. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const T(&v)[N]) const {
  80. uint32_t size = checked_get_container_size(N);
  81. o.pack_array(size);
  82. const T* ptr = v;
  83. for (; ptr != &v[N]; ++ptr) o.pack(*ptr);
  84. return o;
  85. }
  86. };
  87. template <std::size_t N>
  88. struct pack<char[N]> {
  89. template <typename Stream>
  90. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const char(&v)[N]) const {
  91. char const* p = v;
  92. uint32_t size = checked_get_container_size(N);
  93. char const* p2 = static_cast<char const*>(std::memchr(p, '\0', size));
  94. uint32_t adjusted_size = p2 ? static_cast<uint32_t>(p2 - p) : size;
  95. o.pack_str(adjusted_size);
  96. o.pack_str_body(p, adjusted_size);
  97. return o;
  98. }
  99. };
  100. template <std::size_t N>
  101. struct pack<const char[N]> {
  102. template <typename Stream>
  103. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const char(&v)[N]) const {
  104. uint32_t size = checked_get_container_size(N);
  105. char const* p = v;
  106. char const* p2 = static_cast<char const*>(std::memchr(p, '\0', size));
  107. uint32_t adjusted_size = p2 ? static_cast<uint32_t>(p2 - p) : size;
  108. o.pack_str(adjusted_size);
  109. o.pack_str_body(p, adjusted_size);
  110. return o;
  111. }
  112. };
  113. template <std::size_t N>
  114. struct pack<unsigned char[N]> {
  115. template <typename Stream>
  116. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const unsigned char(&v)[N]) const {
  117. unsigned char const* p = v;
  118. uint32_t size = checked_get_container_size(N);
  119. o.pack_bin(size);
  120. o.pack_bin_body(reinterpret_cast<char const*>(p), size);
  121. return o;
  122. }
  123. };
  124. template <std::size_t N>
  125. struct pack<const unsigned char[N]> {
  126. template <typename Stream>
  127. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const unsigned char(&v)[N]) const {
  128. unsigned char const* p = v;
  129. uint32_t size = checked_get_container_size(N);
  130. o.pack_bin(size);
  131. o.pack_bin_body(reinterpret_cast<char const*>(p), size);
  132. return o;
  133. }
  134. };
  135. template <typename T, std::size_t N>
  136. struct object_with_zone<T[N]> {
  137. void operator()(msgpack::object::with_zone& o, const T(&v)[N]) const {
  138. uint32_t size = checked_get_container_size(N);
  139. o.type = msgpack::type::ARRAY;
  140. msgpack::object* ptr = static_cast<msgpack::object*>(o.zone.allocate_align(sizeof(msgpack::object) * size, MSGPACK_ZONE_ALIGNOF(msgpack::object)));
  141. o.via.array.ptr = ptr;
  142. o.via.array.size = size;
  143. const T* pv = v;
  144. for (; pv != &v[size]; ++pv) {
  145. *ptr++ = msgpack::object(*pv, o.zone);
  146. }
  147. }
  148. };
  149. template <std::size_t N>
  150. struct object_with_zone<char[N]> {
  151. void operator()(msgpack::object::with_zone& o, const char(&v)[N]) const {
  152. char const* p = v;
  153. uint32_t size = checked_get_container_size(N);
  154. char const* p2 = static_cast<char const*>(std::memchr(p, '\0', size));
  155. uint32_t adjusted_size = p2 ? static_cast<uint32_t>(p2 - p) : size;
  156. o.type = msgpack::type::STR;
  157. char* ptr = static_cast<char*>(o.zone.allocate_align(adjusted_size, MSGPACK_ZONE_ALIGNOF(char)));
  158. o.via.str.ptr = ptr;
  159. o.via.str.size = adjusted_size;
  160. std::memcpy(ptr, p, adjusted_size);
  161. }
  162. };
  163. template <std::size_t N>
  164. struct object_with_zone<const char[N]> {
  165. void operator()(msgpack::object::with_zone& o, const char(&v)[N]) const {
  166. char const* p = v;
  167. uint32_t size = checked_get_container_size(N);
  168. char const* p2 = static_cast<char const*>(std::memchr(p, '\0', size));
  169. uint32_t adjusted_size = p2 ? static_cast<uint32_t>(p2 - p) : size;
  170. o.type = msgpack::type::STR;
  171. char* ptr = static_cast<char*>(o.zone.allocate_align(adjusted_size, MSGPACK_ZONE_ALIGNOF(char)));
  172. o.via.str.ptr = ptr;
  173. o.via.str.size = adjusted_size;
  174. std::memcpy(ptr, p, adjusted_size);
  175. }
  176. };
  177. template <std::size_t N>
  178. struct object_with_zone<unsigned char[N]> {
  179. void operator()(msgpack::object::with_zone& o, const unsigned char(&v)[N]) const {
  180. uint32_t size = checked_get_container_size(N);
  181. o.type = msgpack::type::BIN;
  182. char* ptr = static_cast<char*>(o.zone.allocate_align(size, MSGPACK_ZONE_ALIGNOF(char)));
  183. o.via.bin.ptr = ptr;
  184. o.via.bin.size = size;
  185. std::memcpy(ptr, v, size);
  186. }
  187. };
  188. template <std::size_t N>
  189. struct object_with_zone<const unsigned char[N]> {
  190. void operator()(msgpack::object::with_zone& o, const unsigned char(&v)[N]) const {
  191. uint32_t size = checked_get_container_size(N);
  192. o.type = msgpack::type::BIN;
  193. char* ptr = static_cast<char*>(o.zone.allocate_align(size, MSGPACK_ZONE_ALIGNOF(char)));
  194. o.via.bin.ptr = ptr;
  195. o.via.bin.size = size;
  196. std::memcpy(ptr, v, size);
  197. }
  198. };
  199. template <std::size_t N>
  200. struct object<char[N]> {
  201. void operator()(msgpack::object& o, const char(&v)[N]) const {
  202. char const* p = v;
  203. uint32_t size = checked_get_container_size(N);
  204. char const* p2 = static_cast<char const*>(std::memchr(p, '\0', size));
  205. uint32_t adjusted_size = p2 ? static_cast<uint32_t>(p2 - p) : size;
  206. o.type = msgpack::type::STR;
  207. o.via.str.ptr = p;
  208. o.via.str.size = adjusted_size;
  209. }
  210. };
  211. template <std::size_t N>
  212. struct object<const char[N]> {
  213. void operator()(msgpack::object& o, const char(&v)[N]) const {
  214. char const* p = v;
  215. uint32_t size = checked_get_container_size(N);
  216. char const* p2 = static_cast<char const*>(std::memchr(p, '\0', size));
  217. uint32_t adjusted_size = p2 ? static_cast<uint32_t>(p2 - p) : size;
  218. o.type = msgpack::type::STR;
  219. o.via.str.ptr = p;
  220. o.via.str.size = adjusted_size;
  221. }
  222. };
  223. } // namespace adaptor
  224. /// @cond
  225. } // MSGPACK_API_VERSION_NAMESPACE(v1)
  226. /// @endcond
  227. } // namespace msgpack
  228. #endif // MSGPACK_V1_TYPE_CARRAY_HPP