mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 08:46:19 +08:00
@@ -34,6 +34,7 @@ The resulting JSON string looks like this:
|
||||
reflect-cpp currently supports the following processors:
|
||||
|
||||
- `rfl::AddStructName`
|
||||
- `rfl::AddTagsToVariants`
|
||||
- `rfl::AllowRawPtrs`
|
||||
- `rfl::DefaultIfMissing`
|
||||
- `rfl::NoExtraFields`
|
||||
@@ -61,6 +62,58 @@ The resulting JSON string looks like this:
|
||||
{"type":"Person","first_name":"Homer","last_name":"Simpson","age":45}
|
||||
```
|
||||
|
||||
### `rfl::AddStructsToVariants`
|
||||
|
||||
This processor automatically adds structs to variants. Consider the following example:
|
||||
|
||||
```cpp
|
||||
struct button_pressed_t {};
|
||||
|
||||
struct button_released_t {};
|
||||
|
||||
struct key_pressed_t {
|
||||
char key;
|
||||
};
|
||||
|
||||
using my_event_type_t =
|
||||
std::variant<button_pressed_t, button_released_t, key_pressed_t, int>;
|
||||
```
|
||||
|
||||
The problem here is that `button_pressed_t` and `butten_released_t` virtually look
|
||||
indistinguishable. The will both be serialized to `{}`.
|
||||
|
||||
But you can add this processor to automatically add tags and avoid the problem:
|
||||
|
||||
```cpp
|
||||
const auto vec = std::vector<my_event_type_t>(
|
||||
{button_pressed_t{}, button_released_t{}, key_pressed_t{'c'}, 3});
|
||||
|
||||
const auto json_string = rfl::json::write<rfl::AddTagsToVariants>(vec);
|
||||
|
||||
rfl::json::write<std::vector<my_event_type_t>, rfl::AddTagsToVariants>(json_string);
|
||||
```
|
||||
|
||||
`vec` will now be serialized as follows:
|
||||
|
||||
```json
|
||||
[{"button_pressed_t":{}},{"button_released_t":{}},{"key_pressed_t":{"key":99}},{"int":3}]
|
||||
```
|
||||
|
||||
You can also set your own custom tags like this:
|
||||
|
||||
```cpp
|
||||
struct key_pressed_t {
|
||||
using Tag = rfl::Literal<"your_custom_tag">;
|
||||
char key;
|
||||
};
|
||||
```
|
||||
|
||||
`key_pressed_t` will now be serialized as follows:
|
||||
|
||||
```json
|
||||
{"your_custom_tag":{"key":99}}
|
||||
```
|
||||
|
||||
### `rfl::AllowRawPtrs`
|
||||
|
||||
By default, reflect-cpp does not allow *reading into* raw pointers. (*Writing from* raw pointers is never a problem.) This is because reading into raw pointers means that the library will allocate memory that the user then has to manually delete. This can lead to misunderstandings and memory leaks.
|
||||
|
||||
@@ -45,9 +45,21 @@ several problems with this:
|
||||
2) It leads to confusing error messages: If none of the alternatives can be matched, you will get an error message telling you why each of the alternatives couldn't be matched. Such error messages are very long-winding and hard to read.
|
||||
3) It is dangerous. Imagine we had written `rfl::Variant<Circle, Square, Rectangle>` instead of `rfl::Variant<Circle, Rectangle, Square>`. This would mean that `Rectangle` could never be matched, because the fields in `Square` are a subset of `Rectangle`. This leads to very confusing bugs.
|
||||
|
||||
## Automatic tags
|
||||
|
||||
The easiest way to solve this problem is to simply add tags automatically. You can do so by using `rfl::AddTagsToVariants`:
|
||||
|
||||
```cpp
|
||||
const auto json_string = rfl::json::write<rfl::AddTagsToVariants>(r);
|
||||
|
||||
const auto r2 = rfl::json::read<Shapes, rfl::AddTagsToVariants>(json_string);
|
||||
```
|
||||
|
||||
Please refer to the section on processors in this documentation for more information.
|
||||
|
||||
## `rfl::TaggedUnion` (internally tagged)
|
||||
|
||||
One way to solve this problem is to add a tag inside the class. That is why we have provided a helper class for these purposes: `rfl::TaggedUnion`.
|
||||
Another way to solve this problem is to add a tag inside the class. That is why we have provided a helper class for these purposes: `rfl::TaggedUnion`.
|
||||
|
||||
TaggedUnions use the name of the struct as an identifying tag. It will then try to take that field from the JSON object, match it to the correct alternative and then only parse the correct alternative.
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#endif
|
||||
|
||||
#include "rfl/AddStructName.hpp"
|
||||
#include "rfl/AddTagsToVariants.hpp"
|
||||
#include "rfl/AllOf.hpp"
|
||||
#include "rfl/AllowRawPtrs.hpp"
|
||||
#include "rfl/AnyOf.hpp"
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
#ifndef RFL_ADDTAGSTOVARIANTS_HPP_
|
||||
#define RFL_ADDTAGSTOVARIANTS_HPP_
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// This is a "fake" processor - it doesn't do much in itself, but its
|
||||
/// inclusion instructs the parsers to automatically add tags to the variants
|
||||
/// they might encounter.
|
||||
struct AddTagsToVariants {
|
||||
public:
|
||||
template <class StructType>
|
||||
static auto process(auto&& _named_tuple) {
|
||||
return _named_tuple;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "internal/is_add_tags_to_variants_v.hpp"
|
||||
#include "internal/is_allow_raw_ptrs_v.hpp"
|
||||
#include "internal/is_default_if_missing_v.hpp"
|
||||
#include "internal/is_no_extra_fields_v.hpp"
|
||||
@@ -17,6 +18,7 @@ struct Processors;
|
||||
|
||||
template <>
|
||||
struct Processors<> {
|
||||
static constexpr bool add_tags_to_variants_ = false;
|
||||
static constexpr bool allow_raw_ptrs_ = false;
|
||||
static constexpr bool all_required_ = false;
|
||||
static constexpr bool default_if_missing_ = false;
|
||||
@@ -32,6 +34,10 @@ struct Processors<> {
|
||||
|
||||
template <class Head, class... Tail>
|
||||
struct Processors<Head, Tail...> {
|
||||
static constexpr bool add_tags_to_variants_ =
|
||||
std::disjunction_v<internal::is_add_tags_to_variants<Head>,
|
||||
internal::is_add_tags_to_variants<Tail>...>;
|
||||
|
||||
static constexpr bool allow_raw_ptrs_ =
|
||||
std::disjunction_v<internal::is_allow_raw_ptrs<Head>,
|
||||
internal::is_allow_raw_ptrs<Tail>...>;
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
#ifndef RFL_INTERNAL_ISADDTAGSTOVARIANTS_HPP_
|
||||
#define RFL_INTERNAL_ISADDTAGSTOVARIANTS_HPP_
|
||||
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include "../AddTagsToVariants.hpp"
|
||||
|
||||
namespace rfl ::internal {
|
||||
|
||||
template <class T>
|
||||
class is_add_tags_to_variants;
|
||||
|
||||
template <class T>
|
||||
class is_add_tags_to_variants : public std::false_type {};
|
||||
|
||||
template <>
|
||||
class is_add_tags_to_variants<AddTagsToVariants> : public std::true_type {};
|
||||
|
||||
template <class T>
|
||||
constexpr bool is_add_tags_to_variants_v = is_add_tags_to_variants<
|
||||
std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
|
||||
|
||||
} // namespace rfl::internal
|
||||
|
||||
#endif
|
||||
@@ -9,8 +9,7 @@
|
||||
namespace rfl::internal {
|
||||
|
||||
template <internal::StringLiteral _discriminator, class T>
|
||||
using tag_t =
|
||||
typename std::invoke_result<decltype(make_tag<_discriminator, T>), T>::type;
|
||||
using tag_t = std::invoke_result_t<decltype(make_tag<_discriminator, T>), T>;
|
||||
|
||||
} // namespace rfl::internal
|
||||
|
||||
|
||||
@@ -64,14 +64,12 @@ struct FieldVariantParser {
|
||||
"Externally tagged variants cannot have duplicate field "
|
||||
"names.");
|
||||
|
||||
const auto handle = [&](const auto& _field) {
|
||||
_v.visit([&](const auto& _field) {
|
||||
const auto named_tuple = make_named_tuple(internal::to_ptr_field(_field));
|
||||
using NamedTupleType = std::remove_cvref_t<decltype(named_tuple)>;
|
||||
Parser<R, W, NamedTupleType, ProcessorsType>::write(_w, named_tuple,
|
||||
_parent);
|
||||
};
|
||||
|
||||
rfl::visit(handle, _v);
|
||||
});
|
||||
}
|
||||
|
||||
static schema::Type to_schema(
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "../internal/nth_element_t.hpp"
|
||||
#include "FieldVariantParser.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
#include "VariantAlternativeWrapper.hpp"
|
||||
#include "schema/Type.hpp"
|
||||
|
||||
namespace rfl::parsing {
|
||||
@@ -26,6 +27,19 @@ class Parser<R, W, rfl::Variant<AlternativeTypes...>, ProcessorsType> {
|
||||
if constexpr (internal::all_fields<std::tuple<AlternativeTypes...>>()) {
|
||||
return FieldVariantParser<R, W, ProcessorsType,
|
||||
AlternativeTypes...>::read(_r, _var);
|
||||
|
||||
} else if constexpr (ProcessorsType::add_tags_to_variants_) {
|
||||
using FieldVariantType =
|
||||
rfl::Variant<VariantAlternativeWrapper<AlternativeTypes>...>;
|
||||
const auto from_field_variant =
|
||||
[](const auto& _field) -> rfl::Variant<AlternativeTypes...> {
|
||||
return std::move(_field.value());
|
||||
};
|
||||
return Parser<R, W, FieldVariantType, ProcessorsType>::read(_r, _var)
|
||||
.transform([&](FieldVariantType&& _f) {
|
||||
return _f.visit(from_field_variant);
|
||||
});
|
||||
|
||||
} else {
|
||||
std::optional<rfl::Variant<AlternativeTypes...>> result;
|
||||
std::vector<Error> errors;
|
||||
@@ -52,6 +66,17 @@ class Parser<R, W, rfl::Variant<AlternativeTypes...>, ProcessorsType> {
|
||||
if constexpr (internal::all_fields<std::tuple<AlternativeTypes...>>()) {
|
||||
FieldVariantParser<R, W, ProcessorsType, AlternativeTypes...>::write(
|
||||
_w, _variant, _parent);
|
||||
|
||||
} else if constexpr (ProcessorsType::add_tags_to_variants_) {
|
||||
using FieldVariantType =
|
||||
rfl::Variant<VariantAlternativeWrapper<const AlternativeTypes*>...>;
|
||||
const auto to_field_variant =
|
||||
[]<class T>(const T& _t) -> FieldVariantType {
|
||||
return VariantAlternativeWrapper<const T*>(&_t);
|
||||
};
|
||||
Parser<R, W, FieldVariantType, ProcessorsType>::write(
|
||||
_w, _variant.visit(to_field_variant), _parent);
|
||||
|
||||
} else {
|
||||
const auto handle = [&](const auto& _v) {
|
||||
using Type = std::remove_cvref_t<decltype(_v)>;
|
||||
@@ -66,6 +91,13 @@ class Parser<R, W, rfl::Variant<AlternativeTypes...>, ProcessorsType> {
|
||||
if constexpr (internal::all_fields<std::tuple<AlternativeTypes...>>()) {
|
||||
return FieldVariantParser<R, W, ProcessorsType,
|
||||
AlternativeTypes...>::to_schema(_definitions);
|
||||
|
||||
} else if constexpr (ProcessorsType::add_tags_to_variants_) {
|
||||
using FieldVariantType =
|
||||
rfl::Variant<VariantAlternativeWrapper<AlternativeTypes>...>;
|
||||
return Parser<R, W, FieldVariantType, ProcessorsType>::to_schema(
|
||||
_definitions);
|
||||
|
||||
} else {
|
||||
std::vector<schema::Type> types;
|
||||
build_schema(
|
||||
|
||||
@@ -13,11 +13,11 @@
|
||||
#include "../internal/to_ptr_field.hpp"
|
||||
#include "FieldVariantParser.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
#include "VariantAlternativeWrapper.hpp"
|
||||
#include "schema/Type.hpp"
|
||||
#include "to_single_error_message.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
namespace rfl::parsing {
|
||||
|
||||
template <class R, class W, class... AlternativeTypes, class ProcessorsType>
|
||||
requires AreReaderAndWriter<R, W, std::variant<AlternativeTypes...>>
|
||||
@@ -41,6 +41,19 @@ class Parser<R, W, std::variant<AlternativeTypes...>, ProcessorsType> {
|
||||
return FieldVariantParser<R, W, ProcessorsType,
|
||||
AlternativeTypes...>::read(_r, _var)
|
||||
.transform(to_std_variant);
|
||||
|
||||
} else if constexpr (ProcessorsType::add_tags_to_variants_) {
|
||||
using FieldVariantType =
|
||||
rfl::Variant<VariantAlternativeWrapper<AlternativeTypes>...>;
|
||||
const auto from_field_variant =
|
||||
[](auto&& _field) -> std::variant<AlternativeTypes...> {
|
||||
return std::move(_field.value());
|
||||
};
|
||||
return Parser<R, W, FieldVariantType, ProcessorsType>::read(_r, _var)
|
||||
.transform([&](FieldVariantType&& _f) {
|
||||
return _f.visit(from_field_variant);
|
||||
});
|
||||
|
||||
} else {
|
||||
std::optional<std::variant<AlternativeTypes...>> result;
|
||||
std::vector<Error> errors;
|
||||
@@ -76,6 +89,16 @@ class Parser<R, W, std::variant<AlternativeTypes...>, ProcessorsType> {
|
||||
R, W, ProcessorsType,
|
||||
ptr_field_t<AlternativeTypes>...>::write(_w, to_rfl_variant(_variant),
|
||||
_parent);
|
||||
} else if constexpr (ProcessorsType::add_tags_to_variants_) {
|
||||
using FieldVariantType =
|
||||
rfl::Variant<VariantAlternativeWrapper<const AlternativeTypes*>...>;
|
||||
const auto to_field_variant =
|
||||
[]<class T>(const T& _t) -> FieldVariantType {
|
||||
return VariantAlternativeWrapper<const T*>(&_t);
|
||||
};
|
||||
Parser<R, W, FieldVariantType, ProcessorsType>::write(
|
||||
_w, std::visit(to_field_variant, _variant), _parent);
|
||||
|
||||
} else {
|
||||
const auto handle = [&](const auto& _v) {
|
||||
using Type = std::remove_cvref_t<decltype(_v)>;
|
||||
@@ -90,6 +113,13 @@ class Parser<R, W, std::variant<AlternativeTypes...>, ProcessorsType> {
|
||||
if constexpr (internal::all_fields<std::tuple<AlternativeTypes...>>()) {
|
||||
return FieldVariantParser<R, W, ProcessorsType,
|
||||
AlternativeTypes...>::to_schema(_definitions);
|
||||
|
||||
} else if constexpr (ProcessorsType::add_tags_to_variants_) {
|
||||
using FieldVariantType =
|
||||
rfl::Variant<VariantAlternativeWrapper<AlternativeTypes>...>;
|
||||
return Parser<R, W, FieldVariantType, ProcessorsType>::to_schema(
|
||||
_definitions);
|
||||
|
||||
} else {
|
||||
std::vector<schema::Type> types;
|
||||
build_schema(
|
||||
@@ -142,7 +172,6 @@ class Parser<R, W, std::variant<AlternativeTypes...>, ProcessorsType> {
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
} // namespace rfl::parsing
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
#ifndef RFL_PARSING_VARIANTALTERNATIVEWRAPPER_HPP_
|
||||
#define RFL_PARSING_VARIANTALTERNATIVEWRAPPER_HPP_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Field.hpp"
|
||||
#include "../Literal.hpp"
|
||||
#include "../internal/StringLiteral.hpp"
|
||||
#include "../internal/get_type_name.hpp"
|
||||
#include "../internal/has_tag_v.hpp"
|
||||
#include "../internal/remove_namespaces.hpp"
|
||||
|
||||
namespace rfl::parsing {
|
||||
|
||||
namespace vaw {
|
||||
|
||||
template <class T>
|
||||
struct GetName {};
|
||||
|
||||
template <internal::StringLiteral _name>
|
||||
struct GetName<Literal<_name>> {
|
||||
constexpr static internal::StringLiteral name_ = _name;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
consteval auto make_tag() {
|
||||
if constexpr (internal::has_tag_v<T>) {
|
||||
return typename T::Tag();
|
||||
} else {
|
||||
return Literal<
|
||||
internal::remove_namespaces<internal::get_type_name<T>()>()>();
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
using tag_t = std::invoke_result_t<
|
||||
decltype(make_tag<std::remove_cvref_t<std::remove_pointer_t<T>>>)>;
|
||||
|
||||
} // namespace vaw
|
||||
|
||||
template <class T>
|
||||
using VariantAlternativeWrapper = Field<vaw::GetName<vaw::tag_t<T>>::name_, T>;
|
||||
|
||||
} // namespace rfl::parsing
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,33 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_add_tag_to_rfl_variant {
|
||||
|
||||
struct button_pressed_t {};
|
||||
|
||||
struct button_released_t {};
|
||||
|
||||
struct key_pressed_t {
|
||||
using Tag = rfl::Literal<"key_pressed">;
|
||||
char key;
|
||||
};
|
||||
|
||||
using my_event_type_t =
|
||||
rfl::Variant<button_pressed_t, button_released_t, key_pressed_t, int>;
|
||||
|
||||
TEST(json, test_add_tag_to_rfl_variant) {
|
||||
const auto vec = std::vector<my_event_type_t>(
|
||||
{button_pressed_t{}, button_released_t{}, key_pressed_t{'c'}, 3});
|
||||
|
||||
write_and_read<rfl::AddTagsToVariants>(
|
||||
vec,
|
||||
R"([{"button_pressed_t":{}},{"button_released_t":{}},{"key_pressed":{"key":99}},{"int":3}])");
|
||||
}
|
||||
} // namespace test_add_tag_to_rfl_variant
|
||||
@@ -0,0 +1,33 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_add_tag_to_variant {
|
||||
|
||||
struct button_pressed_t {};
|
||||
|
||||
struct button_released_t {};
|
||||
|
||||
struct key_pressed_t {
|
||||
using Tag = rfl::Literal<"key_pressed">;
|
||||
char key;
|
||||
};
|
||||
|
||||
using my_event_type_t =
|
||||
std::variant<button_pressed_t, button_released_t, key_pressed_t, int>;
|
||||
|
||||
TEST(json, test_add_tag_to_variant) {
|
||||
const auto vec = std::vector<my_event_type_t>(
|
||||
{button_pressed_t{}, button_released_t{}, key_pressed_t{'c'}, 3});
|
||||
|
||||
write_and_read<rfl::AddTagsToVariants>(
|
||||
vec,
|
||||
R"([{"button_pressed_t":{}},{"button_released_t":{}},{"key_pressed":{"key":99}},{"int":3}])");
|
||||
}
|
||||
} // namespace test_add_tag_to_variant
|
||||
Reference in New Issue
Block a user