mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 16:56:17 +08:00
Added support for tagged unions without explicitly tagging the structs
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
#ifndef RFL_INTERNAL_GETSTRUCTNAME_HPP_
|
||||
#define RFL_INTERNAL_GETSTRUCTNAME_HPP_
|
||||
|
||||
#include <iostream>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
std::string remove_namespaces(const std::string& _name) {
|
||||
const size_t pos = _name.find_last_of(":");
|
||||
if (pos == std::string::npos) {
|
||||
return _name;
|
||||
}
|
||||
return _name.substr(pos + 1);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
std::string get_struct_name_impl() {
|
||||
const std::string func_name = std::source_location::current().function_name();
|
||||
const size_t begin = func_name.find("T = ") + 4;
|
||||
if (begin == std::string::npos) {
|
||||
return "AnonymousStruct";
|
||||
}
|
||||
const size_t end = func_name.find_first_of("])};", begin);
|
||||
if (end == std::string::npos) {
|
||||
return "AnonymousStruct";
|
||||
}
|
||||
return remove_namespaces(func_name.substr(begin, end - begin));
|
||||
}
|
||||
|
||||
template <class T>
|
||||
std::string get_struct_name() {
|
||||
return get_struct_name_impl<std::decay_t<std::remove_pointer_t<T>>>();
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,31 @@
|
||||
#ifndef RFL_HASTAGV_HPP_
|
||||
#define RFL_HASTAGV_HPP_
|
||||
|
||||
#include <cstdint>
|
||||
#include <utility>
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class Wrapper>
|
||||
class HasTag {
|
||||
private:
|
||||
template <class U>
|
||||
static std::int64_t foo(...);
|
||||
|
||||
template <class U>
|
||||
static std::int32_t foo(typename U::Tag*);
|
||||
|
||||
public:
|
||||
static constexpr bool value =
|
||||
sizeof(foo<Wrapper>(nullptr)) == sizeof(std::int32_t);
|
||||
};
|
||||
|
||||
/// Used for tagged unions - determines whether a struct as a Tag.
|
||||
template <typename Wrapper>
|
||||
constexpr bool has_tag_v = HasTag<Wrapper>::value;
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -38,9 +38,11 @@
|
||||
#include "rfl/internal/flattened_ptr_tuple_t.hpp"
|
||||
#include "rfl/internal/flattened_tuple_t.hpp"
|
||||
#include "rfl/internal/get_field_names.hpp"
|
||||
#include "rfl/internal/get_struct_name.hpp"
|
||||
#include "rfl/internal/has_fields.hpp"
|
||||
#include "rfl/internal/has_reflection_method_v.hpp"
|
||||
#include "rfl/internal/has_reflection_type_v.hpp"
|
||||
#include "rfl/internal/has_tag_v.hpp"
|
||||
#include "rfl/internal/is_basic_type.hpp"
|
||||
#include "rfl/internal/move_from_tuple.hpp"
|
||||
#include "rfl/internal/no_duplicate_field_names.hpp"
|
||||
@@ -1067,23 +1069,26 @@ struct Parser<R, W, TaggedUnion<_discriminator, AlternativeTypes...>> {
|
||||
template <class T>
|
||||
static inline bool contains_disc_value(
|
||||
const std::string& _disc_value) noexcept {
|
||||
if constexpr (!internal::has_reflection_type_v<T>) {
|
||||
if constexpr (internal::has_reflection_type_v<T>) {
|
||||
return contains_disc_value<typename T::ReflectionType>(_disc_value);
|
||||
} else if constexpr (internal::has_tag_v<T>) {
|
||||
using LiteralType = typename T::Tag;
|
||||
return LiteralType::contains(_disc_value);
|
||||
} else {
|
||||
using LiteralType = typename T::ReflectionType::Tag;
|
||||
return LiteralType::contains(_disc_value);
|
||||
} else {
|
||||
return _disc_value == internal::get_struct_name<T>();
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
static inline auto make_tag() noexcept {
|
||||
if constexpr (!internal::has_reflection_type_v<T>) {
|
||||
static inline std::string make_tag() noexcept {
|
||||
if constexpr (internal::has_reflection_type_v<T>) {
|
||||
return make_tag<typename T::ReflectionType>();
|
||||
} else if constexpr (internal::has_tag_v<T>) {
|
||||
using LiteralType = typename T::Tag;
|
||||
return LiteralType::template name_of<0>();
|
||||
return LiteralType::template name_of<0>().str();
|
||||
} else {
|
||||
using LiteralType = typename T::ReflectionType::Tag;
|
||||
return LiteralType::template name_of<0>();
|
||||
return internal::get_struct_name<T>();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#ifndef RFL_PARSING_TAGGEDUNIONWRAPPER_HPP_
|
||||
#define RFL_PARSING_TAGGEDUNIONWRAPPER_HPP_
|
||||
|
||||
#include <concepts>
|
||||
#include <string>
|
||||
|
||||
#include "rfl/Flatten.hpp"
|
||||
#include "rfl/Rename.hpp"
|
||||
@@ -12,13 +12,13 @@ namespace parsing {
|
||||
|
||||
template <class T, class LiteralType, internal::StringLiteral _discriminator>
|
||||
struct TaggedUnionWrapperNoFields {
|
||||
rfl::Rename<_discriminator, LiteralType> tag;
|
||||
rfl::Rename<_discriminator, std::string> tag;
|
||||
rfl::Flatten<const T*> fields;
|
||||
};
|
||||
|
||||
template <class T, class LiteralType, internal::StringLiteral _discriminator>
|
||||
struct TaggedUnionWrapperWithFields {
|
||||
rfl::Field<_discriminator, LiteralType> tag;
|
||||
rfl::Field<_discriminator, std::string> tag;
|
||||
rfl::Flatten<const T*> fields;
|
||||
};
|
||||
|
||||
|
||||
@@ -10,19 +10,16 @@
|
||||
namespace test_tagged_union {
|
||||
|
||||
struct Circle {
|
||||
using Tag = rfl::Literal<"Circle">;
|
||||
rfl::Field<"radius", double> radius;
|
||||
double radius;
|
||||
};
|
||||
|
||||
struct Rectangle {
|
||||
using Tag = rfl::Literal<"Rectangle">;
|
||||
rfl::Field<"height", double> height;
|
||||
rfl::Field<"width", double> width;
|
||||
double height;
|
||||
double width;
|
||||
};
|
||||
|
||||
struct Square {
|
||||
using Tag = rfl::Literal<"Square">;
|
||||
rfl::Field<"width", double> width;
|
||||
double width;
|
||||
};
|
||||
|
||||
using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>;
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_tagged_union2 {
|
||||
|
||||
struct Circle {
|
||||
using Tag = rfl::Literal<"Circle">;
|
||||
rfl::Field<"radius", double> radius;
|
||||
};
|
||||
|
||||
struct Rectangle {
|
||||
using Tag = rfl::Literal<"Rectangle">;
|
||||
rfl::Field<"height", double> height;
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
|
||||
struct Square {
|
||||
using Tag = rfl::Literal<"Square">;
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
|
||||
using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>;
|
||||
|
||||
void test() {
|
||||
std::cout << "test_tagged_union2" << std::endl;
|
||||
|
||||
const Shapes r = Rectangle{.height = 10, .width = 5};
|
||||
|
||||
write_and_read(r, R"({"shape":"Rectangle","height":10.0,"width":5.0})");
|
||||
}
|
||||
} // namespace test_tagged_union2
|
||||
@@ -44,6 +44,7 @@
|
||||
#include "test_string_map.hpp"
|
||||
#include "test_string_unordered_map.hpp"
|
||||
#include "test_tagged_union.hpp"
|
||||
#include "test_tagged_union2.hpp"
|
||||
#include "test_timestamp.hpp"
|
||||
#include "test_unique_ptr.hpp"
|
||||
#include "test_unique_ptr2.hpp"
|
||||
@@ -62,6 +63,7 @@ int main() {
|
||||
test_literal::test();
|
||||
test_variant::test();
|
||||
test_tagged_union::test();
|
||||
test_tagged_union2::test();
|
||||
test_field_variant::test();
|
||||
test_ref::test();
|
||||
test_box::test();
|
||||
|
||||
Reference in New Issue
Block a user