object_fwd.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // MessagePack for C++ static resolution routine
  3. //
  4. // Copyright (C) 2008-2016 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_V2_OBJECT_FWD_HPP
  11. #define MSGPACK_V2_OBJECT_FWD_HPP
  12. #include "msgpack/v2/object_fwd_decl.hpp"
  13. #include "msgpack/object_fwd.hpp"
  14. namespace msgpack {
  15. /// @cond
  16. MSGPACK_API_VERSION_NAMESPACE(v2) {
  17. /// @endcond
  18. struct object : v1::object {
  19. object() {}
  20. object(v1::object const& o):v1::object(o) {}
  21. /// Construct object from T
  22. /**
  23. * If `v` is the type that is corresponding to MessegePack format str, bin, ext, array, or map,
  24. * you need to call `object(const T& v, msgpack::zone& z)` instead of this constructor.
  25. *
  26. * @tparam T The type of `v`.
  27. * @param v The value you want to convert.
  28. */
  29. template <typename T>
  30. explicit object(const T& v) {
  31. *this << v;
  32. }
  33. /// Construct object from T
  34. /**
  35. * The object is constructed on the zone `z`.
  36. * See https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_object
  37. *
  38. * @tparam T The type of `v`.
  39. * @param v The value you want to convert.
  40. * @param z The zone that is used by the object.
  41. */
  42. template <typename T>
  43. object(const T& v, msgpack::zone& z):v1::object(v, z) {}
  44. public:
  45. /// Convert the object
  46. /**
  47. * If the object can't be converted to T, msgpack::type_error would be thrown.
  48. * @tparam T The type of v.
  49. * @param v The value you want to get. `v` is output parameter. `v` is overwritten by converted value from the object.
  50. * @return The reference of `v`.
  51. */
  52. template <typename T>
  53. T& convert(T& v) const { return v1::object::convert(v); }
  54. using v1::object::with_zone;
  55. implicit_type convert() const;
  56. };
  57. #if !defined(MSGPACK_USE_CPP03)
  58. namespace adaptor {
  59. // If v1 has as specialization for T, then dispatch v1::adaptor::as<T>.
  60. // So I call v1::has_as<T> meta function intentionally.
  61. template <typename T>
  62. struct as<T, typename std::enable_if<v1::has_as<T>::value>::type> : v1::adaptor::as<T> {
  63. };
  64. } // namespace adaptor
  65. template <typename T>
  66. struct has_as {
  67. private:
  68. template <typename U>
  69. static auto check(U*) ->
  70. typename std::enable_if<
  71. // check v2 specialization
  72. std::is_same<
  73. decltype(adaptor::as<U>()(std::declval<msgpack::object>())),
  74. U
  75. >::value
  76. ||
  77. // check v1 specialization
  78. v1::has_as<U>::value,
  79. std::true_type
  80. >::type;
  81. template <typename...>
  82. static std::false_type check(...);
  83. public:
  84. using type = decltype(check<T>(MSGPACK_NULLPTR));
  85. static constexpr bool value = type::value;
  86. };
  87. #endif // !defined(MSGPACK_USE_CPP03)
  88. /// @cond
  89. } // MSGPACK_API_VERSION_NAMESPACE(v2)
  90. /// @endcond
  91. } // namespace msgpack
  92. #endif // MSGPACK_V2_OBJECT_FWD_HPP