MessagePack for C++
Loading...
Searching...
No Matches
variant.hpp
Go to the documentation of this file.
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
17
18#if MSGPACK_CPP_VERSION >= 201703
19
21#include "msgpack/object.hpp"
23
24#include <variant>
25
26namespace msgpack {
28namespace adaptor {
29namespace detail {
30template <
31 typename Variant,
32 typename T,
33 typename... Ts,
34 std::size_t current_index,
35 std::size_t... indices
36>
37Variant 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 Variant{std::in_place_index<current_index>, object.as<T>()};
44 }
45 else {
46 if (index == current_index) {
47 return Variant{std::in_place_index<current_index>, object.as<T>()};
48 }
49 return construct_variant<Variant, Ts...>(
50 index,
51 object,
52 std::index_sequence<indices...>()
53 );
54 }
55}
56
57struct 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
72template <typename... Ts>
73struct 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
90template<typename... Ts>
91struct 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
109template <typename... Ts>
110struct pack<std::variant<Ts...>>{
111 template<typename Stream>
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
124template<typename... Ts>
125struct object_with_zone<std::variant<Ts...>> {
126 void operator()(
128 std::variant<Ts...> const& v
129 ) const {
130 msgpack::object *p =
131 static_cast<msgpack::object *>(
133 sizeof(msgpack::object) * 2,
135 )
136 );
137
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
The class template that supports continuous packing.
Definition pack.hpp:33
packer< Stream > & pack_array(uint32_t n)
Packing array header and size.
Definition pack.hpp:1195
packer< Stream > & pack_uint64(uint64_t d)
Packing uint16.
Definition pack.hpp:740
packer< Stream > & pack(const T &v)
Packing function template.
Definition object_fwd.hpp:231
Definition cpp03_zone.hpp:30
void * allocate_align(size_t size, size_t align=MSGPACK_ZONE_ALIGN)
Definition cpp03_zone.hpp:255
basic_variant< std::string, std::vector< char >, ext > variant
Definition msgpack_variant.hpp:269
@ ARRAY
Definition object_fwd_decl.hpp:40
Definition adaptor_base.hpp:15
void pack(msgpack::packer< Stream > &o, const T &v)
Definition object.hpp:1185
void convert(T &v, msgpack::object const &o)
Definition object.hpp:1178
msgpack::object const & operator()(msgpack::object const &o, T &v) const
Definition object.hpp:646
void operator()(msgpack::object::with_zone &o, T const &v) const
Definition object.hpp:662
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, T const &v) const
Definition object.hpp:655
Definition object.hpp:35
msgpack::zone & zone
Definition object.hpp:37
uint32_t size
Definition object_fwd.hpp:23
msgpack::object * ptr
Definition object_fwd.hpp:24
Object class that corresponding to MessagePack format object.
Definition object_fwd.hpp:75
std::enable_if< msgpack::has_as< T >::value, T >::type as() const
Get value as T.
Definition object.hpp:1126
union_type via
Definition object_fwd.hpp:93
msgpack::type::object_type type
Definition object_fwd.hpp:92
msgpack::object_array array
Definition object_fwd.hpp:85
uint64_t u64
Definition object_fwd.hpp:78
#define MSGPACK_ZONE_ALIGNOF(type)
Definition cpp03_zone_decl.hpp:30
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition versioning.hpp:66