|
25 | 25 |
|
26 | 26 | namespace msgpack { |
27 | 27 | MSGPACK_API_VERSION_NAMESPACE(v1) { |
28 | | - namespace adaptor { |
29 | | - namespace detail { |
30 | | - template <typename Variant, typename T, typename... Ts, std::size_t current_index, std::size_t... indices> |
31 | | - Variant construct_variant(std::size_t index, |
32 | | - msgpack::object &object, |
33 | | - std::index_sequence<current_index, indices...>) { |
| 28 | +namespace adaptor { |
| 29 | +namespace detail { |
| 30 | +template < |
| 31 | + typename Variant, |
| 32 | + typename T, |
| 33 | + typename... Ts, |
| 34 | + std::size_t current_index, |
| 35 | + std::size_t... indices> |
| 36 | +Variant construct_variant( |
| 37 | + std::size_t index, |
| 38 | + msgpack::object& object, |
| 39 | + std::index_sequence<current_index, indices...>) { |
34 | 40 | if constexpr(sizeof...(Ts) == 0) { |
35 | | - return object.as<T>(); |
36 | | - } else { |
37 | | - if (index == current_index) { |
38 | 41 | return object.as<T>(); |
39 | | - } |
40 | | - return construct_variant<Variant, Ts...>(index, object, std::index_sequence<indices...>()); |
41 | 42 | } |
42 | | - } |
| 43 | + else { |
| 44 | + if (index == current_index) { |
| 45 | + return object.as<T>(); |
| 46 | + } |
| 47 | + return construct_variant<Variant, Ts...>( |
| 48 | + index, |
| 49 | + object, |
| 50 | + std::index_sequence<indices...>()); |
| 51 | + } |
| 52 | +} |
43 | 53 |
|
44 | | - struct object_variant_overload { |
45 | | - object_variant_overload(msgpack::object &obj, msgpack::zone &zone) |
46 | | - : obj{obj} |
47 | | - , zone{zone} {} |
| 54 | +struct object_variant_overload { |
| 55 | + object_variant_overload(msgpack::object& obj, msgpack::zone& zone) |
| 56 | + : obj{obj} |
| 57 | + , zone{zone} {} |
48 | 58 |
|
49 | 59 | template<typename T> |
50 | | - void operator()(T const &value) { |
51 | | - obj = msgpack::object(value, zone); |
| 60 | + void operator()(T const& value) { |
| 61 | + obj = msgpack::object(value, zone); |
52 | 62 | } |
53 | 63 |
|
54 | | - msgpack::object &obj; |
55 | | - msgpack::zone &zone; |
56 | | - }; |
57 | | - } // namespace detail |
58 | | - |
59 | | - template <typename... Ts> |
60 | | - struct as<std::variant<Ts...>, typename std::enable_if<(msgpack::has_as<Ts>::value && ...)>::type> { |
61 | | - std::variant<Ts...> operator()(msgpack::object const &o) const { |
62 | | - if ( o.type != msgpack::type::ARRAY |
63 | | - || o.via.array.size != 2 |
64 | | - || o.via.array.ptr[0].type != msgpack::type::POSITIVE_INTEGER |
65 | | - || o.via.array.ptr[0].via.u64 >= sizeof...(Ts)) { |
66 | | - throw msgpack::type_error{}; |
67 | | - } |
68 | | - |
69 | | - return detail::construct_variant<std::variant<Ts...>, Ts...>( |
70 | | - o.via.array.ptr[0].as<std::size_t>(), |
71 | | - o.via.array.ptr[1], |
72 | | - std::make_index_sequence<sizeof...(Ts)>() |
73 | | - ); |
| 64 | + msgpack::object& obj; |
| 65 | + msgpack::zone& zone; |
| 66 | +}; |
| 67 | +} // namespace detail |
| 68 | + |
| 69 | +template <typename... Ts> |
| 70 | +struct as<std::variant<Ts...>, typename std::enable_if<(msgpack::has_as<Ts>::value && ...)>::type> { |
| 71 | + std::variant<Ts...> operator()(msgpack::object const& o) const { |
| 72 | + if ( o.type != msgpack::type::ARRAY |
| 73 | + || o.via.array.size != 2 |
| 74 | + || o.via.array.ptr[0].type != msgpack::type::POSITIVE_INTEGER |
| 75 | + || o.via.array.ptr[0].via.u64 >= sizeof...(Ts)) { |
| 76 | + throw msgpack::type_error{}; |
| 77 | + } |
| 78 | + |
| 79 | + return detail::construct_variant<std::variant<Ts...>, Ts...>( |
| 80 | + o.via.array.ptr[0].as<std::size_t>(), |
| 81 | + o.via.array.ptr[1], |
| 82 | + std::make_index_sequence<sizeof...(Ts)>() |
| 83 | + ); |
74 | 84 | } |
75 | | - }; |
76 | | - |
77 | | - template<typename... Ts> |
78 | | - struct convert<std::variant<Ts...>> { |
79 | | - msgpack::object const &operator()(msgpack::object const &o, std::variant<Ts...> &v) const { |
80 | | - if ( o.type != msgpack::type::ARRAY |
81 | | - || o.via.array.size != 2 |
82 | | - || o.via.array.ptr[0].type != msgpack::type::POSITIVE_INTEGER |
83 | | - || o.via.array.ptr[0].via.u64 >= sizeof...(Ts)) { |
84 | | - throw msgpack::type_error{}; |
85 | | - } |
86 | | - |
87 | | - v = detail::construct_variant<std::variant<Ts...>, Ts...>( |
88 | | - o.via.array.ptr[0].as<std::size_t>(), |
89 | | - o.via.array.ptr[1], |
90 | | - std::make_index_sequence<sizeof...(Ts)>() |
91 | | - ); |
92 | | - return o; |
| 85 | +}; |
| 86 | + |
| 87 | +template<typename... Ts> |
| 88 | +struct convert<std::variant<Ts...>> { |
| 89 | + msgpack::object const& operator()(msgpack::object const& o, std::variant<Ts...>& v) const { |
| 90 | + if ( o.type != msgpack::type::ARRAY |
| 91 | + || o.via.array.size != 2 |
| 92 | + || o.via.array.ptr[0].type != msgpack::type::POSITIVE_INTEGER |
| 93 | + || o.via.array.ptr[0].via.u64 >= sizeof...(Ts)) { |
| 94 | + throw msgpack::type_error{}; |
| 95 | + } |
| 96 | + |
| 97 | + v = detail::construct_variant<std::variant<Ts...>, Ts...>( |
| 98 | + o.via.array.ptr[0].as<std::size_t>(), |
| 99 | + o.via.array.ptr[1], |
| 100 | + std::make_index_sequence<sizeof...(Ts)>() |
| 101 | + ); |
| 102 | + return o; |
93 | 103 | } |
94 | | - }; |
| 104 | +}; |
95 | 105 |
|
96 | | - template <typename... Ts> |
97 | | - struct pack<std::variant<Ts...>>{ |
| 106 | +template <typename... Ts> |
| 107 | +struct pack<std::variant<Ts...>>{ |
98 | 108 | template<typename Stream> |
99 | | - msgpack::packer<Stream>& operator()(msgpack::packer<Stream> &o, std::variant<Ts...> const &v) const { |
100 | | - o.pack_array(2); |
101 | | - o.pack_uint64(v.index()); |
102 | | - std::visit([&o](auto const &value){o.pack(value);}, v); |
103 | | - return o; |
| 109 | + msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, std::variant<Ts...> const& v) const { |
| 110 | + o.pack_array(2); |
| 111 | + o.pack_uint64(v.index()); |
| 112 | + std::visit([&o](auto const& value){o.pack(value);}, v); |
| 113 | + return o; |
104 | 114 | } |
105 | | - }; |
| 115 | +}; |
106 | 116 |
|
107 | 117 |
|
108 | | - template<typename... Ts> |
109 | | - struct object_with_zone<std::variant<Ts...>> { |
110 | | - void operator()(msgpack::object::with_zone &o, std::variant<Ts...> const &v) const { |
111 | | - msgpack::object *p = static_cast<msgpack::object *>(o.zone.allocate_align(sizeof(msgpack::object) * 2, MSGPACK_ZONE_ALIGNOF(msgpack::object))); |
| 118 | +template<typename... Ts> |
| 119 | +struct object_with_zone<std::variant<Ts...>> { |
| 120 | + void operator()(msgpack::object::with_zone& o, std::variant<Ts...> const& v) const { |
| 121 | + msgpack::object *p = static_cast<msgpack::object *>( |
| 122 | + o.zone.allocate_align(sizeof(msgpack::object) * 2, |
| 123 | + MSGPACK_ZONE_ALIGNOF(msgpack::object))); |
112 | 124 |
|
113 | | - o.type = msgpack::type::ARRAY; |
114 | | - o.via.array.size = 2; |
115 | | - o.via.array.ptr = p; |
116 | | - o.via.array.ptr[0]= msgpack::object(v.index(), o.zone); |
117 | | - std::visit(detail::object_variant_overload(o.via.array.ptr[1], o.zone), v); |
| 125 | + o.type = msgpack::type::ARRAY; |
| 126 | + o.via.array.size = 2; |
| 127 | + o.via.array.ptr = p; |
| 128 | + o.via.array.ptr[0]= msgpack::object(v.index(), o.zone); |
| 129 | + std::visit(detail::object_variant_overload(o.via.array.ptr[1], o.zone), v); |
118 | 130 | } |
119 | | - }; |
120 | | - } // namespace adaptor |
| 131 | +}; |
| 132 | +} // namespace adaptor |
121 | 133 | } |
122 | 134 | } // namespace msgpack |
123 | 135 |
|
|
0 commit comments