object_fwd.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 static_cast<int8_t>(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. struct object_with_zone_type;
  58. /// Object class that corresponding to MessagePack format object
  59. /**
  60. * See https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_object
  61. */
  62. struct object {
  63. union union_type {
  64. bool boolean;
  65. uint64_t u64;
  66. int64_t i64;
  67. #if defined(MSGPACK_USE_LEGACY_NAME_AS_FLOAT)
  68. MSGPACK_DEPRECATED("please use f64 instead")
  69. double dec; // obsolete
  70. #endif // MSGPACK_USE_LEGACY_NAME_AS_FLOAT
  71. double f64;
  72. msgpack::object_array array;
  73. msgpack::object_map map;
  74. msgpack::object_str str;
  75. msgpack::object_bin bin;
  76. msgpack::object_ext ext;
  77. };
  78. msgpack::type::object_type type;
  79. union_type via;
  80. /// Cheking nil
  81. /**
  82. * @return If the object is nil, then return true, else return false.
  83. */
  84. bool is_nil() const { return type == msgpack::type::NIL; }
  85. #if defined(MSGPACK_USE_CPP03)
  86. /// Get value as T
  87. /**
  88. * If the object can't be converted to T, msgpack::type_error would be thrown.
  89. * @tparam T The type you want to get.
  90. * @return The converted object.
  91. */
  92. template <typename T>
  93. T as() const;
  94. #else // defined(MSGPACK_USE_CPP03)
  95. /// Get value as T
  96. /**
  97. * If the object can't be converted to T, msgpack::type_error would be thrown.
  98. * @tparam T The type you want to get.
  99. * @return The converted object.
  100. */
  101. template <typename T>
  102. typename std::enable_if<msgpack::has_as<T>::value, T>::type as() const;
  103. /// Get value as T
  104. /**
  105. * If the object can't be converted to T, msgpack::type_error would be thrown.
  106. * @tparam T The type you want to get.
  107. * @return The converted object.
  108. */
  109. template <typename T>
  110. typename std::enable_if<!msgpack::has_as<T>::value, T>::type as() const;
  111. #endif // defined(MSGPACK_USE_CPP03)
  112. /// Convert the object
  113. /**
  114. * If the object can't be converted to T, msgpack::type_error would be thrown.
  115. * @tparam T The type of v.
  116. * @param v The value you want to get. `v` is output parameter. `v` is overwritten by converted value from the object.
  117. * @return The reference of `v`.
  118. */
  119. template <typename T>
  120. typename msgpack::enable_if<
  121. !msgpack::is_array<T>::value && !msgpack::is_pointer<T>::value,
  122. T&
  123. >::type
  124. convert(T& v) const;
  125. template <typename T, std::size_t N>
  126. T (&convert(T(&v)[N]) const)[N];
  127. #if !defined(MSGPACK_DISABLE_LEGACY_CONVERT)
  128. /// Convert the object (obsolete)
  129. /**
  130. * If the object can't be converted to T, msgpack::type_error would be thrown.
  131. * @tparam T The type of v.
  132. * @param v The pointer of the value you want to get. `v` is output parameter. `*v` is overwritten by converted value from the object.
  133. * @return The pointer of `v`.
  134. */
  135. template <typename T>
  136. MSGPACK_DEPRECATED("please use reference version instead")
  137. typename msgpack::enable_if<
  138. msgpack::is_pointer<T>::value,
  139. T
  140. >::type
  141. convert(T v) const;
  142. #endif // !defined(MSGPACK_DISABLE_LEGACY_CONVERT)
  143. /// Convert the object if not nil
  144. /**
  145. * If the object is not nil and can't be converted to T, msgpack::type_error would be thrown.
  146. * @tparam T The type of v.
  147. * @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.
  148. * @return If the object is nil, then return false, else return true.
  149. */
  150. template <typename T>
  151. bool convert_if_not_nil(T& v) const;
  152. /// Default constructor. The object is set to nil.
  153. object();
  154. /// Construct object from T
  155. /**
  156. * If `v` is the type that is corresponding to MessegePack format str, bin, ext, array, or map,
  157. * you need to call `object(const T& v, msgpack::zone& z)` instead of this constructor.
  158. *
  159. * @tparam T The type of `v`.
  160. * @param v The value you want to convert.
  161. */
  162. template <typename T>
  163. explicit object(const T& v);
  164. /// Construct object from T
  165. /**
  166. * The object is constructed on the zone `z`.
  167. * See https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_object
  168. *
  169. * @tparam T The type of `v`.
  170. * @param v The value you want to convert.
  171. * @param z The zone that is used by the object.
  172. */
  173. template <typename T>
  174. object(const T& v, msgpack::zone& z);
  175. /// Construct object from T (obsolete)
  176. /**
  177. * The object is constructed on the zone `z`.
  178. * Use `object(const T& v, msgpack::zone& z)` instead of this constructor.
  179. * See https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_object
  180. *
  181. * @tparam T The type of `v`.
  182. * @param v The value you want to convert.
  183. * @param z The pointer to the zone that is used by the object.
  184. */
  185. template <typename T>
  186. MSGPACK_DEPRECATED("please use zone reference version instead of the pointer version")
  187. object(const T& v, msgpack::zone* z);
  188. template <typename T>
  189. object& operator=(const T& v);
  190. // Not a nested struct (i.e. 'struct with_zone;') to work around MSVC C++20 modules error C2504
  191. typedef object_with_zone_type with_zone;
  192. protected:
  193. struct implicit_type;
  194. public:
  195. implicit_type convert() const;
  196. };
  197. class type_error : public std::bad_cast { };
  198. struct object::implicit_type {
  199. implicit_type(object const& o) : obj(o) { }
  200. ~implicit_type() { }
  201. template <typename T>
  202. operator T();
  203. private:
  204. object const& obj;
  205. };
  206. /// @cond
  207. } // MSGPACK_API_VERSION_NAMESPACE(v1)
  208. /// @endcond
  209. } // namespace msgpack
  210. #endif // MSGPACK_V1_OBJECT_FWD_HPP