-
Notifications
You must be signed in to change notification settings - Fork 909
feat: add support for std::variant
#1075
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
327f3d1
implement `as` and `pack` for `std::variant`
uyha 818d87c
add option in CMake
uyha d6b2de9
fix MSVC build
uyha 8bd1b78
add headers to Files.cmake
uyha 211c50c
enhance error handling, implement object_with_zone, and add more tests
uyha df1d126
update CI
uyha 6507243
fix style
uyha 35638ea
fix style
uyha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// MessagePack for C++ static resolution routine | ||
// | ||
// Copyright (C) 2023 Uy Ha | ||
// | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENSE_1_0.txt) | ||
// | ||
|
||
#ifndef MSGPACK_TYPE_CPP17_VARIANT_HPP | ||
#define MSGPACK_TYPE_CPP17_VARIANT_HPP | ||
|
||
#include "msgpack/v1/adaptor/cpp17/variant.hpp" | ||
|
||
#endif // MSGPACK_TYPE_CPP17_VARIANT_HPP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
// | ||
// MessagePack for C++ static resolution routine | ||
// | ||
// Copyright (C) 2023 Uy Ha | ||
// | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENSE_1_0.txt) | ||
// | ||
|
||
#ifndef MSGPACK_V1_TYPE_VARIANT_HPP | ||
#define MSGPACK_V1_TYPE_VARIANT_HPP | ||
|
||
#if defined(MSGPACK_USE_STD_VARIANT_ADAPTOR) | ||
|
||
#include "msgpack/cpp_version.hpp" | ||
|
||
#if MSGPACK_CPP_VERSION >= 201703 | ||
|
||
#include "msgpack/adaptor/adaptor_base.hpp" | ||
#include "msgpack/object.hpp" | ||
#include "msgpack/versioning.hpp" | ||
|
||
#include <variant> | ||
|
||
namespace msgpack { | ||
MSGPACK_API_VERSION_NAMESPACE(v1) { | ||
namespace adaptor { | ||
namespace detail { | ||
template < | ||
typename Variant, | ||
typename T, | ||
typename... Ts, | ||
std::size_t current_index, | ||
std::size_t... indices | ||
> | ||
Variant construct_variant( | ||
std::size_t index, | ||
msgpack::object& object, | ||
std::index_sequence<current_index, indices...> | ||
) { | ||
if constexpr(sizeof...(Ts) == 0) { | ||
return object.as<T>(); | ||
} | ||
else { | ||
if (index == current_index) { | ||
return object.as<T>(); | ||
} | ||
return construct_variant<Variant, Ts...>( | ||
index, | ||
object, | ||
std::index_sequence<indices...>() | ||
); | ||
} | ||
} | ||
|
||
struct object_variant_overload { | ||
object_variant_overload(msgpack::object& obj, msgpack::zone& zone) | ||
: obj{obj} | ||
, zone{zone} {} | ||
|
||
template<typename T> | ||
void operator()(T const& value) { | ||
obj = msgpack::object(value, zone); | ||
} | ||
|
||
msgpack::object& obj; | ||
msgpack::zone& zone; | ||
}; | ||
} // namespace detail | ||
|
||
template <typename... Ts> | ||
struct as<std::variant<Ts...>, typename std::enable_if<(msgpack::has_as<Ts>::value && ...)>::type> { | ||
std::variant<Ts...> operator()(msgpack::object const& o) const { | ||
if ( o.type != msgpack::type::ARRAY | ||
uyha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|| o.via.array.size != 2 | ||
|| o.via.array.ptr[0].type != msgpack::type::POSITIVE_INTEGER | ||
|| o.via.array.ptr[0].via.u64 >= sizeof...(Ts)) { | ||
throw msgpack::type_error{}; | ||
} | ||
|
||
return detail::construct_variant<std::variant<Ts...>, Ts...>( | ||
o.via.array.ptr[0].as<std::size_t>(), | ||
o.via.array.ptr[1], | ||
std::make_index_sequence<sizeof...(Ts)>() | ||
); | ||
} | ||
}; | ||
|
||
template<typename... Ts> | ||
struct convert<std::variant<Ts...>> { | ||
msgpack::object const& operator()(msgpack::object const& o, std::variant<Ts...>& v) const { | ||
if ( o.type != msgpack::type::ARRAY | ||
uyha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|| o.via.array.size != 2 | ||
|| o.via.array.ptr[0].type != msgpack::type::POSITIVE_INTEGER | ||
|| o.via.array.ptr[0].via.u64 >= sizeof...(Ts)) { | ||
throw msgpack::type_error{}; | ||
} | ||
|
||
v = detail::construct_variant<std::variant<Ts...>, Ts...>( | ||
o.via.array.ptr[0].as<std::size_t>(), | ||
o.via.array.ptr[1], | ||
std::make_index_sequence<sizeof...(Ts)>() | ||
); | ||
return o; | ||
} | ||
}; | ||
|
||
template <typename... Ts> | ||
struct pack<std::variant<Ts...>>{ | ||
template<typename Stream> | ||
msgpack::packer<Stream>& operator()( | ||
msgpack::packer<Stream>& o, | ||
std::variant<Ts...> const& v | ||
) const { | ||
o.pack_array(2); | ||
o.pack_uint64(v.index()); | ||
std::visit([&o](auto const& value){o.pack(value);}, v); | ||
return o; | ||
} | ||
}; | ||
|
||
|
||
template<typename... Ts> | ||
struct object_with_zone<std::variant<Ts...>> { | ||
void operator()( | ||
msgpack::object::with_zone& o, | ||
std::variant<Ts...> const& v | ||
) 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.via.array.size = 2; | ||
o.via.array.ptr = p; | ||
o.via.array.ptr[0]= msgpack::object(v.index(), o.zone); | ||
std::visit(detail::object_variant_overload(o.via.array.ptr[1], o.zone), v); | ||
} | ||
}; | ||
} // namespace adaptor | ||
} | ||
} // namespace msgpack | ||
|
||
#endif // MSGPACK_CPP_VERSION >= 201703 | ||
#endif // defined(MSGPACK_USE_STD_VARIANT_ADAPTOR) | ||
#endif // MSGPACK_V1_TYPE_VARIANT_HPP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.