mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 10:46:18 +08:00
Allow tagged unions with explicit tags
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include <variant>
|
||||
|
||||
#include "define_literal.hpp"
|
||||
#include "internal/Getter.hpp"
|
||||
#include "internal/StringLiteral.hpp"
|
||||
#include "internal/tag_t.hpp"
|
||||
|
||||
@@ -84,7 +85,8 @@ struct TaggedUnion {
|
||||
/// Returns the underlying variant.
|
||||
const VariantType& variant() const { return variant_; }
|
||||
|
||||
static_assert(!define_literal_t<internal::tag_t<Ts>...>::has_duplicates(),
|
||||
static_assert(!define_literal_t<
|
||||
internal::tag_t<_discriminator, Ts>...>::has_duplicates(),
|
||||
"Duplicate tags are not allowed inside tagged unions.");
|
||||
|
||||
/// The underlying variant - a TaggedUnion is a thin wrapper
|
||||
@@ -92,6 +94,62 @@ struct TaggedUnion {
|
||||
VariantType variant_;
|
||||
};
|
||||
|
||||
namespace internal {
|
||||
|
||||
template <StringLiteral _discriminator, class... NamedTupleTypes>
|
||||
struct Getter<TaggedUnion<_discriminator, NamedTupleTypes...>> {
|
||||
public:
|
||||
/// Retrieves the indicated value from the tuple.
|
||||
template <int _index>
|
||||
static inline auto& get(
|
||||
TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) {
|
||||
return Getter<std::variant<NamedTupleTypes...>>::template get<_index>(
|
||||
_tu.variant_);
|
||||
}
|
||||
|
||||
/// Gets a field by name.
|
||||
template <StringLiteral _field_name>
|
||||
static inline auto& get(
|
||||
TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) {
|
||||
return Getter<std::variant<NamedTupleTypes...>>::template get<_field_name>(
|
||||
_tu.variant_);
|
||||
}
|
||||
|
||||
/// Gets a field by the field type.
|
||||
template <class Field>
|
||||
static inline auto& get(
|
||||
TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) {
|
||||
return Getter<std::variant<NamedTupleTypes...>>::template get<Field>(
|
||||
_tu.variant_);
|
||||
}
|
||||
|
||||
/// Retrieves the indicated value from the tuple.
|
||||
template <int _index>
|
||||
static inline const auto& get_const(
|
||||
const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) {
|
||||
return Getter<std::variant<NamedTupleTypes...>>::template get_const<_index>(
|
||||
_tu.variant_);
|
||||
}
|
||||
|
||||
/// Gets a field by name.
|
||||
template <StringLiteral _field_name>
|
||||
static inline const auto& get_const(
|
||||
const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) {
|
||||
return Getter<std::variant<NamedTupleTypes...>>::template get_const<
|
||||
_field_name>(_tu.variant_);
|
||||
}
|
||||
|
||||
/// Gets a field by the field type.
|
||||
template <class Field>
|
||||
static inline const auto& get_const(
|
||||
const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) {
|
||||
return Getter<std::variant<NamedTupleTypes...>>::template get_const<Field>(
|
||||
_tu.variant_);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif // RFL_TAGGEDUNION_HPP_
|
||||
|
||||
@@ -10,9 +10,8 @@
|
||||
|
||||
namespace rfl {
|
||||
|
||||
template <internal::StringLiteral _field_name, class NamedTupleType>
|
||||
using field_type_t =
|
||||
typename internal::FieldType<_field_name, NamedTupleType>::Type;
|
||||
template <internal::StringLiteral _field_name, class T>
|
||||
using field_type_t = typename internal::FieldType<_field_name, T>::Type;
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
|
||||
@@ -4,12 +4,10 @@
|
||||
#include <tuple>
|
||||
#include <variant>
|
||||
|
||||
#include "../TaggedUnion.hpp"
|
||||
#include "StringLiteral.hpp"
|
||||
#include "find_index.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
namespace rfl::internal {
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
@@ -153,62 +151,6 @@ struct Getter<std::variant<NamedTupleTypes...>> {
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/// For handling std::variant.
|
||||
template <StringLiteral _discriminator, class... NamedTupleTypes>
|
||||
struct Getter<TaggedUnion<_discriminator, NamedTupleTypes...>> {
|
||||
public:
|
||||
/// Retrieves the indicated value from the tuple.
|
||||
template <int _index>
|
||||
static inline auto& get(
|
||||
TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) {
|
||||
return Getter<std::variant<NamedTupleTypes...>>::template get<_index>(
|
||||
_tu.variant_);
|
||||
}
|
||||
|
||||
/// Gets a field by name.
|
||||
template <StringLiteral _field_name>
|
||||
static inline auto& get(
|
||||
TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) {
|
||||
return Getter<std::variant<NamedTupleTypes...>>::template get<_field_name>(
|
||||
_tu.variant_);
|
||||
}
|
||||
|
||||
/// Gets a field by the field type.
|
||||
template <class Field>
|
||||
static inline auto& get(
|
||||
TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) {
|
||||
return Getter<std::variant<NamedTupleTypes...>>::template get<Field>(
|
||||
_tu.variant_);
|
||||
}
|
||||
|
||||
/// Retrieves the indicated value from the tuple.
|
||||
template <int _index>
|
||||
static inline const auto& get_const(
|
||||
const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) {
|
||||
return Getter<std::variant<NamedTupleTypes...>>::template get_const<_index>(
|
||||
_tu.variant_);
|
||||
}
|
||||
|
||||
/// Gets a field by name.
|
||||
template <StringLiteral _field_name>
|
||||
static inline const auto& get_const(
|
||||
const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) {
|
||||
return Getter<std::variant<NamedTupleTypes...>>::template get_const<
|
||||
_field_name>(_tu.variant_);
|
||||
}
|
||||
|
||||
/// Gets a field by the field type.
|
||||
template <class Field>
|
||||
static inline const auto& get_const(
|
||||
const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) {
|
||||
return Getter<std::variant<NamedTupleTypes...>>::template get_const<Field>(
|
||||
_tu.variant_);
|
||||
}
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
} // namespace rfl::internal
|
||||
|
||||
#endif
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
|
||||
#include "../always_false.hpp"
|
||||
#include "get_fake_object.hpp"
|
||||
#include "is_named_tuple.hpp"
|
||||
#include "num_fields.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
@@ -23,8 +23,7 @@
|
||||
#endif
|
||||
#endif
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
namespace rfl::internal {
|
||||
|
||||
template <class T>
|
||||
struct Wrapper {
|
||||
@@ -112,6 +111,8 @@ auto concat_literals(const Head& _head, const Tail&... _tail) {
|
||||
}
|
||||
}
|
||||
|
||||
inline auto concat_literals() { return rfl::Literal<>(); }
|
||||
|
||||
#ifdef __clang__
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wundefined-var-template"
|
||||
@@ -150,7 +151,6 @@ auto get_field_names() {
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
} // namespace rfl::internal
|
||||
|
||||
#endif
|
||||
|
||||
@@ -10,10 +10,11 @@ namespace rfl::internal {
|
||||
|
||||
template <class T>
|
||||
constexpr bool is_empty() {
|
||||
if constexpr (is_named_tuple_v<T>) {
|
||||
return std::remove_cvref_t<std::remove_pointer_t<T>>::size() == 0;
|
||||
using U = std::remove_cvref_t<std::remove_pointer_t<T>>;
|
||||
if constexpr (is_named_tuple_v<U>) {
|
||||
return U::size() == 0;
|
||||
} else {
|
||||
using TupleType = ptr_tuple_t<T>;
|
||||
using TupleType = ptr_tuple_t<U>;
|
||||
return std::tuple_size_v<TupleType> == 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,18 +2,22 @@
|
||||
#define RFL_INTERNAL_MAKE_TAG_HPP_
|
||||
|
||||
#include "../Literal.hpp"
|
||||
#include "../field_names_t.hpp"
|
||||
#include "../to_view.hpp"
|
||||
#include "StringLiteral.hpp"
|
||||
#include "get_type_name.hpp"
|
||||
#include "has_reflection_type_v.hpp"
|
||||
#include "has_tag_v.hpp"
|
||||
#include "remove_namespaces.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
namespace rfl::internal {
|
||||
|
||||
template <class T>
|
||||
static inline auto make_tag() noexcept {
|
||||
template <internal::StringLiteral _discriminator, class T>
|
||||
static inline auto make_tag(const T& _t) noexcept {
|
||||
if constexpr (internal::has_reflection_type_v<T>) {
|
||||
return make_tag<typename T::ReflectionType>();
|
||||
} else if constexpr (field_names_t<T>::template contains<_discriminator>()) {
|
||||
return *to_view(_t).template get<_discriminator>();
|
||||
} else if constexpr (internal::has_tag_v<T>) {
|
||||
using LiteralType = typename T::Tag;
|
||||
return LiteralType::template name_of<0>();
|
||||
@@ -23,7 +27,6 @@ static inline auto make_tag() noexcept {
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
} // namespace rfl::internal
|
||||
|
||||
#endif
|
||||
|
||||
@@ -3,15 +3,15 @@
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "StringLiteral.hpp"
|
||||
#include "make_tag.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
namespace rfl::internal {
|
||||
|
||||
template <class T>
|
||||
using tag_t = typename std::invoke_result<decltype(make_tag<T>)>::type;
|
||||
template <internal::StringLiteral _discriminator, class T>
|
||||
using tag_t =
|
||||
typename std::invoke_result<decltype(make_tag<_discriminator, T>), T>::type;
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
} // namespace rfl::internal
|
||||
|
||||
#endif
|
||||
|
||||
@@ -53,9 +53,8 @@ struct Parser<R, W, TaggedUnion<_discriminator, AlternativeTypes...>> {
|
||||
|
||||
static schema::Type to_schema(
|
||||
std::map<std::string, schema::Type>* _definitions) {
|
||||
using VariantType =
|
||||
std::variant<std::invoke_result_t<decltype(wrap<AlternativeTypes>),
|
||||
AlternativeTypes>...>;
|
||||
using VariantType = std::variant<std::invoke_result_t<
|
||||
decltype(wrap_if_necessary<AlternativeTypes>), AlternativeTypes>...>;
|
||||
return Parser<R, W, VariantType>::to_schema(_definitions);
|
||||
}
|
||||
|
||||
@@ -117,32 +116,36 @@ struct Parser<R, W, TaggedUnion<_discriminator, AlternativeTypes...>> {
|
||||
template <class T>
|
||||
static inline bool contains_disc_value(
|
||||
const std::string& _disc_value) noexcept {
|
||||
return internal::tag_t<T>::contains(_disc_value);
|
||||
return internal::tag_t<_discriminator, T>::contains(_disc_value);
|
||||
}
|
||||
|
||||
/// Writes a wrapped version of the original object, which contains the tag.
|
||||
template <class T, class P>
|
||||
static void write_wrapped(const W& _w, const T& _val,
|
||||
const P& _parent) noexcept {
|
||||
const auto wrapped = wrap(_val);
|
||||
const auto wrapped = wrap_if_necessary(_val);
|
||||
Parser<R, W, std::remove_cvref_t<decltype(wrapped)>>::write(_w, wrapped,
|
||||
_parent);
|
||||
}
|
||||
|
||||
/// Generates a wrapped version of the original object, which contains the
|
||||
/// tag.
|
||||
/// tag, if the object doesn't already contain the wrap.
|
||||
template <class T>
|
||||
static auto wrap(const T& _val) noexcept {
|
||||
const auto tag = internal::make_tag<T>();
|
||||
using TagType = std::remove_cvref_t<decltype(tag)>;
|
||||
if constexpr (internal::has_fields<std::remove_cvref_t<T>>()) {
|
||||
using WrapperType =
|
||||
TaggedUnionWrapperWithFields<T, TagType, _discriminator>;
|
||||
return WrapperType{.tag = tag, .fields = &_val};
|
||||
static auto wrap_if_necessary(const T& _val) noexcept {
|
||||
if constexpr (field_names_t<T>::template contains<_discriminator>()) {
|
||||
return _val;
|
||||
} else {
|
||||
using WrapperType =
|
||||
TaggedUnionWrapperNoFields<T, TagType, _discriminator>;
|
||||
return WrapperType{.tag = tag, .fields = &_val};
|
||||
const auto tag = internal::make_tag<_discriminator, T>(_val);
|
||||
using TagType = std::remove_cvref_t<decltype(tag)>;
|
||||
if constexpr (internal::has_fields<std::remove_cvref_t<T>>()) {
|
||||
using WrapperType =
|
||||
TaggedUnionWrapperWithFields<T, TagType, _discriminator>;
|
||||
return WrapperType{.tag = tag, .fields = &_val};
|
||||
} else {
|
||||
using WrapperType =
|
||||
TaggedUnionWrapperNoFields<T, TagType, _discriminator>;
|
||||
return WrapperType{.tag = tag, .fields = &_val};
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
#include "test_tagged_union3.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_tagged_union3 {
|
||||
|
||||
struct Circle {
|
||||
rfl::Literal<"circle", "Circle"> shape;
|
||||
double radius;
|
||||
};
|
||||
|
||||
struct Rectangle {
|
||||
rfl::Literal<"rectangle", "Rectangle", "rect"> shape;
|
||||
double height;
|
||||
double width;
|
||||
};
|
||||
|
||||
struct Square {
|
||||
rfl::Literal<"square", "Square"> shape;
|
||||
double width;
|
||||
};
|
||||
|
||||
struct Empty {};
|
||||
|
||||
using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle, Empty>;
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const Shapes r = Rectangle{
|
||||
.shape = rfl::Literal<"rectangle", "Rectangle", "rect">::make<"rect">(),
|
||||
.height = 10,
|
||||
.width = 5};
|
||||
|
||||
write_and_read(r, R"({"shape":"rect","height":10.0,"width":5.0})");
|
||||
}
|
||||
} // namespace test_tagged_union3
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_tagged_union3 {
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -73,6 +73,7 @@
|
||||
#include "test_string_unordered_map.hpp"
|
||||
#include "test_tagged_union.hpp"
|
||||
#include "test_tagged_union2.hpp"
|
||||
#include "test_tagged_union3.hpp"
|
||||
#include "test_timestamp.hpp"
|
||||
#include "test_transform.hpp"
|
||||
#include "test_unique_ptr.hpp"
|
||||
@@ -97,6 +98,7 @@ int main() {
|
||||
test_variant::test();
|
||||
test_tagged_union::test();
|
||||
test_tagged_union2::test();
|
||||
test_tagged_union3::test();
|
||||
test_field_variant::test();
|
||||
test_ref::test();
|
||||
test_box::test();
|
||||
|
||||
Reference in New Issue
Block a user