mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-10-14 15:05:37 +02:00
fix style
This commit is contained in:
@@ -32,11 +32,13 @@ template <
|
|||||||
typename T,
|
typename T,
|
||||||
typename... Ts,
|
typename... Ts,
|
||||||
std::size_t current_index,
|
std::size_t current_index,
|
||||||
std::size_t... indices>
|
std::size_t... indices
|
||||||
|
>
|
||||||
Variant construct_variant(
|
Variant construct_variant(
|
||||||
std::size_t index,
|
std::size_t index,
|
||||||
msgpack::object& object,
|
msgpack::object& object,
|
||||||
std::index_sequence<current_index, indices...>) {
|
std::index_sequence<current_index, indices...>
|
||||||
|
) {
|
||||||
if constexpr(sizeof...(Ts) == 0) {
|
if constexpr(sizeof...(Ts) == 0) {
|
||||||
return object.as<T>();
|
return object.as<T>();
|
||||||
}
|
}
|
||||||
@@ -45,9 +47,10 @@ Variant construct_variant(
|
|||||||
return object.as<T>();
|
return object.as<T>();
|
||||||
}
|
}
|
||||||
return construct_variant<Variant, Ts...>(
|
return construct_variant<Variant, Ts...>(
|
||||||
index,
|
index,
|
||||||
object,
|
object,
|
||||||
std::index_sequence<indices...>());
|
std::index_sequence<indices...>()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,16 +73,16 @@ template <typename... Ts>
|
|||||||
struct as<std::variant<Ts...>, typename std::enable_if<(msgpack::has_as<Ts>::value && ...)>::type> {
|
struct as<std::variant<Ts...>, typename std::enable_if<(msgpack::has_as<Ts>::value && ...)>::type> {
|
||||||
std::variant<Ts...> operator()(msgpack::object const& o) const {
|
std::variant<Ts...> operator()(msgpack::object const& o) const {
|
||||||
if ( o.type != msgpack::type::ARRAY
|
if ( o.type != msgpack::type::ARRAY
|
||||||
|| o.via.array.size != 2
|
|| o.via.array.size != 2
|
||||||
|| o.via.array.ptr[0].type != msgpack::type::POSITIVE_INTEGER
|
|| o.via.array.ptr[0].type != msgpack::type::POSITIVE_INTEGER
|
||||||
|| o.via.array.ptr[0].via.u64 >= sizeof...(Ts)) {
|
|| o.via.array.ptr[0].via.u64 >= sizeof...(Ts)) {
|
||||||
throw msgpack::type_error{};
|
throw msgpack::type_error{};
|
||||||
}
|
}
|
||||||
|
|
||||||
return detail::construct_variant<std::variant<Ts...>, Ts...>(
|
return detail::construct_variant<std::variant<Ts...>, Ts...>(
|
||||||
o.via.array.ptr[0].as<std::size_t>(),
|
o.via.array.ptr[0].as<std::size_t>(),
|
||||||
o.via.array.ptr[1],
|
o.via.array.ptr[1],
|
||||||
std::make_index_sequence<sizeof...(Ts)>()
|
std::make_index_sequence<sizeof...(Ts)>()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -88,16 +91,16 @@ template<typename... Ts>
|
|||||||
struct convert<std::variant<Ts...>> {
|
struct convert<std::variant<Ts...>> {
|
||||||
msgpack::object const& operator()(msgpack::object const& o, std::variant<Ts...>& v) const {
|
msgpack::object const& operator()(msgpack::object const& o, std::variant<Ts...>& v) const {
|
||||||
if ( o.type != msgpack::type::ARRAY
|
if ( o.type != msgpack::type::ARRAY
|
||||||
|| o.via.array.size != 2
|
|| o.via.array.size != 2
|
||||||
|| o.via.array.ptr[0].type != msgpack::type::POSITIVE_INTEGER
|
|| o.via.array.ptr[0].type != msgpack::type::POSITIVE_INTEGER
|
||||||
|| o.via.array.ptr[0].via.u64 >= sizeof...(Ts)) {
|
|| o.via.array.ptr[0].via.u64 >= sizeof...(Ts)) {
|
||||||
throw msgpack::type_error{};
|
throw msgpack::type_error{};
|
||||||
}
|
}
|
||||||
|
|
||||||
v = detail::construct_variant<std::variant<Ts...>, Ts...>(
|
v = detail::construct_variant<std::variant<Ts...>, Ts...>(
|
||||||
o.via.array.ptr[0].as<std::size_t>(),
|
o.via.array.ptr[0].as<std::size_t>(),
|
||||||
o.via.array.ptr[1],
|
o.via.array.ptr[1],
|
||||||
std::make_index_sequence<sizeof...(Ts)>()
|
std::make_index_sequence<sizeof...(Ts)>()
|
||||||
);
|
);
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
@@ -106,7 +109,10 @@ struct convert<std::variant<Ts...>> {
|
|||||||
template <typename... Ts>
|
template <typename... Ts>
|
||||||
struct pack<std::variant<Ts...>>{
|
struct pack<std::variant<Ts...>>{
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, std::variant<Ts...> const& v) const {
|
msgpack::packer<Stream>& operator()(
|
||||||
|
msgpack::packer<Stream>& o,
|
||||||
|
std::variant<Ts...> const& v
|
||||||
|
) const {
|
||||||
o.pack_array(2);
|
o.pack_array(2);
|
||||||
o.pack_uint64(v.index());
|
o.pack_uint64(v.index());
|
||||||
std::visit([&o](auto const& value){o.pack(value);}, v);
|
std::visit([&o](auto const& value){o.pack(value);}, v);
|
||||||
@@ -117,10 +123,17 @@ struct pack<std::variant<Ts...>>{
|
|||||||
|
|
||||||
template<typename... Ts>
|
template<typename... Ts>
|
||||||
struct object_with_zone<std::variant<Ts...>> {
|
struct object_with_zone<std::variant<Ts...>> {
|
||||||
void operator()(msgpack::object::with_zone& o, std::variant<Ts...> const& v) const {
|
void operator()(
|
||||||
msgpack::object *p = static_cast<msgpack::object *>(
|
msgpack::object::with_zone& o,
|
||||||
o.zone.allocate_align(sizeof(msgpack::object) * 2,
|
std::variant<Ts...> const& v
|
||||||
MSGPACK_ZONE_ALIGNOF(msgpack::object)));
|
) const {
|
||||||
|
msgpack::object *p =
|
||||||
|
static_cast<msgpack::object *>(
|
||||||
|
o.zone.allocate_align(
|
||||||
|
sizeof(msgpack::object) * 2,
|
||||||
|
MSGPACK_ZONE_ALIGNOF(msgpack::object)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
o.type = msgpack::type::ARRAY;
|
o.type = msgpack::type::ARRAY;
|
||||||
o.via.array.size = 2;
|
o.via.array.size = 2;
|
||||||
|
@@ -464,24 +464,25 @@ BOOST_AUTO_TEST_CASE(carray_byte_object_with_zone)
|
|||||||
#if defined(MSGPACK_USE_STD_VARIANT_ADAPTOR)
|
#if defined(MSGPACK_USE_STD_VARIANT_ADAPTOR)
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(variant_pack_unpack_as) {
|
BOOST_AUTO_TEST_CASE(variant_pack_unpack_as) {
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
std::variant<bool, int, float, double> val1{1.0};
|
std::variant<bool, int, float, double> val1{1.0};
|
||||||
msgpack::pack(ss, val1);
|
msgpack::pack(ss, val1);
|
||||||
std::string const& str = ss.str();
|
std::string const& str = ss.str();
|
||||||
msgpack::object_handle oh =
|
msgpack::object_handle oh =
|
||||||
msgpack::unpack(str.data(), str.size());
|
msgpack::unpack(str.data(), str.size());
|
||||||
std::variant<bool, int, float, double> val2 = oh.get().as<std::variant<bool, int, float, double> >();
|
std::variant<bool, int, float, double> val2 =
|
||||||
BOOST_CHECK(val1 == val2);
|
oh.get().as<std::variant<bool, int, float, double> >();
|
||||||
BOOST_CHECK_THROW((oh.get().as<std::variant<bool>>()), msgpack::type_error);
|
BOOST_CHECK(val1 == val2);
|
||||||
|
BOOST_CHECK_THROW((oh.get().as<std::variant<bool>>()), msgpack::type_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(variant_with_zone) {
|
BOOST_AUTO_TEST_CASE(variant_with_zone) {
|
||||||
msgpack::zone z;
|
msgpack::zone z;
|
||||||
std::variant<bool, int, float, double> val1{1.0};
|
std::variant<bool, int, float, double> val1{1.0};
|
||||||
msgpack::object obj(val1, z);
|
msgpack::object obj(val1, z);
|
||||||
std::variant<bool, int, float, double> val2 = obj.as<std::variant<bool, int, float, double>>();
|
std::variant<bool, int, float, double> val2 = obj.as<std::variant<bool, int, float, double>>();
|
||||||
BOOST_CHECK(val1 == val2);
|
BOOST_CHECK(val1 == val2);
|
||||||
BOOST_CHECK_THROW((obj.as<std::variant<bool>>()), msgpack::type_error);
|
BOOST_CHECK_THROW((obj.as<std::variant<bool>>()), msgpack::type_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // defined(MSGPACK_USE_STD_VARIANT_ADAPTOR)
|
#endif // defined(MSGPACK_USE_STD_VARIANT_ADAPTOR)
|
||||||
|
Reference in New Issue
Block a user