object_fwd.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. //
  2. // MessagePack for C++ static resolution routine
  3. //
  4. // Copyright (C) 2008-2014 FURUHASHI Sadayuki and 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_OBJECT_FWD_HPP
  11. #define MSGPACK_V1_OBJECT_FWD_HPP
  12. #include "msgpack/object_fwd_decl.hpp"
  13. namespace msgpack {
  14. /// @cond
  15. MSGPACK_API_VERSION_NAMESPACE(v1) {
  16. /// @endcond
  17. struct object_array {
  18. uint32_t size;
  19. msgpack::object* ptr;
  20. };
  21. struct object_map {
  22. uint32_t size;
  23. msgpack::object_kv* ptr;
  24. };
  25. struct object_str {
  26. uint32_t size;
  27. const char* ptr;
  28. };
  29. struct object_bin {
  30. uint32_t size;
  31. const char* ptr;
  32. };
  33. struct object_ext {
  34. int8_t type() const { return ptr[0]; }
  35. const char* data() const { return &ptr[1]; }
  36. uint32_t size;
  37. const char* ptr;
  38. };
  39. #if !defined(MSGPACK_USE_CPP03)
  40. template <typename T>
  41. struct has_as {
  42. private:
  43. template <typename U>
  44. static auto check(U*) ->
  45. // Check v1 specialization
  46. typename std::is_same<
  47. decltype(adaptor::as<U>()(std::declval<msgpack::object>())),
  48. T
  49. >::type;
  50. template <typename...>
  51. static std::false_type check(...);
  52. public:
  53. using type = decltype(check<T>(MSGPACK_NULLPTR));
  54. static constexpr bool value = type::value;
  55. };
  56. #endif // !defined(MSGPACK_USE_CPP03)
  57. /// Object class that corresponding to MessagePack format object
  58. /**
  59. * See https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_object
  60. */
  61. struct object {
  62. union union_type {
  63. bool boolean;
  64. uint64_t u64;
  65. int64_t i64;
  66. #if defined(MSGPACK_USE_LEGACY_NAME_AS_FLOAT)
  67. MSGPACK_DEPRECATED("please use f64 instead")
  68. double dec; // obsolete
  69. #endif // MSGPACK_USE_LEGACY_NAME_AS_FLOAT
  70. double f64;
  71. msgpack::object_array array;
  72. msgpack::object_map map;
  73. msgpack::object_str str;
  74. msgpack::object_bin bin;
  75. msgpack::object_ext ext;
  76. };
  77. msgpack::type::object_type type;
  78. union_type via;
  79. /// Cheking nil
  80. /**
  81. * @return If the object is nil, then return true, else return false.
  82. */
  83. bool is_nil() const { return type == msgpack::type::NIL; }
  84. #if defined(MSGPACK_USE_CPP03)
  85. /// Get value as T
  86. /**
  87. * If the object can't be converted to T, msgpack::type_error would be thrown.
  88. * @tparam T The type you want to get.
  89. * @return The converted object.
  90. */
  91. template <typename T>
  92. T as() const;
  93. #else // defined(MSGPACK_USE_CPP03)
  94. /// Get value as T
  95. /**
  96. * If the object can't be converted to T, msgpack::type_error would be thrown.
  97. * @tparam T The type you want to get.
  98. * @return The converted object.
  99. */
  100. template <typename T>
  101. typename std::enable_if<msgpack::has_as<T>::value, T>::type as() const;
  102. /// Get value as T
  103. /**
  104. * If the object can't be converted to T, msgpack::type_error would be thrown.
  105. * @tparam T The type you want to get.
  106. * @return The converted object.
  107. */
  108. template <typename T>
  109. typename std::enable_if<!msgpack::has_as<T>::value, T>::type as() const;
  110. #endif // defined(MSGPACK_USE_CPP03)
  111. /// Convert the object
  112. /**
  113. * If the object can't be converted to T, msgpack::type_error would be thrown.
  114. * @tparam T The type of v.
  115. * @param v The value you want to get. `v` is output parameter. `v` is overwritten by converted value from the object.
  116. * @return The reference of `v`.
  117. */
  118. template <typename T>
  119. typename msgpack::enable_if<
  120. !msgpack::is_array<T>::value && !msgpack::is_pointer<T>::value,
  121. T&
  122. >::type
  123. convert(T& v) const;
  124. template <typename T, std::size_t N>
  125. T (&convert(T(&v)[N]) const)[N];
  126. #if !defined(MSGPACK_DISABLE_LEGACY_CONVERT)
  127. /// Convert the object (obsolete)
  128. /**
  129. * If the object can't be converted to T, msgpack::type_error would be thrown.
  130. * @tparam T The type of v.
  131. * @param v The pointer of the value you want to get. `v` is output parameter. `*v` is overwritten by converted value from the object.
  132. * @return The pointer of `v`.
  133. */
  134. template <typename T>
  135. MSGPACK_DEPRECATED("please use reference version instead")
  136. typename msgpack::enable_if<
  137. msgpack::is_pointer<T>::value,
  138. T
  139. >::type
  140. convert(T v) const;
  141. #endif // !defined(MSGPACK_DISABLE_LEGACY_CONVERT)
  142. /// Convert the object if not nil
  143. /**
  144. * If the object is not nil and can't be converted to T, msgpack::type_error would be thrown.
  145. * @tparam T The type of v.
  146. * @param v The value you want to get. `v` is output parameter. `v` is overwritten by converted value from the object if the object is not nil.
  147. * @return If the object is nil, then return false, else return true.
  148. */
  149. template <typename T>
  150. bool convert_if_not_nil(T& v) const;
  151. /// Default constructor. The object is set to nil.
  152. object();
  153. /// Copy constructor. Object is shallow copied.
  154. object(const msgpack_object& o);
  155. /// Construct object from T
  156. /**
  157. * If `v` is the type that is corresponding to MessegePack format str, bin, ext, array, or map,
  158. * you need to call `object(const T& v, msgpack::zone& z)` instead of this constructor.
  159. *
  160. * @tparam T The type of `v`.
  161. * @param v The value you want to convert.
  162. */
  163. template <typename T>
  164. explicit object(const T& v);
  165. /// Construct object from T
  166. /**
  167. * The object is constructed on the zone `z`.
  168. * See https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_object
  169. *
  170. * @tparam T The type of `v`.
  171. * @param v The value you want to convert.
  172. * @param z The zone that is used by the object.
  173. */
  174. template <typename T>
  175. object(const T& v, msgpack::zone& z);
  176. /// Construct object from T (obsolete)
  177. /**
  178. * The object is constructed on the zone `z`.
  179. * Use `object(const T& v, msgpack::zone& z)` instead of this constructor.
  180. * See https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_object
  181. *
  182. * @tparam T The type of `v`.
  183. * @param v The value you want to convert.
  184. * @param z The pointer to the zone that is used by the object.
  185. */
  186. template <typename T>
  187. MSGPACK_DEPRECATED("please use zone reference version instead of the pointer version")
  188. object(const T& v, msgpack::zone* z);
  189. template <typename T>
  190. object& operator=(const T& v);
  191. operator msgpack_object() const;
  192. struct with_zone;
  193. protected:
  194. struct implicit_type;
  195. public:
  196. implicit_type convert() const;
  197. };
  198. class type_error : public std::bad_cast { };
  199. struct object::implicit_type {
  200. implicit_type(object const& o) : obj(o) { }
  201. ~implicit_type() { }
  202. template <typename T>
  203. operator T();
  204. private:
  205. object const& obj;
  206. };
  207. /// @cond
  208. } // MSGPACK_API_VERSION_NAMESPACE(v1)
  209. /// @endcond
  210. } // namespace msgpack
  211. #endif // MSGPACK_V1_OBJECT_FWD_HPP