|
| 1 | +// |
| 2 | +// MessagePack for C++ static resolution routine |
| 3 | +// |
| 4 | +// Copyright (C) 2023 Uy Ha |
| 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 | + |
| 11 | +#ifndef MSGPACK_V1_TYPE_VARIANT_HPP |
| 12 | +#define MSGPACK_V1_TYPE_VARIANT_HPP |
| 13 | + |
| 14 | +#if defined(MSGPACK_USE_STD_VARIANT_ADAPTOR) |
| 15 | + |
| 16 | +#include "msgpack/cpp_version.hpp" |
| 17 | + |
| 18 | +#if MSGPACK_CPP_VERSION >= 201703 |
| 19 | + |
| 20 | +#include "msgpack/adaptor/adaptor_base.hpp" |
| 21 | +#include "msgpack/object.hpp" |
| 22 | +#include "msgpack/versioning.hpp" |
| 23 | + |
| 24 | +#include <variant> |
| 25 | + |
| 26 | +namespace msgpack { |
| 27 | +MSGPACK_API_VERSION_NAMESPACE(v1) { |
| 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 | +> |
| 37 | +Variant construct_variant( |
| 38 | + std::size_t index, |
| 39 | + msgpack::object& object, |
| 40 | + std::index_sequence<current_index, indices...> |
| 41 | +) { |
| 42 | + if constexpr(sizeof...(Ts) == 0) { |
| 43 | + return object.as<T>(); |
| 44 | + } |
| 45 | + else { |
| 46 | + if (index == current_index) { |
| 47 | + return object.as<T>(); |
| 48 | + } |
| 49 | + return construct_variant<Variant, Ts...>( |
| 50 | + index, |
| 51 | + object, |
| 52 | + std::index_sequence<indices...>() |
| 53 | + ); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +struct object_variant_overload { |
| 58 | + object_variant_overload(msgpack::object& obj, msgpack::zone& zone) |
| 59 | + : obj{obj} |
| 60 | + , zone{zone} {} |
| 61 | + |
| 62 | + template<typename T> |
| 63 | + void operator()(T const& value) { |
| 64 | + obj = msgpack::object(value, zone); |
| 65 | + } |
| 66 | + |
| 67 | + msgpack::object& obj; |
| 68 | + msgpack::zone& zone; |
| 69 | +}; |
| 70 | +} // namespace detail |
| 71 | + |
| 72 | +template <typename... Ts> |
| 73 | +struct as<std::variant<Ts...>, typename std::enable_if<(msgpack::has_as<Ts>::value && ...)>::type> { |
| 74 | + std::variant<Ts...> operator()(msgpack::object const& o) const { |
| 75 | + if ( o.type != msgpack::type::ARRAY |
| 76 | + || o.via.array.size != 2 |
| 77 | + || o.via.array.ptr[0].type != msgpack::type::POSITIVE_INTEGER |
| 78 | + || o.via.array.ptr[0].via.u64 >= sizeof...(Ts)) { |
| 79 | + throw msgpack::type_error{}; |
| 80 | + } |
| 81 | + |
| 82 | + return detail::construct_variant<std::variant<Ts...>, Ts...>( |
| 83 | + o.via.array.ptr[0].as<std::size_t>(), |
| 84 | + o.via.array.ptr[1], |
| 85 | + std::make_index_sequence<sizeof...(Ts)>() |
| 86 | + ); |
| 87 | + } |
| 88 | +}; |
| 89 | + |
| 90 | +template<typename... Ts> |
| 91 | +struct convert<std::variant<Ts...>> { |
| 92 | + msgpack::object const& operator()(msgpack::object const& o, std::variant<Ts...>& v) const { |
| 93 | + if ( o.type != msgpack::type::ARRAY |
| 94 | + || o.via.array.size != 2 |
| 95 | + || o.via.array.ptr[0].type != msgpack::type::POSITIVE_INTEGER |
| 96 | + || o.via.array.ptr[0].via.u64 >= sizeof...(Ts)) { |
| 97 | + throw msgpack::type_error{}; |
| 98 | + } |
| 99 | + |
| 100 | + v = detail::construct_variant<std::variant<Ts...>, Ts...>( |
| 101 | + o.via.array.ptr[0].as<std::size_t>(), |
| 102 | + o.via.array.ptr[1], |
| 103 | + std::make_index_sequence<sizeof...(Ts)>() |
| 104 | + ); |
| 105 | + return o; |
| 106 | + } |
| 107 | +}; |
| 108 | + |
| 109 | +template <typename... Ts> |
| 110 | +struct pack<std::variant<Ts...>>{ |
| 111 | + template<typename Stream> |
| 112 | + msgpack::packer<Stream>& operator()( |
| 113 | + msgpack::packer<Stream>& o, |
| 114 | + std::variant<Ts...> const& v |
| 115 | + ) const { |
| 116 | + o.pack_array(2); |
| 117 | + o.pack_uint64(v.index()); |
| 118 | + std::visit([&o](auto const& value){o.pack(value);}, v); |
| 119 | + return o; |
| 120 | + } |
| 121 | +}; |
| 122 | + |
| 123 | + |
| 124 | +template<typename... Ts> |
| 125 | +struct object_with_zone<std::variant<Ts...>> { |
| 126 | + void operator()( |
| 127 | + msgpack::object::with_zone& o, |
| 128 | + std::variant<Ts...> const& v |
| 129 | + ) const { |
| 130 | + msgpack::object *p = |
| 131 | + static_cast<msgpack::object *>( |
| 132 | + o.zone.allocate_align( |
| 133 | + sizeof(msgpack::object) * 2, |
| 134 | + MSGPACK_ZONE_ALIGNOF(msgpack::object) |
| 135 | + ) |
| 136 | + ); |
| 137 | + |
| 138 | + o.type = msgpack::type::ARRAY; |
| 139 | + o.via.array.size = 2; |
| 140 | + o.via.array.ptr = p; |
| 141 | + o.via.array.ptr[0]= msgpack::object(v.index(), o.zone); |
| 142 | + std::visit(detail::object_variant_overload(o.via.array.ptr[1], o.zone), v); |
| 143 | + } |
| 144 | +}; |
| 145 | +} // namespace adaptor |
| 146 | +} |
| 147 | +} // namespace msgpack |
| 148 | + |
| 149 | +#endif // MSGPACK_CPP_VERSION >= 201703 |
| 150 | +#endif // defined(MSGPACK_USE_STD_VARIANT_ADAPTOR) |
| 151 | +#endif // MSGPACK_V1_TYPE_VARIANT_HPP |
0 commit comments