mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 18:56:19 +08:00
Added code and first tests
This commit is contained in:
@@ -30,3 +30,10 @@
|
||||
*.exe
|
||||
*.out
|
||||
*.app
|
||||
|
||||
# CMake
|
||||
**/CMakeCache.txt
|
||||
**/Makefile
|
||||
**/*.cmake
|
||||
**/CMakeFiles/**
|
||||
**/reflect-cpp-tests
|
||||
|
||||
@@ -27,53 +27,59 @@ Design principles for reflect-cpp include:
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <rfl>
|
||||
#include <rfl/json>
|
||||
#include <rfl/json.hpp>
|
||||
#include <rfl.hpp>
|
||||
|
||||
// "firstName", "lastName" and "children" are the field names
|
||||
// as they will appear in the JSON. The C++ standard is
|
||||
// snake case, the JSON standard is camel case, so the names
|
||||
// will not always be identical.
|
||||
struct Person {
|
||||
rfl::Field<std::string, "firstName"> first_name;
|
||||
rfl::Field<std::string, "lastName"> last_name;
|
||||
rfl::Field<std::vector<Person>, "children"> children;
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name;
|
||||
rfl::Field<"children", std::vector<Person>> children;
|
||||
};
|
||||
|
||||
const auto bart = Person{
|
||||
.first_name = "Bart",
|
||||
.last_name = "Simpson",
|
||||
.children = std::vector<Person>()
|
||||
};
|
||||
const auto bart = Person{.first_name = "Bart",
|
||||
.last_name = "Simpson",
|
||||
.children = std::vector<Person>()};
|
||||
|
||||
const auto lisa = Person{
|
||||
.first_name = "Lisa",
|
||||
.last_name = "Simpson",
|
||||
.children = rfl::default_value // same as std::vector<Person>()
|
||||
.first_name = "Lisa",
|
||||
.last_name = "Simpson",
|
||||
.children = rfl::default_value // same as std::vector<Person>()
|
||||
};
|
||||
|
||||
// Returns a deep copy of the original object,
|
||||
// replacing first_name.
|
||||
const auto maggie = rfl::replace(
|
||||
lisa, rfl::make_field<"firstName">("Maggie"));
|
||||
const auto maggie =
|
||||
rfl::replace(lisa, rfl::make_field<"firstName">(std::string("Maggie")));
|
||||
|
||||
const auto homer = Person{
|
||||
.first_name = "Homer",
|
||||
.last_name = "Simpson",
|
||||
.children = std::vector<Person>({bart, lisa, maggie})
|
||||
};
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer",
|
||||
.last_name = "Simpson",
|
||||
.children = std::vector<Person>({bart, lisa, maggie})};
|
||||
|
||||
// We can now transform this into a JSON string.
|
||||
const std::string json_string = rfl::json::write(homer);
|
||||
|
||||
// {"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson","children":[]},{"firstName":"Lisa","lastName":"Simpson","children":[]},{"firstName":"Maggie","lastName":"Simpson","children":[]}]}
|
||||
std::cout << json_string << std::endl;
|
||||
|
||||
// And we can parse the string to recreate the struct.
|
||||
auto homer2 = rfl::json::read<Person>(json_string).value();
|
||||
|
||||
// Fields can be accessed like this:
|
||||
std::cout << "Hello, my name is " << homer.first_name() << " " << homer.last_name() << "." << std::endl;
|
||||
std::cout << "Hello, my name is " << homer.first_name() << " "
|
||||
<< homer.last_name() << "." << std::endl;
|
||||
|
||||
// Since homer2 is mutable, we can also change the values like this:
|
||||
homer2.first_name = "Marge";
|
||||
|
||||
std::cout << "Hello, my name is " << homer2.first_name() << " "
|
||||
<< homer2.last_name() << "." << std::endl;
|
||||
|
||||
|
||||
```
|
||||
|
||||
## Support for containers
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
#ifndef RFL_RFL_HPP_
|
||||
#define RFL_RFL_HPP_
|
||||
|
||||
#include "rfl/Box.hpp"
|
||||
#include "rfl/Enum.hpp"
|
||||
#include "rfl/Field.hpp"
|
||||
#include "rfl/Flatten.hpp"
|
||||
#include "rfl/Literal.hpp"
|
||||
#include "rfl/NamedTuple.hpp"
|
||||
#include "rfl/Ref.hpp"
|
||||
#include "rfl/TaggedUnion.hpp"
|
||||
#include "rfl/always_false.hpp"
|
||||
#include "rfl/as.hpp"
|
||||
#include "rfl/default.hpp"
|
||||
#include "rfl/define_literal.hpp"
|
||||
#include "rfl/define_named_tuple.hpp"
|
||||
#include "rfl/define_tagged_union.hpp"
|
||||
#include "rfl/define_variant.hpp"
|
||||
#include "rfl/extract_discriminators.hpp"
|
||||
#include "rfl/field_type.hpp"
|
||||
#include "rfl/from_named_tuple.hpp"
|
||||
#include "rfl/get.hpp"
|
||||
#include "rfl/make_named_tuple.hpp"
|
||||
#include "rfl/name_t.hpp"
|
||||
#include "rfl/named_tuple_t.hpp"
|
||||
#include "rfl/remove_fields.hpp"
|
||||
#include "rfl/replace.hpp"
|
||||
#include "rfl/to_named_tuple.hpp"
|
||||
#include "rfl/visit.hpp"
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,47 @@
|
||||
#ifndef RFL_BOX_HPP_
|
||||
#define RFL_BOX_HPP_
|
||||
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// The Box class behaves very similarly to the unique_ptr, but unlike the
|
||||
/// unique_ptr, it is 100% guaranteed to be filled at all times.
|
||||
template <class T>
|
||||
class Box {
|
||||
public:
|
||||
/// The only way of creating new boxes is
|
||||
/// Box<T>::make(...).
|
||||
template <class... Args>
|
||||
static Box<T> make(Args... _args) {
|
||||
return Box<T>(std::make_unique<T>(_args...));
|
||||
}
|
||||
|
||||
~Box() = default;
|
||||
|
||||
/// Returns a pointer to the underlying object
|
||||
inline T* get() const { return ptr_.get(); }
|
||||
|
||||
/// Returns the underlying object.
|
||||
inline T& operator*() const { return *ptr_; }
|
||||
|
||||
/// Returns the underlying object.
|
||||
inline T* operator->() const { return ptr_.get(); }
|
||||
|
||||
/// Returns the underlying unique_ptr
|
||||
inline const std::unique_ptr<T>& ptr() const { return ptr_; }
|
||||
|
||||
private:
|
||||
/// Only make is allowed to use this constructor.
|
||||
explicit Box(std::unique_ptr<T>&& _ptr)
|
||||
: ptr_(std::forward<std::unique_ptr<T>>(_ptr)) {}
|
||||
|
||||
private:
|
||||
/// The underlying unique_ptr_
|
||||
std::unique_ptr<T> ptr_;
|
||||
};
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,60 @@
|
||||
#ifndef RFL_ENUM_HPP_
|
||||
#define RFL_ENUM_HPP_
|
||||
|
||||
#include <tuple>
|
||||
#include <variant>
|
||||
|
||||
#include "rfl/NamedTuple.hpp"
|
||||
#include "rfl/internal/StringLiteral.hpp"
|
||||
#include "rfl/internal/no_duplicate_field_names.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
template <class... FieldTypes>
|
||||
struct Enum {
|
||||
using Fields = std::tuple<FieldTypes...>;
|
||||
|
||||
/// The type of the underlying variant.
|
||||
using VariantType = std::variant<FieldTypes...>;
|
||||
|
||||
Enum(const VariantType& _variant) : variant_(_variant) {
|
||||
static_assert(internal::no_duplicate_field_names<Fields>(),
|
||||
"Duplicate field names are not allowed");
|
||||
}
|
||||
|
||||
Enum(VariantType&& _variant)
|
||||
: variant_(std::forward<VariantType>(_variant)) {
|
||||
static_assert(internal::no_duplicate_field_names<Fields>(),
|
||||
"Duplicate field names are not allowed");
|
||||
}
|
||||
|
||||
~Enum() = default;
|
||||
|
||||
/// Assigns the underlying variant.
|
||||
inline void operator=(const VariantType& _variant) {
|
||||
static_assert(internal::no_duplicate_field_names<Fields>(),
|
||||
"Duplicate field names are not allowed");
|
||||
variant_ = _variant;
|
||||
}
|
||||
|
||||
/// Assigns the underlying variant.
|
||||
inline void operator=(VariantType&& _variant) {
|
||||
static_assert(internal::no_duplicate_field_names<Fields>(),
|
||||
"Duplicate field names are not allowed");
|
||||
variant_ = std::forward<VariantType>(_variant);
|
||||
}
|
||||
|
||||
/// Returns the underlying variant.
|
||||
inline VariantType& variant() { return variant_; }
|
||||
|
||||
/// Returns the underlying variant.
|
||||
inline const VariantType& variant() const { return variant_; }
|
||||
|
||||
/// The underlying variant - an Enum is a thin wrapper
|
||||
/// around a variant that is mainly used for parsing.
|
||||
VariantType variant_;
|
||||
};
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif // RFL_TAGGEDUNION_HPP_
|
||||
@@ -0,0 +1,140 @@
|
||||
#ifndef RFL_FIELD_HPP_
|
||||
#define RFL_FIELD_HPP_
|
||||
|
||||
#include <algorithm>
|
||||
#include <string_view>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include "rfl/Literal.hpp"
|
||||
#include "rfl/default.hpp"
|
||||
#include "rfl/internal/StringLiteral.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// Used to define a field in the NamedTuple.
|
||||
template <internal::StringLiteral _name, class _Type>
|
||||
struct Field {
|
||||
/// The underlying type.
|
||||
using Type = std::decay_t<_Type>;
|
||||
|
||||
/// The name of the field.
|
||||
using Name = rfl::Literal<_name>;
|
||||
|
||||
Field(const Type& _value) : value_(_value) {}
|
||||
|
||||
Field(Type&& _value) : value_(std::forward<Type>(_value)) {}
|
||||
|
||||
Field(const Field<_name, Type>& _field) : value_(_field.get()) {}
|
||||
|
||||
Field(Field<_name, Type>&& _field)
|
||||
: value_(std::forward<Type>(_field.value_)) {}
|
||||
|
||||
template <class T>
|
||||
Field(const Field<_name, T>& _field) : value_(_field.get()) {}
|
||||
|
||||
template <class T>
|
||||
Field(Field<_name, T>&& _field) : value_(_field.get()) {}
|
||||
|
||||
template <class T, typename std::enable_if<std::is_convertible_v<T, Type>,
|
||||
bool>::type = true>
|
||||
Field(const T& _value) : value_(_value) {}
|
||||
|
||||
template <class T, typename std::enable_if<std::is_convertible_v<T, Type>,
|
||||
bool>::type = true>
|
||||
Field(T&& _value) : value_(_value) {}
|
||||
|
||||
template <class T, typename std::enable_if<std::is_convertible_v<T, Type>,
|
||||
bool>::type = true>
|
||||
Field(const Field<_name, T>& _field) : value_(_field.value()) {}
|
||||
|
||||
/// Assigns the underlying object to its default value.
|
||||
template <class T = Type,
|
||||
typename std::enable_if<std::is_default_constructible_v<T>,
|
||||
bool>::type = true>
|
||||
Field(const Default& _default) : value_(Type()) {}
|
||||
|
||||
~Field() = default;
|
||||
|
||||
/// The name of the field, for internal use.
|
||||
constexpr static const internal::StringLiteral name_ = _name;
|
||||
|
||||
/// Returns the underlying object.
|
||||
inline const Type& get() const { return value_; }
|
||||
|
||||
/// Returns the underlying object.
|
||||
inline Type& operator()() { return value_; }
|
||||
|
||||
/// Returns the underlying object.
|
||||
inline const Type& operator()() const { return value_; }
|
||||
|
||||
/// Assigns the underlying object.
|
||||
inline void operator=(const Type& _value) { value_ = _value; }
|
||||
|
||||
/// Assigns the underlying object.
|
||||
inline void operator=(Type&& _value) {
|
||||
value_ = std::forward<Type>(_value);
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class T, typename std::enable_if<std::is_convertible_v<T, Type>,
|
||||
bool>::type = true>
|
||||
inline void operator=(const T& _value) {
|
||||
value_ = _value;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object to its default value.
|
||||
template <class T = Type,
|
||||
typename std::enable_if<std::is_default_constructible_v<T>,
|
||||
bool>::type = true>
|
||||
inline void operator=(const Default& _default) {
|
||||
value_ = Type();
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
inline void operator=(const Field<_name, Type>& _field) {
|
||||
value_ = _field.get();
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
inline void operator=(Field<_name, Type>&& _field) {
|
||||
value_ = _field.get();
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class T>
|
||||
inline void operator=(const Field<_name, T>& _field) {
|
||||
value_ = _field.get();
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class T>
|
||||
inline void operator=(Field<_name, T>&& _field) {
|
||||
value_ = _field.get();
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
inline void set(const Type& _value) { value_ = _value; }
|
||||
|
||||
/// Assigns the underlying object.
|
||||
inline void set(Type&& _value) { value_ = std::forward<Type>(_value); }
|
||||
|
||||
/// Returns the underlying object.
|
||||
inline Type& value() { return value_; }
|
||||
|
||||
/// Returns the underlying object.
|
||||
inline const Type& value() const { return value_; }
|
||||
|
||||
/// The underlying value.
|
||||
Type value_;
|
||||
};
|
||||
|
||||
template <internal::StringLiteral _name, class _Type>
|
||||
inline auto make_field(const _Type& _value) {
|
||||
return Field<_name, _Type>(_value);
|
||||
}
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif // RFL_FIELD_HPP_
|
||||
@@ -0,0 +1,98 @@
|
||||
#ifndef RFL_FLATTEN_HPP_
|
||||
#define RFL_FLATTEN_HPP_
|
||||
|
||||
#include <algorithm>
|
||||
#include <string_view>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// Used to embed another struct into the generated output.
|
||||
template <class _Type>
|
||||
struct Flatten {
|
||||
/// The underlying type.
|
||||
using Type = std::decay_t<_Type>;
|
||||
|
||||
Flatten(const Type& _value) : value_(_value) {}
|
||||
|
||||
Flatten(Type&& _value) : value_(std::forward<Type>(_value)) {}
|
||||
|
||||
Flatten(const Flatten<Type>& _f) : value_(_f.get()) {}
|
||||
|
||||
Flatten(Flatten<Type>&& _f) : value_(std::forward<Type>(_f.value_)) {}
|
||||
|
||||
template <class T>
|
||||
Flatten(const Flatten<T>& _f) : value_(_f.get()) {}
|
||||
|
||||
template <class T>
|
||||
Flatten(Flatten<T>&& _f) : value_(_f.get()) {}
|
||||
|
||||
template <class T, typename std::enable_if<std::is_convertible_v<T, Type>,
|
||||
bool>::type = true>
|
||||
Flatten(const T& _value) : value_(_value) {}
|
||||
|
||||
template <class T, typename std::enable_if<std::is_convertible_v<T, Type>,
|
||||
bool>::type = true>
|
||||
Flatten(T&& _value) : value_(_value) {}
|
||||
|
||||
~Flatten() = default;
|
||||
|
||||
/// Returns the underlying object.
|
||||
inline const Type& get() const { return value_; }
|
||||
|
||||
/// Returns the underlying object.
|
||||
inline Type& operator()() { return value_; }
|
||||
|
||||
/// Returns the underlying object.
|
||||
inline const Type& operator()() const { return value_; }
|
||||
|
||||
/// Assigns the underlying object.
|
||||
inline void operator=(const Type& _value) { value_ = _value; }
|
||||
|
||||
/// Assigns the underlying object.
|
||||
inline void operator=(Type&& _value) {
|
||||
value_ = std::forward<Type>(_value);
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class T, typename std::enable_if<std::is_convertible_v<T, Type>,
|
||||
bool>::type = true>
|
||||
inline void operator=(const T& _value) {
|
||||
value_ = _value;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
inline void operator=(const Flatten<Type>& _f) { value_ = _f.get(); }
|
||||
|
||||
/// Assigns the underlying object.
|
||||
inline void operator=(Flatten<Type>&& _f) {
|
||||
value_ = std::forward<Type>(_f);
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class T>
|
||||
inline void operator=(const Flatten<T>& _f) {
|
||||
value_ = _f.get();
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class T>
|
||||
inline void operator=(Flatten<T>&& _f) {
|
||||
value_ = std::forward<T>(_f);
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
inline void set(const Type& _value) { value_ = _value; }
|
||||
|
||||
/// Assigns the underlying object.
|
||||
inline void set(Type&& _value) { value_ = std::forward<Type>(_value); }
|
||||
|
||||
/// The underlying value.
|
||||
Type value_;
|
||||
};
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,312 @@
|
||||
#ifndef RFL_LITERAL_HPP_
|
||||
#define RFL_LITERAL_HPP_
|
||||
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
|
||||
#include "rfl/Result.hpp"
|
||||
#include "rfl/internal/StringLiteral.hpp"
|
||||
#include "rfl/internal/VisitTree.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
template <internal::StringLiteral _field>
|
||||
struct LiteralHelper {
|
||||
constexpr static internal::StringLiteral field_ = _field;
|
||||
};
|
||||
|
||||
template <internal::StringLiteral... fields_>
|
||||
class Literal {
|
||||
using FieldsType = std::tuple<LiteralHelper<fields_>...>;
|
||||
|
||||
public:
|
||||
using ValueType =
|
||||
std::conditional_t<sizeof...(fields_) <=
|
||||
std::numeric_limits<std::uint8_t>::max(),
|
||||
std::uint8_t, std::uint16_t>;
|
||||
|
||||
/// The number of different fields or different options that the literal
|
||||
/// can assume.
|
||||
static constexpr ValueType num_fields_ = sizeof...(fields_);
|
||||
|
||||
/// Constructs a Literal from another literal.
|
||||
Literal(const Literal<fields_...>& _other) : value_(_other.value_) {}
|
||||
|
||||
/// Constructs a Literal from another literal.
|
||||
Literal(Literal<fields_...>&& _other) noexcept : value_(_other.value_) {}
|
||||
|
||||
/// A single-field literal is special because it
|
||||
/// can also have a default constructor.
|
||||
template <ValueType num_fields = num_fields_,
|
||||
typename = std::enable_if_t<num_fields == 1>>
|
||||
Literal() : value_(0) {}
|
||||
|
||||
~Literal() = default;
|
||||
|
||||
/// Determines whether the literal contains the string.
|
||||
inline static bool contains(const std::string& _str) {
|
||||
return has_value(_str);
|
||||
}
|
||||
|
||||
/// Determines whether the literal contains the string at compile time.
|
||||
template <internal::StringLiteral _name>
|
||||
inline static constexpr bool contains() {
|
||||
return find_value_of<_name>() != -1;
|
||||
}
|
||||
|
||||
/// Determines whether the literal contains any of the strings in the other
|
||||
/// literal at compile time.
|
||||
template <class OtherLiteralType, int _i = 0>
|
||||
inline static constexpr bool contains_any() {
|
||||
if constexpr (_i == num_fields_) {
|
||||
return false;
|
||||
} else {
|
||||
constexpr auto name = find_name_within_own_fields<_i>();
|
||||
return OtherLiteralType::template contains<name>() ||
|
||||
contains_any<OtherLiteralType, _i + 1>();
|
||||
}
|
||||
}
|
||||
|
||||
/// Determines whether the literal contains all of the strings in the other
|
||||
/// literal at compile time.
|
||||
template <class OtherLiteralType, int _i = 0, int _n_found = 0>
|
||||
inline static constexpr bool contains_all() {
|
||||
if constexpr (_i == num_fields_) {
|
||||
return _n_found == OtherLiteralType::num_fields_;
|
||||
} else {
|
||||
constexpr auto name = find_name_within_own_fields<_i>();
|
||||
if constexpr (OtherLiteralType::template contains<name>()) {
|
||||
return contains_all<OtherLiteralType, _i + 1, _n_found + 1>();
|
||||
} else {
|
||||
return contains_all<OtherLiteralType, _i + 1, _n_found>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Determines whether the literal has duplicate strings at compile time.
|
||||
/// These is useful for checking collections of strings in other contexts.
|
||||
inline static constexpr bool has_duplicates() {
|
||||
return has_duplicate_strings();
|
||||
}
|
||||
|
||||
/// Constructs a Literal from a string. Returns an error if the string
|
||||
/// cannot be found.
|
||||
static Result<Literal> from_string(const std::string& _str) {
|
||||
const auto to_literal = [](const auto& _v) {
|
||||
return Literal<fields_...>(_v);
|
||||
};
|
||||
return find_value(_str).transform(to_literal);
|
||||
};
|
||||
|
||||
/// Constructs a new Literal.
|
||||
template <internal::StringLiteral _name>
|
||||
static Literal<fields_...> make() {
|
||||
return Literal(Literal<fields_...>::template value_of<_name>());
|
||||
}
|
||||
|
||||
/// The name defined by the Literal.
|
||||
std::string name() const { return find_name(); }
|
||||
|
||||
/// Helper function to retrieve a name at compile time.
|
||||
template <int _value>
|
||||
inline constexpr static auto name_of() {
|
||||
constexpr auto name = find_name_within_own_fields<_value>();
|
||||
return Literal<name>();
|
||||
}
|
||||
|
||||
/// Assigns from another literal.
|
||||
Literal<fields_...> operator=(const Literal<fields_...>& _other) {
|
||||
value_ = _other.value_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns from another literal.
|
||||
Literal<fields_...> operator=(Literal<fields_...>&& _other) noexcept {
|
||||
value_ = _other.value_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the literal from a string
|
||||
Literal<fields_...> operator=(const std::string& _str) {
|
||||
value_ = find_value(_str);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Equality operator other Literals.
|
||||
inline bool operator==(const Literal<fields_...>& _other) const {
|
||||
return value() == _other.value();
|
||||
}
|
||||
|
||||
/// Equality operator other Literals with different fields.
|
||||
template <internal::StringLiteral... other_fields>
|
||||
inline bool operator==(const Literal<other_fields...>& _other) const {
|
||||
return name() == _other.name();
|
||||
}
|
||||
|
||||
/// Inequality operator for other Literals.
|
||||
template <internal::StringLiteral... other_fields>
|
||||
inline bool operator!=(const Literal<other_fields...>& _other) const {
|
||||
return !(*this == _other);
|
||||
}
|
||||
|
||||
/// Equality operator for strings.
|
||||
inline bool operator==(const std::string& _str) const {
|
||||
return name() == _str;
|
||||
}
|
||||
|
||||
/// Inequality operator for strings.
|
||||
inline bool operator!=(const std::string& _str) const {
|
||||
return name() != _str;
|
||||
}
|
||||
|
||||
/// Returns the value actually contained in the Literal.
|
||||
inline ValueType value() const { return value_; }
|
||||
|
||||
/// Returns the value of the string literal in the template.
|
||||
template <internal::StringLiteral _name>
|
||||
static constexpr ValueType value_of() {
|
||||
constexpr auto value = find_value_of<_name>();
|
||||
static_assert(value >= 0, "String not supported.");
|
||||
return value;
|
||||
}
|
||||
|
||||
private:
|
||||
/// Only make is allowed to use this constructor.
|
||||
Literal(const ValueType _value) : value_(_value) {}
|
||||
|
||||
/// Returns all of the allowed fields.
|
||||
template <int _i = 0>
|
||||
inline static std::string allowed_strings(const std::string& _values = "") {
|
||||
using FieldType = typename std::tuple_element<_i, FieldsType>::type;
|
||||
const auto head = "'" + FieldType::field_.str() + "'";
|
||||
const auto values = _values.size() == 0 ? head : _values + ", " + head;
|
||||
if constexpr (_i + 1 < num_fields_) {
|
||||
return allowed_strings<_i + 1>(values);
|
||||
} else {
|
||||
return values;
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the Literal contains duplicate strings.
|
||||
template <int _i = 1>
|
||||
inline constexpr static bool has_duplicate_strings() {
|
||||
if constexpr (_i >= num_fields_) {
|
||||
return false;
|
||||
} else {
|
||||
return is_duplicate<_i>() || has_duplicate_strings<_i + 1>();
|
||||
}
|
||||
}
|
||||
|
||||
template <int _i, int _j = _i - 1>
|
||||
inline constexpr static bool is_duplicate() {
|
||||
using FieldType1 = typename std::tuple_element<_i, FieldsType>::type;
|
||||
using FieldType2 = typename std::tuple_element<_j, FieldsType>::type;
|
||||
if constexpr (FieldType1::field_ == FieldType2::field_) {
|
||||
return true;
|
||||
} else if constexpr (_j > 0) {
|
||||
return is_duplicate<_i, _j - 1>();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Finds the correct index associated with
|
||||
/// the string at run time.
|
||||
template <int _i = 0>
|
||||
std::string find_name() const {
|
||||
if (_i == value_) {
|
||||
using FieldType = typename std::tuple_element<_i, FieldsType>::type;
|
||||
return FieldType::field_.str();
|
||||
}
|
||||
if constexpr (_i + 1 == num_fields_) {
|
||||
return "";
|
||||
} else {
|
||||
return find_name<_i + 1>();
|
||||
}
|
||||
}
|
||||
|
||||
/// Finds the correct index associated with
|
||||
/// the string at compile time within the Literal's own fields.
|
||||
template <int _i>
|
||||
inline constexpr static auto find_name_within_own_fields() {
|
||||
using FieldType = typename std::tuple_element<_i, FieldsType>::type;
|
||||
return FieldType::field_;
|
||||
}
|
||||
|
||||
/// Finds the correct value associated with
|
||||
/// the string at run time.
|
||||
template <int _i = 0>
|
||||
inline static Result<int> find_value(const std::string& _str) {
|
||||
using FieldType = typename std::tuple_element<_i, FieldsType>::type;
|
||||
if (FieldType::field_.str() == _str) {
|
||||
return _i;
|
||||
}
|
||||
if constexpr (_i + 1 == num_fields_) {
|
||||
return Error("Literal does not support string '" + _str +
|
||||
"'. The following strings are supported: " +
|
||||
allowed_strings() + ".");
|
||||
} else {
|
||||
return find_value<_i + 1>(_str);
|
||||
}
|
||||
}
|
||||
|
||||
/// Finds the value of a string literal at compile time.
|
||||
template <internal::StringLiteral _name, int _i = 0>
|
||||
static constexpr int find_value_of() {
|
||||
using FieldType = typename std::tuple_element<_i, FieldsType>::type;
|
||||
if constexpr (FieldType::field_ == _name) {
|
||||
return _i;
|
||||
} else if constexpr (_i + 1 < num_fields_) {
|
||||
return find_value_of<_name, _i + 1>();
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the literal contains this string.
|
||||
template <int _i = 0>
|
||||
inline static bool has_value(const std::string& _str) {
|
||||
using FieldType = typename std::tuple_element<_i, FieldsType>::type;
|
||||
if (FieldType::field_.str() == _str) {
|
||||
return true;
|
||||
}
|
||||
if constexpr (_i + 1 == num_fields_) {
|
||||
return false;
|
||||
} else {
|
||||
return has_value<_i + 1>(_str);
|
||||
}
|
||||
}
|
||||
|
||||
static_assert(sizeof...(fields_) > 0,
|
||||
"There must be at least one field in a Literal.");
|
||||
|
||||
static_assert(sizeof...(fields_) <= std::numeric_limits<ValueType>::max(),
|
||||
"Too many fields.");
|
||||
|
||||
static_assert(!has_duplicates(),
|
||||
"Duplicate strings are not allowed in a Literal.");
|
||||
|
||||
private:
|
||||
/// The underlying value.
|
||||
ValueType value_;
|
||||
};
|
||||
|
||||
/// Helper function to retrieve a name at compile time.
|
||||
template <class LiteralType, int _value>
|
||||
inline constexpr auto name_of() {
|
||||
return LiteralType::template name_of<_value>();
|
||||
}
|
||||
|
||||
/// Helper function to retrieve a value at compile time.
|
||||
template <class LiteralType, internal::StringLiteral _name>
|
||||
inline constexpr auto value_of() {
|
||||
return LiteralType::template value_of<_name>();
|
||||
}
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif // RFL_LITERAL_HPP_
|
||||
@@ -0,0 +1,441 @@
|
||||
#ifndef RFL_NAMEDTUPLE_HPP_
|
||||
#define RFL_NAMEDTUPLE_HPP_
|
||||
|
||||
#include <algorithm>
|
||||
#include <string_view>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include "rfl/Field.hpp"
|
||||
#include "rfl/get.hpp"
|
||||
#include "rfl/internal/StringLiteral.hpp"
|
||||
#include "rfl/internal/find_index.hpp"
|
||||
#include "rfl/internal/no_duplicate_field_names.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// A named tuple behaves like std::tuple,
|
||||
/// but the fields have explicit names, which
|
||||
/// allows for reflection.
|
||||
template <class... FieldTypes>
|
||||
class NamedTuple {
|
||||
public:
|
||||
using Fields = std::tuple<std::decay_t<FieldTypes>...>;
|
||||
|
||||
public:
|
||||
/// Construct from the fields.
|
||||
NamedTuple(FieldTypes... _fields)
|
||||
: values_(std::forward_as_tuple(_fields.value_...)) {
|
||||
static_assert(no_duplicate_field_names(),
|
||||
"Duplicate field names are not allowed");
|
||||
}
|
||||
|
||||
/// Construct from a tuple containing fields.
|
||||
NamedTuple(const std::tuple<FieldTypes...>& _tup)
|
||||
: NamedTuple(std::make_from_tuple<NamedTuple<FieldTypes...>>(_tup)) {
|
||||
static_assert(no_duplicate_field_names(),
|
||||
"Duplicate field names are not allowed");
|
||||
}
|
||||
|
||||
/// Copy constructor.
|
||||
NamedTuple(const NamedTuple<FieldTypes...>& _other)
|
||||
: values_(_other.values_) {
|
||||
// Duplicate field checks not required - has already been checked.
|
||||
}
|
||||
|
||||
/// Move constructor.
|
||||
NamedTuple(NamedTuple<FieldTypes...>&& _other)
|
||||
: values_(std::move(_other.values_)) {
|
||||
// Duplicate field checks not required - has already been checked.
|
||||
}
|
||||
|
||||
/// Copy constructor.
|
||||
template <class... OtherFieldTypes>
|
||||
NamedTuple(const NamedTuple<OtherFieldTypes...>& _other)
|
||||
: NamedTuple(retrieve_fields(_other.fields())) {
|
||||
static_assert(no_duplicate_field_names(),
|
||||
"Duplicate field names are not allowed");
|
||||
}
|
||||
|
||||
/// Move constructor.
|
||||
template <class... OtherFieldTypes>
|
||||
NamedTuple(NamedTuple<OtherFieldTypes...>&& _other)
|
||||
: NamedTuple(retrieve_fields(_other.fields())) {
|
||||
static_assert(no_duplicate_field_names(),
|
||||
"Duplicate field names are not allowed");
|
||||
}
|
||||
|
||||
~NamedTuple() = default;
|
||||
|
||||
/// Returns a new named tuple with additional fields.
|
||||
template <class Head, class... Tail>
|
||||
auto add(Head&& _head, Tail&&... _tail) const {
|
||||
if constexpr (sizeof...(Tail) > 0) {
|
||||
return NamedTuple<FieldTypes..., std::decay_t<Head>>(
|
||||
make_fields<1, Head>(std::forward<Head>(_head)))
|
||||
.add(std::forward<Tail>(_tail)...);
|
||||
} else {
|
||||
return NamedTuple<FieldTypes..., std::decay_t<Head>>(
|
||||
make_fields<1, Head>(std::forward<Head>(_head)));
|
||||
}
|
||||
}
|
||||
|
||||
/// Template specialization for std::tuple, so we can pass fields from other
|
||||
/// named tuples.
|
||||
template <class... TupContent, class... Tail>
|
||||
auto add(std::tuple<TupContent...>&& _tuple, Tail&&... _tail) const {
|
||||
if constexpr (sizeof...(Tail) > 0) {
|
||||
return add_tuple(std::forward<std::tuple<TupContent...>>(_tuple),
|
||||
std::make_index_sequence<sizeof...(TupContent)>{})
|
||||
.replace(_tail...);
|
||||
} else {
|
||||
return add_tuple(std::forward<std::tuple<TupContent...>>(_tuple),
|
||||
std::make_index_sequence<sizeof...(TupContent)>{});
|
||||
}
|
||||
}
|
||||
|
||||
/// Template specialization for std::tuple, so we can pass fields from other
|
||||
/// named tuples.
|
||||
template <class... TupContent, class... Tail>
|
||||
auto add(const std::tuple<TupContent...>& _tuple, Tail&&... _tail) const {
|
||||
if constexpr (sizeof...(Tail) > 0) {
|
||||
return add_tuple(std::forward<std::tuple<TupContent...>>(_tuple),
|
||||
std::make_index_sequence<sizeof...(TupContent)>{})
|
||||
.replace(_tail...);
|
||||
} else {
|
||||
return add_tuple(std::forward<std::tuple<TupContent...>>(_tuple),
|
||||
std::make_index_sequence<sizeof...(TupContent)>{});
|
||||
}
|
||||
}
|
||||
|
||||
/// Template specialization for NamedTuple, so we can pass fields from other
|
||||
/// named tuples.
|
||||
template <class... TupContent, class... Tail>
|
||||
auto add(NamedTuple<TupContent...>&& _named_tuple, Tail&&... _tail) const {
|
||||
return add(_named_tuple.fields(), std::forward<Tail>(_tail)...);
|
||||
}
|
||||
|
||||
/// Template specialization for NamedTuple, so we can pass fields from other
|
||||
/// named tuples.
|
||||
template <class... TupContent, class... Tail>
|
||||
auto add(const NamedTuple<TupContent...>& _named_tuple,
|
||||
Tail&&... _tail) const {
|
||||
return add(_named_tuple.fields(), std::forward<Tail>(_tail)...);
|
||||
}
|
||||
|
||||
/// Returns a tuple containing the fields.
|
||||
Fields fields() const { return make_fields(); }
|
||||
|
||||
/// Gets a field by index.
|
||||
template <int _index>
|
||||
inline auto& get() {
|
||||
return rfl::get<_index>(*this);
|
||||
}
|
||||
|
||||
/// Gets a field by name.
|
||||
template <internal::StringLiteral _field_name>
|
||||
inline auto& get() {
|
||||
return rfl::get<_field_name>(*this);
|
||||
}
|
||||
|
||||
/// Gets a field by the field type.
|
||||
template <class Field>
|
||||
inline auto& get() {
|
||||
return rfl::get<Field>(*this);
|
||||
}
|
||||
|
||||
/// Gets a field by index.
|
||||
template <int _index>
|
||||
inline const auto& get() const {
|
||||
return rfl::get<_index>(*this);
|
||||
}
|
||||
|
||||
/// Gets a field by name.
|
||||
template <internal::StringLiteral _field_name>
|
||||
inline const auto& get() const {
|
||||
return rfl::get<_field_name>(*this);
|
||||
}
|
||||
|
||||
/// Gets a field by the field type.
|
||||
template <class Field>
|
||||
inline const auto& get() const {
|
||||
return rfl::get<Field>(*this);
|
||||
}
|
||||
|
||||
/// Returns the results wrapped in a field.
|
||||
template <internal::StringLiteral _field_name>
|
||||
inline auto get_field() const {
|
||||
return rfl::make_field<_field_name>(rfl::get<_field_name>(*this));
|
||||
}
|
||||
|
||||
/// Copy assignment operator.
|
||||
NamedTuple<FieldTypes...>& operator=(
|
||||
const NamedTuple<FieldTypes...>& _other) {
|
||||
values_ = _other.values_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Move assignment operator.
|
||||
NamedTuple<FieldTypes...>& operator=(
|
||||
NamedTuple<FieldTypes...>&& _other) noexcept {
|
||||
if (this == &_other) {
|
||||
return *this;
|
||||
}
|
||||
values_ = std::move(_other.values_);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Replaces one or several fields, returning a new version
|
||||
/// with the non-replaced fields left unchanged.
|
||||
template <class RField, class... OtherRFields>
|
||||
NamedTuple<FieldTypes...> replace(RField&& _field,
|
||||
OtherRFields&&... _other_fields) const {
|
||||
constexpr auto num_other_fields = sizeof...(OtherRFields);
|
||||
if constexpr (num_other_fields == 0) {
|
||||
return replace_value<RField>(_field.value_);
|
||||
} else {
|
||||
return replace_value<RField>(_field.value_)
|
||||
.replace(std::forward<OtherRFields>(_other_fields)...);
|
||||
}
|
||||
}
|
||||
|
||||
/// Template specialization for std::tuple, so we can pass fields from other
|
||||
/// named tuples.
|
||||
template <class... TupContent, class... Tail>
|
||||
auto replace(std::tuple<TupContent...>&& _tuple, Tail&&... _tail) const {
|
||||
if constexpr (sizeof...(Tail) > 0) {
|
||||
return replace_tuple(
|
||||
std::forward<std::tuple<TupContent...>>(_tuple),
|
||||
std::make_index_sequence<sizeof...(TupContent)>{})
|
||||
.replace(std::forward<Tail>(_tail)...);
|
||||
} else {
|
||||
return replace_tuple(
|
||||
std::forward<std::tuple<TupContent...>>(_tuple),
|
||||
std::make_index_sequence<sizeof...(TupContent)>{});
|
||||
}
|
||||
}
|
||||
|
||||
/// Template specialization for std::tuple, so we can pass fields from other
|
||||
/// named tuples.
|
||||
template <class... TupContent, class... Tail>
|
||||
auto replace(const std::tuple<TupContent...>& _tuple,
|
||||
Tail&&... _tail) const {
|
||||
if constexpr (sizeof...(Tail) > 0) {
|
||||
return replace_tuple(
|
||||
std::forward<std::tuple<TupContent...>>(_tuple),
|
||||
std::make_index_sequence<sizeof...(TupContent)>{})
|
||||
.replace(std::forward<Tail>(_tail)...);
|
||||
} else {
|
||||
return replace_tuple(
|
||||
std::forward<std::tuple<TupContent...>>(_tuple),
|
||||
std::make_index_sequence<sizeof...(TupContent)>{});
|
||||
}
|
||||
}
|
||||
|
||||
/// Template specialization for NamedTuple, so we can pass fields from other
|
||||
/// named tuples.
|
||||
template <class... TupContent, class... Tail>
|
||||
auto replace(NamedTuple<TupContent...>&& _named_tuple,
|
||||
Tail&&... _tail) const {
|
||||
return replace(_named_tuple.fields(), std::forward<Tail>(_tail)...);
|
||||
}
|
||||
|
||||
/// Template specialization for NamedTuple, so we can pass fields from other
|
||||
/// named tuples.
|
||||
template <class... TupContent, class... Tail>
|
||||
auto replace(const NamedTuple<TupContent...>& _named_tuple,
|
||||
Tail&&... _tail) const {
|
||||
return replace(_named_tuple.fields(), std::forward<Tail>(_tail)...);
|
||||
}
|
||||
|
||||
/// Returns the underlying std::tuple.
|
||||
auto& values() { return values_; }
|
||||
|
||||
/// Returns the underlying std::tuple.
|
||||
const auto& values() const { return values_; }
|
||||
|
||||
private:
|
||||
/// Adds the elements of a tuple to a newly created named tuple,
|
||||
/// and other elements to a newly created named tuple.
|
||||
template <class Tuple, size_t... Is>
|
||||
constexpr auto add_tuple(Tuple&& _tuple,
|
||||
const std::index_sequence<Is...>) const {
|
||||
return add(std::get<Is>(_tuple)...);
|
||||
}
|
||||
|
||||
/// Generates the fields.
|
||||
template <int num_additional_fields = 0, class... Args>
|
||||
auto make_fields(Args&&... _args) const {
|
||||
constexpr auto size = sizeof...(Args) - num_additional_fields;
|
||||
constexpr auto num_fields = std::tuple_size<Fields>{};
|
||||
constexpr auto i = num_fields - size - 1;
|
||||
|
||||
constexpr bool retrieved_all_fields = size == num_fields;
|
||||
|
||||
if constexpr (retrieved_all_fields) {
|
||||
return std::make_tuple(std::forward<Args>(_args)...);
|
||||
} else {
|
||||
// When we add additional fields, it is more intuitive to add
|
||||
// them to the end, that is why we do it like this.
|
||||
using FieldType = typename std::tuple_element<i, Fields>::type;
|
||||
return make_fields<num_additional_fields>(
|
||||
FieldType(std::get<i>(values_)), std::forward<Args>(_args)...);
|
||||
}
|
||||
}
|
||||
|
||||
/// Generates a new named tuple with one value replaced with a new value.
|
||||
template <int _index, class T, class... Args>
|
||||
auto make_replaced(T&& _val, Args&&... _args) const {
|
||||
constexpr auto size = sizeof...(Args);
|
||||
|
||||
constexpr bool retrieved_all_fields = size == std::tuple_size<Fields>{};
|
||||
|
||||
if constexpr (retrieved_all_fields) {
|
||||
return NamedTuple<FieldTypes...>(std::forward<Args>(_args)...);
|
||||
} else {
|
||||
using FieldType = typename std::tuple_element<size, Fields>::type;
|
||||
|
||||
if constexpr (size == _index) {
|
||||
return make_replaced<_index, T>(std::forward<T>(_val),
|
||||
std::forward<Args>(_args)...,
|
||||
FieldType(_val));
|
||||
} else {
|
||||
return make_replaced<_index, T>(
|
||||
std::forward<T>(_val), std::forward<Args>(_args)...,
|
||||
FieldType(std::get<size>(values_)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// We cannot allow duplicate field names.
|
||||
constexpr static bool no_duplicate_field_names() {
|
||||
return internal::no_duplicate_field_names<Fields>();
|
||||
}
|
||||
|
||||
/// Replaced the field signified by the field type.
|
||||
template <class Field, class T>
|
||||
NamedTuple<FieldTypes...> replace_value(T&& _val) const {
|
||||
using FieldType = std::decay_t<Field>;
|
||||
constexpr auto index = internal::find_index<FieldType::name_, Fields>();
|
||||
static_assert(
|
||||
std::is_same<typename std::tuple_element<index, Fields>::type::Type,
|
||||
typename FieldType::Type>(),
|
||||
"If two fields have the same name, "
|
||||
"their type must be the same as "
|
||||
"well.");
|
||||
return make_replaced<index, T>(_val);
|
||||
}
|
||||
|
||||
/// Adds the elements of a tuple to a newly created named tuple,
|
||||
/// and other elements to a newly created named tuple.
|
||||
template <class Tuple, size_t... Is>
|
||||
constexpr auto replace_tuple(Tuple&& _tuple,
|
||||
const std::index_sequence<Is...>) const {
|
||||
return replace(std::get<Is>(_tuple)...);
|
||||
}
|
||||
|
||||
/// Retrieves the fields from another tuple.
|
||||
template <class... OtherFieldTypes, class... Args>
|
||||
constexpr static Fields retrieve_fields(
|
||||
std::tuple<OtherFieldTypes...>&& _other_fields, Args&&... _args) {
|
||||
constexpr auto size = sizeof...(Args);
|
||||
|
||||
constexpr bool retrieved_all_fields = size == std::tuple_size<Fields>{};
|
||||
|
||||
if constexpr (retrieved_all_fields) {
|
||||
return std::make_tuple(std::forward<Args>(_args)...);
|
||||
} else {
|
||||
constexpr auto field_name =
|
||||
std::tuple_element<size, Fields>::type::name_;
|
||||
|
||||
constexpr auto index =
|
||||
internal::find_index<field_name,
|
||||
std::tuple<OtherFieldTypes...>>();
|
||||
|
||||
using FieldType = typename std::tuple_element<size, Fields>::type;
|
||||
|
||||
return retrieve_fields(
|
||||
std::forward<std::tuple<OtherFieldTypes...>>(_other_fields),
|
||||
std::forward<Args>(_args)...,
|
||||
FieldType(std::get<index>(_other_fields).value_));
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
/// The values actually contained in the named tuple.
|
||||
/// As you can see, a NamedTuple is just a normal tuple under-the-hood,
|
||||
/// everything else is resolved at compile time. It should have no
|
||||
/// runtime overhead over a normal std::tuple.
|
||||
std::tuple<typename std::decay<FieldTypes>::type::Type...> values_;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
template <class... FieldTypes>
|
||||
inline bool operator==(const rfl::NamedTuple<FieldTypes...>& _nt1,
|
||||
const rfl::NamedTuple<FieldTypes...>& _nt2) {
|
||||
return _nt1.values() == _nt2.values();
|
||||
}
|
||||
|
||||
template <class... FieldTypes>
|
||||
inline bool operator!=(const rfl::NamedTuple<FieldTypes...>& _nt1,
|
||||
const rfl::NamedTuple<FieldTypes...>& _nt2) {
|
||||
return _nt1.values() != _nt2.values();
|
||||
}
|
||||
|
||||
template <internal::StringLiteral _name1, class Type1,
|
||||
internal::StringLiteral _name2, class Type2>
|
||||
inline auto operator*(const rfl::Field<_name1, Type1>& _f1,
|
||||
const rfl::Field<_name2, Type2>& _f2) {
|
||||
return NamedTuple(_f1, _f2);
|
||||
}
|
||||
|
||||
template <internal::StringLiteral _name, class Type, class... FieldTypes>
|
||||
inline auto operator*(const NamedTuple<FieldTypes...>& _tup,
|
||||
const rfl::Field<_name, Type>& _f) {
|
||||
return _tup.add(_f);
|
||||
}
|
||||
|
||||
template <internal::StringLiteral _name, class Type, class... FieldTypes>
|
||||
inline auto operator*(const rfl::Field<_name, Type>& _f,
|
||||
const NamedTuple<FieldTypes...>& _tup) {
|
||||
return NamedTuple(_f).add(_tup);
|
||||
}
|
||||
|
||||
template <class... FieldTypes1, class... FieldTypes2>
|
||||
inline auto operator*(const NamedTuple<FieldTypes1...>& _tup1,
|
||||
const NamedTuple<FieldTypes2...>& _tup2) {
|
||||
return _tup1.add(_tup2);
|
||||
}
|
||||
|
||||
template <internal::StringLiteral _name1, class Type1,
|
||||
internal::StringLiteral _name2, class Type2>
|
||||
inline auto operator*(rfl::Field<_name1, Type1>&& _f1,
|
||||
rfl::Field<_name2, Type2>&& _f2) {
|
||||
return NamedTuple(std::forward<Field<_name1, Type1>>(_f1),
|
||||
std::forward<Field<_name2, Type2>>(_f2));
|
||||
}
|
||||
|
||||
template <internal::StringLiteral _name, class Type, class... FieldTypes>
|
||||
inline auto operator*(NamedTuple<FieldTypes...>&& _tup,
|
||||
rfl::Field<_name, Type>&& _f) {
|
||||
return _tup.add(std::forward<Field<_name, Type>>(_f));
|
||||
}
|
||||
|
||||
template <internal::StringLiteral _name, class Type, class... FieldTypes>
|
||||
inline auto operator*(rfl::Field<_name, Type>&& _f,
|
||||
NamedTuple<FieldTypes...>&& _tup) {
|
||||
return NamedTuple(std::forward<Field<_name, Type>>(_f))
|
||||
.add(std::forward<NamedTuple<FieldTypes...>>(_tup));
|
||||
}
|
||||
|
||||
template <class... FieldTypes1, class... FieldTypes2>
|
||||
inline auto operator*(NamedTuple<FieldTypes1...>&& _tup1,
|
||||
NamedTuple<FieldTypes2...>&& _tup2) {
|
||||
return _tup1.add(std::forward<NamedTuple<FieldTypes2...>>(_tup2));
|
||||
}
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif // RFL_NAMEDTUPLE_HPP_
|
||||
@@ -0,0 +1,71 @@
|
||||
#ifndef RFL_REF_HPP_
|
||||
#define RFL_REF_HPP_
|
||||
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// The Ref class behaves very similarly to the shared_ptr, but unlike the
|
||||
/// shared_ptr, it is 100% guaranteed to be filled at all times.
|
||||
template <class T>
|
||||
class Ref {
|
||||
public:
|
||||
/// The default way of creating new references is
|
||||
/// Ref<T>::make(...).
|
||||
template <class... Args>
|
||||
static Ref<T> make(Args... _args) {
|
||||
return Ref<T>(std::make_shared<T>(_args...));
|
||||
}
|
||||
|
||||
/// Wrapper around a shared_ptr, leads to a runtime error,
|
||||
/// if the shared_ptr is not allocated.
|
||||
explicit Ref(const std::shared_ptr<T>& _ptr) {
|
||||
assert_true(_ptr);
|
||||
if (!_ptr) {
|
||||
throw std::runtime_error(
|
||||
"Could not create Ref from shared_ptr, because shared_ptr was "
|
||||
"not "
|
||||
"set.");
|
||||
}
|
||||
ptr_ = _ptr;
|
||||
}
|
||||
|
||||
template <class Y>
|
||||
Ref(const Ref<Y>& _other) : ptr_(_other.ptr()) {}
|
||||
|
||||
~Ref() = default;
|
||||
|
||||
/// Returns a pointer to the underlying object
|
||||
inline T* get() const { return ptr_.get(); }
|
||||
|
||||
/// Returns the underlying object.
|
||||
inline T& operator*() const { return *ptr_; }
|
||||
|
||||
/// Returns the underlying object.
|
||||
inline T* operator->() const { return ptr_.get(); }
|
||||
|
||||
/// Returns the underlying shared_ptr
|
||||
inline const std::shared_ptr<T>& ptr() const { return ptr_; }
|
||||
|
||||
/// Copy assignment operator.
|
||||
template <class Y>
|
||||
Ref<T>& operator=(const Ref<Y>& _other) {
|
||||
ptr_ = _other.ptr();
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
/// Only make is allowed to use this constructor.
|
||||
explicit Ref(std::shared_ptr<T>&& _ptr)
|
||||
: ptr_(std::forward<std::shared_ptr<T>>(_ptr)) {}
|
||||
|
||||
private:
|
||||
/// The underlying shared_ptr_
|
||||
std::shared_ptr<T> ptr_;
|
||||
};
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif // RFL_REF_HPP_
|
||||
|
||||
@@ -0,0 +1,489 @@
|
||||
#ifndef RFL_RESULT_HPP_
|
||||
#define RFL_RESULT_HPP_
|
||||
|
||||
#include <optional>
|
||||
#include <ranges>
|
||||
#include <stdexcept>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// To be returned
|
||||
class Error {
|
||||
public:
|
||||
Error(const std::string& _what) : what_(_what) {}
|
||||
|
||||
~Error() = default;
|
||||
|
||||
/// Returns the error message, equivalent to .what() in std::exception.
|
||||
const std::string& what() const { return what_; }
|
||||
|
||||
private:
|
||||
/// Documents what went wrong
|
||||
std::string what_;
|
||||
};
|
||||
|
||||
/// Can be used when we are simply interested in whether an operation was
|
||||
/// successful.
|
||||
struct Nothing {};
|
||||
|
||||
/// Helper class to be used to within the Result class to support the operator*.
|
||||
/// This is necessary, because we only want this class to be unwrapped, not
|
||||
/// normal std::tuple.
|
||||
template <class... Args>
|
||||
struct HelperTuple {
|
||||
using TupleType = std::tuple<Args...>;
|
||||
TupleType tuple_;
|
||||
};
|
||||
|
||||
template <class>
|
||||
struct is_helper_tuple : std::false_type {};
|
||||
|
||||
template <class... Args>
|
||||
struct is_helper_tuple<HelperTuple<Args...>> : std::true_type {};
|
||||
|
||||
template <class T>
|
||||
struct value_type {
|
||||
using Type = T;
|
||||
};
|
||||
|
||||
template <class... Args>
|
||||
struct value_type<HelperTuple<Args...>> {
|
||||
using Type = HelperTuple<Args...>::TupleType;
|
||||
};
|
||||
|
||||
/// The Result class is used for monadic error handling.
|
||||
template <class T>
|
||||
class Result {
|
||||
static_assert(!std::is_same<T, Error>(),
|
||||
"The result type cannot be Error.");
|
||||
|
||||
public:
|
||||
using ValueType = typename value_type<T>::Type;
|
||||
|
||||
Result(const T& _val) : t_or_err_(_val) {}
|
||||
|
||||
Result(T&& _val) : t_or_err_(_val) {}
|
||||
|
||||
Result(const Error& _err) : t_or_err_(_err) {}
|
||||
|
||||
Result(Error&& _err) : t_or_err_(_err) {}
|
||||
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
Result(const Result<U>& _other)
|
||||
: t_or_err_(
|
||||
_other.transform([](const U& _u) { return T(_u); }).t_or_err_) {}
|
||||
|
||||
~Result() = default;
|
||||
|
||||
/// Returns Result<U>, if successful and error otherwise.
|
||||
/// Inspired by .and(...) in the Rust std::result type.
|
||||
template <class U>
|
||||
Result<U> and_other(const Result<U>& _r) const noexcept {
|
||||
const auto f = [&](const auto& _) { return _r; };
|
||||
return and_then(f);
|
||||
}
|
||||
|
||||
/// Monadic operation - F must be a function of type T -> Result<U>.
|
||||
template <class F>
|
||||
auto and_then(const F& _f) {
|
||||
const auto apply = [&_f](auto&& _t) {
|
||||
if constexpr (is_helper_tuple<T>()) {
|
||||
using TupleType = std::decay_t<decltype(_t.tuple)>;
|
||||
return std::apply(_f, std::forward<TupleType>(_t.tuple_));
|
||||
} else {
|
||||
return _f(_t);
|
||||
}
|
||||
};
|
||||
|
||||
/// Result_U is expected to be of type Result<U>.
|
||||
using Result_U = typename std::invoke_result<decltype(apply), T>::type;
|
||||
|
||||
const auto handle_variant =
|
||||
[apply]<class TOrError>(TOrError&& _t_or_err) -> Result_U {
|
||||
if constexpr (!std::is_same<std::decay_t<TOrError>, Error>()) {
|
||||
return apply(std::forward<TOrError>(_t_or_err));
|
||||
} else {
|
||||
return _t_or_err;
|
||||
}
|
||||
};
|
||||
|
||||
return std::visit(handle_variant, t_or_err_);
|
||||
}
|
||||
|
||||
/// Monadic operation - F must be a function of type T -> Result<U>.
|
||||
template <class F>
|
||||
auto and_then(const F& _f) const {
|
||||
const auto apply = [&_f](const auto& _t) {
|
||||
if constexpr (is_helper_tuple<T>()) {
|
||||
return std::apply(_f, _t.tuple_);
|
||||
} else {
|
||||
return _f(_t);
|
||||
}
|
||||
};
|
||||
|
||||
/// Result_U is expected to be of type Result<U>.
|
||||
using Result_U = typename std::invoke_result<decltype(apply), T>::type;
|
||||
|
||||
const auto handle_variant =
|
||||
[apply]<class TOrError>(const TOrError& _t_or_err) -> Result_U {
|
||||
if constexpr (!std::is_same<TOrError, Error>()) {
|
||||
return apply(_t_or_err);
|
||||
} else {
|
||||
return _t_or_err;
|
||||
}
|
||||
};
|
||||
|
||||
return std::visit(handle_variant, t_or_err_);
|
||||
}
|
||||
|
||||
/// Results types can be iterated over, which even make it possible to use
|
||||
/// them within a std::range.
|
||||
ValueType* begin() noexcept {
|
||||
const auto get_ptr =
|
||||
[this]<class TOrError>(const TOrError& _t_or_err) -> ValueType* {
|
||||
if constexpr (!std::is_same<TOrError, Error>()) {
|
||||
if constexpr (is_helper_tuple<T>()) {
|
||||
return &_t_or_err.tuple_;
|
||||
} else {
|
||||
return &_t_or_err;
|
||||
}
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
};
|
||||
return std::visit(get_ptr, t_or_err_);
|
||||
}
|
||||
|
||||
/// Results types can be iterated over, which even make it possible to use
|
||||
/// them within a std::range.
|
||||
const ValueType* begin() const noexcept {
|
||||
const auto get_ptr =
|
||||
[this]<class TOrError>(
|
||||
const TOrError& _t_or_err) -> const ValueType* {
|
||||
if constexpr (!std::is_same<TOrError, Error>()) {
|
||||
if constexpr (is_helper_tuple<T>()) {
|
||||
return &_t_or_err.tuple_;
|
||||
} else {
|
||||
return &_t_or_err;
|
||||
}
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
};
|
||||
return std::visit(get_ptr, t_or_err_);
|
||||
}
|
||||
|
||||
/// Results types can be iterated over, which even make it possible to use
|
||||
/// them within a std::range.
|
||||
ValueType* end() noexcept {
|
||||
const auto get_ptr =
|
||||
[this]<class TOrError>(const TOrError& _t_or_err) -> ValueType* {
|
||||
if constexpr (!std::is_same<TOrError, Error>()) {
|
||||
if constexpr (is_helper_tuple<T>()) {
|
||||
return &_t_or_err.tuple_ + 1;
|
||||
} else {
|
||||
return &_t_or_err + 1;
|
||||
}
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
};
|
||||
return std::visit(get_ptr, t_or_err_);
|
||||
}
|
||||
|
||||
/// Results types can be iterated over, which even make it possible to use
|
||||
/// them within a std::range.
|
||||
const ValueType* end() const noexcept {
|
||||
const auto get_ptr =
|
||||
[this]<class TOrError>(
|
||||
const TOrError& _t_or_err) -> const ValueType* {
|
||||
if constexpr (!std::is_same<TOrError, Error>()) {
|
||||
if constexpr (is_helper_tuple<T>()) {
|
||||
return &_t_or_err.tuple_ + 1;
|
||||
} else {
|
||||
return &_t_or_err + 1;
|
||||
}
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
};
|
||||
return std::visit(get_ptr, t_or_err_);
|
||||
}
|
||||
|
||||
/// Returns an std::optional<error> if this does in fact contain an error
|
||||
/// or std::nullopt otherwise.
|
||||
std::optional<Error> error() const noexcept {
|
||||
const auto get_err =
|
||||
[this]<class TOrError>(
|
||||
const TOrError& _t_or_err) -> std::optional<Error> {
|
||||
if constexpr (!std::is_same<TOrError, Error>()) {
|
||||
return std::nullopt;
|
||||
} else {
|
||||
return _t_or_err;
|
||||
}
|
||||
};
|
||||
return std::visit(get_err, t_or_err_);
|
||||
}
|
||||
|
||||
/// Returns true if the result contains a value, false otherwise.
|
||||
operator bool() const noexcept {
|
||||
return !std::holds_alternative<Error>(t_or_err_);
|
||||
}
|
||||
|
||||
/// Allows access to the underlying value. Careful: Will result in undefined
|
||||
/// behavior, if the result contains an error.
|
||||
T& operator*() { return *std::get_if<T>(&t_or_err_); }
|
||||
|
||||
/// Allows read access to the underlying value. Careful: Will result in
|
||||
/// undefined behavior, if the result contains an error.
|
||||
const T& operator*() const { return *std::get_if<T>(&t_or_err_); }
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
inline void operator=(const Result<U>& _other) {
|
||||
const auto to_t = [](const U& _u) -> T { return _u; };
|
||||
t_or_err_ = _other.transform(to_t).t_or_err_;
|
||||
}
|
||||
|
||||
/// Expects a function that takes of type Error -> Result<T> and returns
|
||||
/// Result<T>.
|
||||
template <class F>
|
||||
Result<T> or_else(const F& _f) const {
|
||||
const auto handle_variant =
|
||||
[&_f]<class TOrError>(const TOrError& _t_or_err) -> Result<T> {
|
||||
if constexpr (std::is_same<TOrError, Error>()) {
|
||||
return _f(_t_or_err);
|
||||
} else {
|
||||
return _t_or_err;
|
||||
}
|
||||
};
|
||||
return std::visit(handle_variant, t_or_err_);
|
||||
}
|
||||
|
||||
/// Returns the value contained if successful or the provided result r if
|
||||
/// not.
|
||||
Result<T> or_other(const Result<T>& _r) const noexcept {
|
||||
const auto f = [&](const auto& _) { return _r; };
|
||||
return or_else(f);
|
||||
}
|
||||
|
||||
/// Functor operation - F must be a function of type T -> U.
|
||||
template <class F>
|
||||
auto transform(const F& _f) {
|
||||
const auto apply = [&_f](auto&& _t) {
|
||||
if constexpr (is_helper_tuple<T>()) {
|
||||
using TupleType = std::decay_t<decltype(_t.tuple)>;
|
||||
return std::apply(_f, std::forward<TupleType>(_t.tuple_));
|
||||
} else {
|
||||
return _f(std::forward<T>(_t));
|
||||
}
|
||||
};
|
||||
|
||||
/// Result_U is expected to be of type Result<U>.
|
||||
using U = typename std::invoke_result<decltype(apply), T>::type;
|
||||
|
||||
const auto handle_variant =
|
||||
[apply]<class TOrError>(TOrError&& _t_or_err) -> rfl::Result<U> {
|
||||
if constexpr (!std::is_same<std::decay_t<TOrError>, Error>()) {
|
||||
return apply(std::forward<TOrError>(_t_or_err));
|
||||
} else {
|
||||
return _t_or_err;
|
||||
}
|
||||
};
|
||||
|
||||
return std::visit(handle_variant, t_or_err_);
|
||||
}
|
||||
|
||||
/// Functor operation - F must be a function of type T -> U.
|
||||
template <class F>
|
||||
auto transform(const F& _f) const {
|
||||
const auto apply = [&_f](const auto& _t) {
|
||||
if constexpr (is_helper_tuple<T>()) {
|
||||
return std::apply(_f, _t.tuple_);
|
||||
} else {
|
||||
return _f(_t);
|
||||
}
|
||||
};
|
||||
|
||||
/// Result_U is expected to be of type Result<U>.
|
||||
using U = typename std::invoke_result<decltype(apply), T>::type;
|
||||
|
||||
const auto handle_variant =
|
||||
[apply]<class TOrError>(
|
||||
const TOrError& _t_or_err) -> rfl::Result<U> {
|
||||
if constexpr (!std::is_same<TOrError, Error>()) {
|
||||
return apply(_t_or_err);
|
||||
} else {
|
||||
return _t_or_err;
|
||||
}
|
||||
};
|
||||
|
||||
return std::visit(handle_variant, t_or_err_);
|
||||
}
|
||||
|
||||
/// Returns the value if the result does not contain an error, throws an
|
||||
/// exceptions if not. Similar to .unwrap() in Rust.
|
||||
ValueType& value() {
|
||||
const auto handle_variant =
|
||||
[&]<class TOrError>(TOrError& _t_or_err) -> ValueType& {
|
||||
if constexpr (!std::is_same<TOrError, Error>()) {
|
||||
if constexpr (is_helper_tuple<T>()) {
|
||||
return _t_or_err.tuple_;
|
||||
} else {
|
||||
return _t_or_err;
|
||||
}
|
||||
} else {
|
||||
throw std::runtime_error(_t_or_err.what());
|
||||
}
|
||||
};
|
||||
return std::visit(handle_variant, t_or_err_);
|
||||
}
|
||||
|
||||
/// Returns the value if the result does not contain an error, throws an
|
||||
/// exceptions if not. Similar to .unwrap() in Rust.
|
||||
const ValueType& value() const {
|
||||
const auto handle_variant =
|
||||
[&]<class TOrError>(const TOrError& _t_or_err) -> const ValueType& {
|
||||
if constexpr (!std::is_same<TOrError, Error>()) {
|
||||
if constexpr (is_helper_tuple<T>()) {
|
||||
return _t_or_err.tuple_;
|
||||
} else {
|
||||
return _t_or_err;
|
||||
}
|
||||
} else {
|
||||
throw std::runtime_error(_t_or_err.what());
|
||||
}
|
||||
};
|
||||
return std::visit(handle_variant, t_or_err_);
|
||||
}
|
||||
|
||||
/// Returns the value or a default.
|
||||
T value_or(const T& _default) const noexcept {
|
||||
const auto handle_variant =
|
||||
[&]<class TOrError>(const TOrError& _t_or_err) -> T {
|
||||
if constexpr (!std::is_same<TOrError, Error>()) {
|
||||
return _t_or_err;
|
||||
} else {
|
||||
return _default;
|
||||
}
|
||||
};
|
||||
return std::visit(handle_variant, t_or_err_);
|
||||
}
|
||||
|
||||
private:
|
||||
/// The underlying variant, can either be T or Error.
|
||||
std::variant<T, Error> t_or_err_;
|
||||
};
|
||||
|
||||
/// operator* allows to combine result types as a product type.
|
||||
|
||||
template <class T, class U>
|
||||
inline auto operator*(const Result<T>& _rt, const Result<U>& _ru) {
|
||||
const auto f1 = [&_ru](const T& _t) {
|
||||
const auto f2 = [&_t](const U& _u) {
|
||||
return HelperTuple<T, U>(std::make_tuple(_t, _u));
|
||||
};
|
||||
return _ru.transform(f2);
|
||||
};
|
||||
return _rt.and_then(f1);
|
||||
}
|
||||
|
||||
template <class... T, class... U>
|
||||
inline auto operator*(const Result<HelperTuple<T...>>& _rt,
|
||||
const Result<HelperTuple<U...>>& _ru) {
|
||||
const auto f1 = [&_ru](const T&... _t) {
|
||||
const auto f2 = [&](const U&... _u) {
|
||||
return HelperTuple<T..., U...>(std::make_tuple(_t..., _u...));
|
||||
};
|
||||
return _ru.transform(f2);
|
||||
};
|
||||
return _rt.and_then(f1);
|
||||
}
|
||||
|
||||
template <class... T, class U>
|
||||
inline auto operator*(const Result<const HelperTuple<T...>>& _rt,
|
||||
const Result<U>& _ru) {
|
||||
const auto f = [](const U& _u) {
|
||||
return HelperTuple<U>(std::make_tuple(_u));
|
||||
};
|
||||
return _rt * _ru.transform(f);
|
||||
}
|
||||
|
||||
template <class T, class... U>
|
||||
inline auto operator*(const Result<T>& _rt,
|
||||
const Result<HelperTuple<U...>>& _ru) {
|
||||
const auto f = [](const T& _t) {
|
||||
return HelperTuple<T>(std::make_tuple(_t));
|
||||
};
|
||||
return _rt.transform(f) * _ru;
|
||||
}
|
||||
|
||||
template <class T, class U>
|
||||
inline auto operator*(const Result<T>& _rt, const U& _u) {
|
||||
return _rt * Result<U>(_u);
|
||||
}
|
||||
|
||||
template <class T, class U>
|
||||
inline auto operator*(const T& _t, const Result<U>& _ru) {
|
||||
return Result<T>(_t) * _ru;
|
||||
}
|
||||
|
||||
template <class T, class U>
|
||||
inline auto operator*(Result<T>&& _rt, Result<U>&& _ru) {
|
||||
const auto f1 = [&_ru](T&& _t) {
|
||||
const auto f2 = [&_t](U&& _u) {
|
||||
return HelperTuple<T, U>(std::forward_as_tuple(_t, _u));
|
||||
};
|
||||
return _ru.transform(f2);
|
||||
};
|
||||
return _rt.and_then(f1);
|
||||
}
|
||||
|
||||
template <class... T, class... U>
|
||||
inline auto operator*(Result<HelperTuple<T...>>&& _rt,
|
||||
Result<HelperTuple<U...>>&& _ru) {
|
||||
const auto f1 = [&_ru](T&&... _t) {
|
||||
const auto f2 = [&](U&&... _u) {
|
||||
return HelperTuple<T..., U...>(std::forward_as_tuple(_t..., _u...));
|
||||
};
|
||||
return _ru.transform(f2);
|
||||
};
|
||||
return _rt.and_then(f1);
|
||||
}
|
||||
|
||||
template <class... T, class U>
|
||||
inline auto operator*(Result<HelperTuple<T...>>&& _rt, Result<U>&& _ru) {
|
||||
const auto f = [](U&& _u) {
|
||||
return HelperTuple<U>(std::forward_as_tuple(_u));
|
||||
};
|
||||
return _rt * _ru.transform(f);
|
||||
}
|
||||
|
||||
template <class T, class... U>
|
||||
inline auto operator*(Result<T>&& _rt, Result<HelperTuple<U...>>&& _ru) {
|
||||
const auto f = [](T&& _t) {
|
||||
return HelperTuple<T>(std::forward_as_tuple(_t));
|
||||
};
|
||||
return _rt.transform(f) * _ru;
|
||||
}
|
||||
|
||||
template <class T, class U>
|
||||
inline auto operator*(Result<T>&& _rt, U&& _u) {
|
||||
return _rt * Result<U>(_u);
|
||||
}
|
||||
|
||||
template <class T, class U>
|
||||
inline auto operator*(T&& _t, Result<U>&& _ru) {
|
||||
return Result<T>(_t) * _ru;
|
||||
}
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
#ifndef RFL_TAGGEDUNION_HPP_
|
||||
#define RFL_TAGGEDUNION_HPP_
|
||||
|
||||
#include <variant>
|
||||
|
||||
#include "rfl/internal/StringLiteral.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
// https://serde.rs/enum-representations.html
|
||||
template <internal::StringLiteral _discriminator, class... NamedTupleTypes>
|
||||
struct TaggedUnion {
|
||||
static constexpr internal::StringLiteral discrimininator_ = _discriminator;
|
||||
|
||||
/// The type of the underlying variant.
|
||||
using VariantType = std::variant<NamedTupleTypes...>;
|
||||
|
||||
/// Returns the underlying variant.
|
||||
inline VariantType& variant() { return variant_; }
|
||||
|
||||
/// Returns the underlying variant.
|
||||
inline const VariantType& variant() const { return variant_; }
|
||||
|
||||
/// The underlying variant - a TaggedUnion is a thin wrapper
|
||||
/// around a variant that is mainly used for parsing.
|
||||
VariantType variant_;
|
||||
};
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif // RFL_TAGGEDUNION_HPP_
|
||||
@@ -0,0 +1,12 @@
|
||||
#ifndef RFL_ALWAYSFALSE_HPP_
|
||||
#define RFL_ALWAYSFALSE_HPP_
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// To be used inside visitor patterns
|
||||
template <class>
|
||||
inline constexpr bool always_false_v = false;
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif // RFL_ALWAYSFALSE_HPP_
|
||||
@@ -0,0 +1,35 @@
|
||||
#ifndef RFL_AS_HPP_
|
||||
#define RFL_AS_HPP_
|
||||
|
||||
#include "rfl/from_named_tuple.hpp"
|
||||
#include "rfl/make_named_tuple.hpp"
|
||||
#include "rfl/to_named_tuple.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// Generates a type T from the input values.
|
||||
template <class T, class Head, class... Tail>
|
||||
T as(Head&& _head, Tail&&... _tail) {
|
||||
if constexpr (sizeof...(_tail) == 0) {
|
||||
return from_named_tuple<T>(to_named_tuple(std::forward<Head>(_head)));
|
||||
} else {
|
||||
return from_named_tuple<T>(
|
||||
to_named_tuple(std::forward<Head>(_head))
|
||||
.add(to_named_tuple(std::forward<Tail>(_tail))...));
|
||||
}
|
||||
}
|
||||
|
||||
/// Generates a type T from the input values.
|
||||
template <class T, class Head, class... Tail>
|
||||
T as(const Head& _head, const Tail&... _tail) {
|
||||
if constexpr (sizeof...(_tail) == 0) {
|
||||
return from_named_tuple<T>(to_named_tuple(_head));
|
||||
} else {
|
||||
return from_named_tuple<T>(
|
||||
to_named_tuple(_head).add(to_named_tuple(_tail)...));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,14 @@
|
||||
#ifndef RFL_DEFAULT_HPP_
|
||||
#define RFL_DEFAULT_HPP_
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// Helper class that can be passed to a field
|
||||
/// to trigger the default value of the type.
|
||||
struct Default {};
|
||||
|
||||
inline static const auto default_value = Default{};
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef RFL_DEFINELITERAL_HPP_
|
||||
#define RFL_DEFINELITERAL_HPP_
|
||||
|
||||
#include "rfl/Literal.hpp"
|
||||
#include "rfl/internal/define_literal.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// Allows you to combine several literal types.
|
||||
template <class... LiteralTypes>
|
||||
using define_literal_t =
|
||||
typename internal::define_literal<LiteralTypes...>::type;
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif // RFL_DEFINELITERAL_HPP_
|
||||
@@ -0,0 +1,15 @@
|
||||
#ifndef RFL_DEFINENAMEDTUPLE_HPP_
|
||||
#define RFL_DEFINENAMEDTUPLE_HPP_
|
||||
|
||||
#include "rfl/NamedTuple.hpp"
|
||||
#include "rfl/internal/define_named_tuple.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
template <class... FieldTypes>
|
||||
using define_named_tuple_t =
|
||||
typename internal::define_named_tuple<FieldTypes...>::type;
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif // RFL_DEFINENAMEDTUPLE_HPP_
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef RFL_DEFINETAGGEDUNION_HPP_
|
||||
#define RFL_DEFINETAGGEDUNION_HPP_
|
||||
|
||||
#include "rfl/TaggedUnion.hpp"
|
||||
#include "rfl/internal/StringLiteral.hpp"
|
||||
#include "rfl/internal/define_tagged_union.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
template <internal::StringLiteral _discriminator, class... TaggedUnionTypes>
|
||||
using define_tagged_union_t =
|
||||
typename internal::define_tagged_union<_discriminator,
|
||||
TaggedUnionTypes...>::type;
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,15 @@
|
||||
#ifndef RFL_DEFINEVARIANT_HPP_
|
||||
#define RFL_DEFINEVARIANT_HPP_
|
||||
|
||||
#include <variant>
|
||||
|
||||
#include "rfl/internal/define_variant.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
template <class... Vars>
|
||||
using define_variant_t = typename internal::define_variant<Vars...>::type;
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif // RFL_DEFINEVARIANT_HPP_
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef RFL_EXTRACTDISTRIMINATORS_HPP_
|
||||
#define RFL_EXTRACTDISTRIMINATORS_HPP_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "rfl/TaggedUnion.hpp"
|
||||
#include "rfl/define_literal.hpp"
|
||||
#include "rfl/field_type.hpp"
|
||||
#include "rfl/internal/extract_discriminators.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// Extracts a Literal containing all of the discriminators from a TaggedUnion.
|
||||
template <class TaggedUnionType>
|
||||
using extract_discriminators_t =
|
||||
typename internal::extract_discriminators<TaggedUnionType>::type;
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif // RFL_EXTRACTDISTRIMINATORS_HPP_
|
||||
@@ -0,0 +1,19 @@
|
||||
#ifndef RFL_FIELD_TYPE_HPP_
|
||||
#define RFL_FIELD_TYPE_HPP_
|
||||
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <variant>
|
||||
|
||||
#include "rfl/internal/StringLiteral.hpp"
|
||||
#include "rfl/internal/field_type.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
template <internal::StringLiteral _field_name, class NamedTupleType>
|
||||
using field_type_t =
|
||||
typename internal::FieldType<_field_name, NamedTupleType>::Type;
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,93 @@
|
||||
#ifndef RFL_FROM_NAMED_TUPLE_HPP_
|
||||
#define RFL_FROM_NAMED_TUPLE_HPP_
|
||||
|
||||
#include <functional>
|
||||
#include <type_traits>
|
||||
|
||||
#include "rfl/internal/field_tuple_t.hpp"
|
||||
#include "rfl/internal/has_flatten_fields.hpp"
|
||||
#include "rfl/internal/is_named_tuple.hpp"
|
||||
#include "rfl/named_tuple_t.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
template <class FieldTupleType, class NamedTupleType, class... Args>
|
||||
FieldTupleType concat_named_tuple(const NamedTupleType& _n, Args&&... _args);
|
||||
|
||||
/// Creates a struct of type T from a named tuple.
|
||||
/// All fields of the struct must be an rfl::Field.
|
||||
template <class T, class NamedTupleType>
|
||||
T from_named_tuple(NamedTupleType&& _n) {
|
||||
using RequiredType = std::decay_t<named_tuple_t<T>>;
|
||||
|
||||
if constexpr (internal::is_named_tuple_v<std::decay_t<T>>) {
|
||||
return std::forward<NamedTupleType>(_n);
|
||||
} else if constexpr (std::is_same<std::decay_t<NamedTupleType>,
|
||||
RequiredType>()) {
|
||||
const auto make = []<class... FieldTypes>(FieldTypes&&... _fields) {
|
||||
return T{std::forward<FieldTypes>(_fields)...};
|
||||
};
|
||||
|
||||
using FieldTupleType = internal::field_tuple_t<std::decay_t<T>>;
|
||||
|
||||
if constexpr (!internal::has_flatten_fields<FieldTupleType>()) {
|
||||
return std::apply(make, _n.fields());
|
||||
} else {
|
||||
const auto fields = concat_named_tuple<FieldTupleType>(_n);
|
||||
return std::apply(make, fields);
|
||||
}
|
||||
|
||||
} else {
|
||||
return from_named_tuple<T, RequiredType>(
|
||||
RequiredType(std::forward<NamedTupleType>(_n)));
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a struct of type T from a named tuple.
|
||||
/// All fields of the struct must be an rfl::Field.
|
||||
template <class T, class NamedTupleType>
|
||||
T from_named_tuple(const NamedTupleType& _n) {
|
||||
using RequiredType = std::decay_t<named_tuple_t<T>>;
|
||||
if constexpr (internal::is_named_tuple_v<std::decay_t<T>>) {
|
||||
return _n;
|
||||
|
||||
} else if constexpr (std::is_same<std::decay_t<NamedTupleType>,
|
||||
RequiredType>()) {
|
||||
const auto make = [](const auto&... _fields) { return T{_fields...}; };
|
||||
|
||||
using FieldTupleType = internal::field_tuple_t<T>;
|
||||
|
||||
if constexpr (!internal::has_flatten_fields<FieldTupleType>()) {
|
||||
return std::apply(make, _n.fields());
|
||||
} else {
|
||||
const auto fields = concat_named_tuple<FieldTupleType>(_n);
|
||||
return std::apply(make, fields);
|
||||
}
|
||||
|
||||
} else {
|
||||
return from_named_tuple<T, RequiredType>(RequiredType(_n));
|
||||
}
|
||||
}
|
||||
|
||||
template <class FieldTupleType, class NamedTupleType, class... Args>
|
||||
FieldTupleType concat_named_tuple(const NamedTupleType& _n, Args&&... _args) {
|
||||
constexpr auto i = sizeof...(Args);
|
||||
if constexpr (i == std::tuple_size_v<std::decay_t<FieldTupleType>>) {
|
||||
return std::make_tuple(std::forward<Args>(_args)...);
|
||||
} else {
|
||||
using T = std::tuple_element_t<i, FieldTupleType>;
|
||||
if constexpr (internal::is_flatten_field<T>::value) {
|
||||
return concat_named_tuple<FieldTupleType>(
|
||||
_n, std::forward<Args>(_args)...,
|
||||
from_named_tuple<typename T::Type>(_n));
|
||||
} else {
|
||||
return concat_named_tuple<FieldTupleType>(
|
||||
_n, std::forward<Args>(_args)...,
|
||||
_n.template get_field<T::name_>());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,48 @@
|
||||
#ifndef RFL_GET_HPP_
|
||||
#define RFL_GET_HPP_
|
||||
|
||||
#include "rfl/internal/Getter.hpp"
|
||||
#include "rfl/internal/StringLiteral.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// Gets a field by index.
|
||||
template <int _index, class NamedTupleType>
|
||||
inline auto& get(NamedTupleType& _tup) {
|
||||
return internal::Getter<NamedTupleType>::template get<_index>(_tup);
|
||||
}
|
||||
|
||||
/// Gets a field by name.
|
||||
template <internal::StringLiteral _field_name, class NamedTupleType>
|
||||
inline auto& get(NamedTupleType& _tup) {
|
||||
return internal::Getter<NamedTupleType>::template get<_field_name>(_tup);
|
||||
}
|
||||
|
||||
/// Gets a field by the field type.
|
||||
template <class Field, class NamedTupleType>
|
||||
inline auto& get(NamedTupleType& _tup) {
|
||||
return internal::Getter<NamedTupleType>::template get<Field>(_tup);
|
||||
}
|
||||
|
||||
/// Gets a field by index.
|
||||
template <int _index, class NamedTupleType>
|
||||
inline const auto& get(const NamedTupleType& _tup) {
|
||||
return internal::Getter<NamedTupleType>::template get_const<_index>(_tup);
|
||||
}
|
||||
|
||||
/// Gets a field by name.
|
||||
template <internal::StringLiteral _field_name, class NamedTupleType>
|
||||
inline const auto& get(const NamedTupleType& _tup) {
|
||||
return internal::Getter<NamedTupleType>::template get_const<_field_name>(
|
||||
_tup);
|
||||
}
|
||||
|
||||
/// Gets a field by the field type.
|
||||
template <class Field, class NamedTupleType>
|
||||
inline const auto& get(const NamedTupleType& _tup) {
|
||||
return internal::Getter<NamedTupleType>::template get_const<Field>(_tup);
|
||||
}
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,216 @@
|
||||
#ifndef RFL_INTERNAL_GETTER_HPP_
|
||||
#define RFL_INTERNAL_GETTER_HPP_
|
||||
|
||||
#include <tuple>
|
||||
#include <variant>
|
||||
|
||||
#include "rfl/TaggedUnion.hpp"
|
||||
#include "rfl/internal/StringLiteral.hpp"
|
||||
#include "rfl/internal/find_index.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
template <class NamedTupleType>
|
||||
struct Getter;
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/// Default case - anything that cannot be explicitly matched.
|
||||
template <class NamedTupleType>
|
||||
struct Getter {
|
||||
public:
|
||||
/// Retrieves the indicated value from the tuple.
|
||||
template <int _index>
|
||||
static inline auto& get(NamedTupleType& _tup) {
|
||||
return std::get<_index>(_tup.values());
|
||||
}
|
||||
|
||||
/// Gets a field by name.
|
||||
template <StringLiteral _field_name>
|
||||
static inline auto& get(NamedTupleType& _tup) {
|
||||
constexpr auto index =
|
||||
find_index<_field_name, typename NamedTupleType::Fields>();
|
||||
return Getter<NamedTupleType>::template get<index>(_tup);
|
||||
}
|
||||
|
||||
/// Gets a field by the field type.
|
||||
template <class Field>
|
||||
static inline auto& get(NamedTupleType& _tup) {
|
||||
constexpr auto index =
|
||||
find_index<Field::name_, typename NamedTupleType::Fields>();
|
||||
static_assert(
|
||||
std::is_same<
|
||||
typename std::tuple_element<
|
||||
index, typename NamedTupleType::Fields>::type::Type,
|
||||
typename Field::Type>(),
|
||||
"If two fields have the same name, "
|
||||
"their type must be the same as "
|
||||
"well.");
|
||||
return Getter<NamedTupleType>::template get<index>(_tup);
|
||||
}
|
||||
|
||||
/// Retrieves the indicated value from the tuple.
|
||||
template <int _index>
|
||||
static inline const auto& get_const(const NamedTupleType& _tup) {
|
||||
return std::get<_index>(_tup.values());
|
||||
}
|
||||
|
||||
/// Gets a field by name.
|
||||
template <StringLiteral _field_name>
|
||||
static inline const auto& get_const(const NamedTupleType& _tup) {
|
||||
constexpr auto index =
|
||||
find_index<_field_name, typename NamedTupleType::Fields>();
|
||||
return Getter<NamedTupleType>::template get_const<index>(_tup);
|
||||
}
|
||||
|
||||
/// Gets a field by the field type.
|
||||
template <class Field>
|
||||
static inline const auto& get_const(const NamedTupleType& _tup) {
|
||||
constexpr auto index =
|
||||
find_index<Field::name_, typename NamedTupleType::Fields>();
|
||||
static_assert(
|
||||
std::is_same<
|
||||
typename std::tuple_element<
|
||||
index, typename NamedTupleType::Fields>::type::Type,
|
||||
typename Field::Type>(),
|
||||
"If two fields have the same name, "
|
||||
"their type must be the same as "
|
||||
"well.");
|
||||
return Getter<NamedTupleType>::template get_const<index>(_tup);
|
||||
}
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/// For handling std::variant.
|
||||
template <class... NamedTupleTypes>
|
||||
struct Getter<std::variant<NamedTupleTypes...>> {
|
||||
public:
|
||||
/// Retrieves the indicated value from the tuple.
|
||||
template <int _index>
|
||||
static inline auto& get(std::variant<NamedTupleTypes...>& _tup) {
|
||||
const auto apply = [](auto& _t) -> auto& {
|
||||
using NamedTupleType = std::decay_t<decltype(_t)>;
|
||||
return Getter<NamedTupleType>::template get<_index>(_t);
|
||||
};
|
||||
return std::visit(apply, _tup);
|
||||
}
|
||||
|
||||
/// Gets a field by name.
|
||||
template <StringLiteral _field_name>
|
||||
static inline auto& get(std::variant<NamedTupleTypes...>& _tup) {
|
||||
const auto apply = [](auto& _t) -> auto& {
|
||||
using NamedTupleType = std::decay_t<decltype(_t)>;
|
||||
return Getter<NamedTupleType>::template get<_field_name>(_t);
|
||||
};
|
||||
return std::visit(apply, _tup);
|
||||
}
|
||||
|
||||
/// Gets a field by the field type.
|
||||
template <class Field>
|
||||
static inline auto& get(std::variant<NamedTupleTypes...>& _tup) {
|
||||
const auto apply = [](auto& _t) -> auto& {
|
||||
using NamedTupleType = std::decay_t<decltype(_t)>;
|
||||
return Getter<NamedTupleType>::template get<Field>(_t);
|
||||
};
|
||||
return std::visit(apply, _tup);
|
||||
}
|
||||
|
||||
/// Retrieves the indicated value from the tuple.
|
||||
template <int _index>
|
||||
static inline const auto& get_const(
|
||||
const std::variant<NamedTupleTypes...>& _tup) {
|
||||
const auto apply = [](const auto& _tup) -> const auto& {
|
||||
using NamedTupleType = std::decay_t<decltype(_tup)>;
|
||||
return Getter<NamedTupleType>::template get_const<_index>(_tup);
|
||||
};
|
||||
return std::visit(apply, _tup);
|
||||
}
|
||||
|
||||
/// Gets a field by name.
|
||||
template <StringLiteral _field_name>
|
||||
static inline const auto& get_const(
|
||||
const std::variant<NamedTupleTypes...>& _tup) {
|
||||
const auto apply = [](const auto& _t) -> const auto& {
|
||||
using NamedTupleType = std::decay_t<decltype(_t)>;
|
||||
return Getter<NamedTupleType>::template get_const<_field_name>(_t);
|
||||
};
|
||||
return std::visit(apply, _tup);
|
||||
}
|
||||
|
||||
/// Gets a field by the field type.
|
||||
template <class Field>
|
||||
static inline const auto& get_const(
|
||||
const std::variant<NamedTupleTypes...>& _tup) {
|
||||
const auto apply = [](const auto& _t) -> const auto& {
|
||||
using NamedTupleType = std::decay_t<decltype(_t)>;
|
||||
return Getter<NamedTupleType>::template get_const<Field>(_t);
|
||||
};
|
||||
return std::visit(apply, _tup);
|
||||
}
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/// 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
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,45 @@
|
||||
#ifndef RFL_INTERNAL_STRINGLITERAL_HPP_
|
||||
#define RFL_INTERNAL_STRINGLITERAL_HPP_
|
||||
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
/// Normal strings cannot be used as template
|
||||
/// parameters, but this can. This is needed
|
||||
/// for the parameters names in the NamedTuples.
|
||||
template <size_t N>
|
||||
struct StringLiteral {
|
||||
constexpr StringLiteral(const char (&str)[N]) {
|
||||
std::copy_n(str, N, value_);
|
||||
}
|
||||
|
||||
char value_[N];
|
||||
|
||||
/// Returns the value as a string.
|
||||
std::string str() const { return std::string(value_); }
|
||||
};
|
||||
|
||||
template <size_t N1, size_t N2>
|
||||
constexpr inline bool operator==(const StringLiteral<N1>& _first,
|
||||
const StringLiteral<N2>& _second) {
|
||||
if constexpr (N1 != N2) {
|
||||
return false;
|
||||
}
|
||||
return std::string_view(_first.value_, N1) ==
|
||||
std::string_view(_second.value_, N2);
|
||||
}
|
||||
|
||||
template <size_t N1, size_t N2>
|
||||
constexpr inline bool operator!=(const StringLiteral<N1>& _first,
|
||||
const StringLiteral<N2>& _second) {
|
||||
return !(_first == _second);
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,29 @@
|
||||
#ifndef RFL_INTERNAL_VISITTREE_HPP_
|
||||
#define RFL_INTERNAL_VISITTREE_HPP_
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
struct VisitTree {
|
||||
/// Evaluates a visitor pattern using a tree-like structure.
|
||||
template <int _begin, int _end, class Visitor, class... Args>
|
||||
static inline auto visit(const auto& _v, const int _i,
|
||||
const Args&... _args) {
|
||||
static_assert(_end > _begin, "_end needs to be greater than _begin.");
|
||||
if constexpr (_end - _begin == 1) {
|
||||
return _v.template visit<_begin>(_args...);
|
||||
} else {
|
||||
constexpr int middle = (_begin + _end) / 2;
|
||||
if (_i < middle) {
|
||||
return visit<_begin, middle, Visitor>(_v, _i, _args...);
|
||||
} else {
|
||||
return visit<middle, _end, Visitor>(_v, _i, _args...);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,27 @@
|
||||
#ifndef RFL_INTERNAL_VISITORWRAPPER_HPP_
|
||||
#define RFL_INTERNAL_VISITORWRAPPER_HPP_
|
||||
|
||||
#include "rfl/Literal.hpp"
|
||||
#include "rfl/TaggedUnion.hpp"
|
||||
#include "rfl/internal/StringLiteral.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
/// Necessary for the VisitTree structure.
|
||||
template <class Visitor, internal::StringLiteral... _fields>
|
||||
struct VisitorWrapper {
|
||||
/// Calls the underlying visitor when required to do so.
|
||||
template <int _i, class... Args>
|
||||
inline auto visit(const Args&... _args) const {
|
||||
return (*visitor_)(name_of<Literal<_fields...>, _i>(), _args...);
|
||||
}
|
||||
|
||||
/// The underlying visitor.
|
||||
const Visitor* visitor_;
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif // RFL_VISIT_HPP_
|
||||
@@ -0,0 +1,29 @@
|
||||
#ifndef RFL_INTERNAL_DEFINELITERAL_HPP_
|
||||
#define RFL_INTERNAL_DEFINELITERAL_HPP_
|
||||
|
||||
#include "rfl/Literal.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
/// Allows you to combine several literals.
|
||||
template <class... LiteralTypes>
|
||||
struct define_literal;
|
||||
|
||||
/// General case
|
||||
template <StringLiteral... _content1, StringLiteral... _content2, class... Tail>
|
||||
struct define_literal<Literal<_content1...>, Literal<_content2...>, Tail...> {
|
||||
using type = typename define_literal<Literal<_content1..., _content2...>,
|
||||
Tail...>::type;
|
||||
};
|
||||
|
||||
/// Special case - only a single literal is left
|
||||
template <StringLiteral... _content>
|
||||
struct define_literal<Literal<_content...>> {
|
||||
using type = Literal<_content...>;
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,46 @@
|
||||
#ifndef RFL_INTERNAL_DEFINENAMEDTUPLE_HPP_
|
||||
#define RFL_INTERNAL_DEFINENAMEDTUPLE_HPP_
|
||||
|
||||
#include "rfl/NamedTuple.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class... FieldTypes>
|
||||
struct define_named_tuple;
|
||||
|
||||
/// Allows you to combine several named tuples and/or additional fields.
|
||||
/// Recursive case - all types are fields.
|
||||
template <class Head, class... Tail>
|
||||
struct define_named_tuple<Head, Tail...> {
|
||||
using type = typename define_named_tuple<NamedTuple<Head>, Tail...>::type;
|
||||
};
|
||||
|
||||
/// Allows you to combine several named tuples and/or additional fields.
|
||||
/// Recursive case - first type is NamedTuple, second type is field.
|
||||
template <class Head, class... TupContent, class... Tail>
|
||||
struct define_named_tuple<NamedTuple<TupContent...>, Head, Tail...> {
|
||||
using type = typename define_named_tuple<NamedTuple<TupContent..., Head>,
|
||||
Tail...>::type;
|
||||
};
|
||||
|
||||
/// Allows you to combine several named tuples and/or additional fields.
|
||||
/// Recursive case - first type is NamedTuple, second type is also NamedTuple.
|
||||
template <class... TupContent, class... TupContent2, class... Tail>
|
||||
struct define_named_tuple<NamedTuple<TupContent...>, NamedTuple<TupContent2...>,
|
||||
Tail...> {
|
||||
using type =
|
||||
typename define_named_tuple<NamedTuple<TupContent..., TupContent2...>,
|
||||
Tail...>::type;
|
||||
};
|
||||
|
||||
/// Allows you to combine several named tuples and/or additional fields.
|
||||
template <class... TupContent>
|
||||
struct define_named_tuple<NamedTuple<TupContent...>> {
|
||||
using type = NamedTuple<TupContent...>;
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,57 @@
|
||||
#ifndef RFL_INTERNAL_DEFINETAGGEDUNION_HPP_
|
||||
#define RFL_INTERNAL_DEFINETAGGEDUNION_HPP_
|
||||
|
||||
#include "rfl/TaggedUnion.hpp"
|
||||
#include "rfl/internal/StringLiteral.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
/// Allows you to combine several tagged unions.
|
||||
template <StringLiteral _discriminator, class... TaggedUnionTypes>
|
||||
struct define_tagged_union;
|
||||
|
||||
/// Recursive case - both tagged union.
|
||||
template <StringLiteral _discriminator, class... NamedTupleTypes1,
|
||||
class... NamedTupleTypes2, class... Tail>
|
||||
struct define_tagged_union<
|
||||
_discriminator, TaggedUnion<_discriminator, NamedTupleTypes1...>,
|
||||
TaggedUnion<_discriminator, NamedTupleTypes2...>, Tail...> {
|
||||
using type = typename define_tagged_union<
|
||||
_discriminator,
|
||||
TaggedUnion<_discriminator, NamedTupleTypes1..., NamedTupleTypes2...>,
|
||||
Tail...>::type;
|
||||
};
|
||||
|
||||
/// Recursive case - tagged union plus named tuple.
|
||||
template <StringLiteral _discriminator, class... NamedTupleTypes,
|
||||
class... FieldTypes, class... Tail>
|
||||
struct define_tagged_union<_discriminator,
|
||||
TaggedUnion<_discriminator, NamedTupleTypes...>,
|
||||
NamedTuple<FieldTypes...>, Tail...> {
|
||||
using type = typename define_tagged_union<
|
||||
_discriminator,
|
||||
TaggedUnion<_discriminator, NamedTupleTypes...,
|
||||
NamedTuple<FieldTypes...>>,
|
||||
Tail...>::type;
|
||||
};
|
||||
|
||||
/// Recursive case - named tuple.
|
||||
template <StringLiteral _discriminator, class... FieldTypes, class... Tail>
|
||||
struct define_tagged_union<_discriminator, NamedTuple<FieldTypes...>, Tail...> {
|
||||
using type = typename define_tagged_union<
|
||||
_discriminator, TaggedUnion<_discriminator, NamedTuple<FieldTypes...>>,
|
||||
Tail...>::type;
|
||||
};
|
||||
|
||||
/// Special case - only a single TaggedUnion is left.
|
||||
template <StringLiteral _discriminator, class... NamedTupleTypes>
|
||||
struct define_tagged_union<_discriminator,
|
||||
TaggedUnion<_discriminator, NamedTupleTypes...>> {
|
||||
using type = TaggedUnion<_discriminator, NamedTupleTypes...>;
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,42 @@
|
||||
#ifndef RFL_INTERNAL_DEFINEVARIANT_HPP_
|
||||
#define RFL_INTERNAL_DEFINEVARIANT_HPP_
|
||||
|
||||
#include <variant>
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
/// Allows you to combine several variants.
|
||||
template <class... Vars>
|
||||
struct define_variant;
|
||||
|
||||
/// Recursive case - both variants.
|
||||
template <class... Vars1, class... Vars2, class... Tail>
|
||||
struct define_variant<std::variant<Vars1...>, std::variant<Vars2...>, Tail...> {
|
||||
using type = typename define_variant<std::variant<Vars1..., Vars2...>,
|
||||
Tail...>::type;
|
||||
};
|
||||
|
||||
/// Recursive case - variant plus other type.
|
||||
template <class... Vars, class Head, class... Tail>
|
||||
struct define_variant<std::variant<Vars...>, Head, Tail...> {
|
||||
using type =
|
||||
typename define_variant<std::variant<Vars..., Head>, Tail...>::type;
|
||||
};
|
||||
|
||||
/// Recursive case - other type.
|
||||
template <class Head, class... Tail>
|
||||
struct define_variant<Head, Tail...> {
|
||||
using type = typename define_variant<std::variant<Head>, Tail...>::type;
|
||||
};
|
||||
|
||||
/// Special case - only a single variant is left.
|
||||
template <class... Vars>
|
||||
struct define_variant<std::variant<Vars...>> {
|
||||
using type = std::variant<Vars...>;
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef RFL_INTERNAL_EXTRACTDISTRIMINATORS_HPP_
|
||||
#define RFL_INTERNAL_EXTRACTDISTRIMINATORS_HPP_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "rfl/TaggedUnion.hpp"
|
||||
#include "rfl/define_literal.hpp"
|
||||
#include "rfl/field_type.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class TaggedUnionType>
|
||||
struct extract_discriminators;
|
||||
|
||||
template <StringLiteral _discriminator, class... NamedTupleType>
|
||||
struct extract_discriminators<TaggedUnion<_discriminator, NamedTupleType...>> {
|
||||
using type = define_literal_t<
|
||||
std::decay_t<field_type_t<_discriminator, NamedTupleType>>...>;
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef RFL_INTERNAL_FIELD_TUPLE_T_HPP_
|
||||
#define RFL_INTERNAL_FIELD_TUPLE_T_HPP_
|
||||
|
||||
#include <functional>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
|
||||
#include "rfl/internal/to_field_tuple.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class T>
|
||||
using field_tuple_t =
|
||||
typename std::invoke_result<decltype(to_field_tuple<T>), T>::type;
|
||||
|
||||
}
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,63 @@
|
||||
#ifndef RFL_INTERNAL_FIELD_TYPE_HPP_
|
||||
#define RFL_INTERNAL_FIELD_TYPE_HPP_
|
||||
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <variant>
|
||||
|
||||
#include "rfl/NamedTuple.hpp"
|
||||
#include "rfl/TaggedUnion.hpp"
|
||||
#include "rfl/internal/StringLiteral.hpp"
|
||||
#include "rfl/internal/find_index.hpp"
|
||||
#include "rfl/named_tuple_t.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class T, class... Ts>
|
||||
struct are_same : std::conjunction<std::is_same<T, Ts>...> {};
|
||||
|
||||
/// Finds the type of the field signified by _field_name
|
||||
template <StringLiteral _field_name, class T>
|
||||
struct FieldType;
|
||||
|
||||
/// Default option - for named tuples.
|
||||
template <StringLiteral _field_name, class T>
|
||||
struct FieldType {
|
||||
using NamedTupleType = named_tuple_t<T>;
|
||||
|
||||
static constexpr int field_ix_ =
|
||||
internal::find_index<_field_name, typename NamedTupleType::Fields>();
|
||||
|
||||
using Type = typename std::tuple_element<
|
||||
field_ix_, typename NamedTupleType::Fields>::type::Type;
|
||||
};
|
||||
|
||||
/// For variants - in this case the FieldType returned by all options must be
|
||||
/// the same.
|
||||
template <StringLiteral _field_name, class FirstAlternativeType,
|
||||
class... OtherAlternativeTypes>
|
||||
struct FieldType<_field_name,
|
||||
std::variant<FirstAlternativeType, OtherAlternativeTypes...>> {
|
||||
constexpr static bool all_types_match = std::conjunction_v<std::is_same<
|
||||
typename FieldType<_field_name, FirstAlternativeType>::Type,
|
||||
typename FieldType<_field_name, OtherAlternativeTypes>::Type>...>;
|
||||
|
||||
static_assert(all_types_match, "All field types must be the same.");
|
||||
|
||||
using Type = typename FieldType<_field_name, FirstAlternativeType>::Type;
|
||||
};
|
||||
|
||||
/// For tagged union - just defers to the variant.
|
||||
template <StringLiteral _field_name, StringLiteral _discriminator_name,
|
||||
class... VarTypes>
|
||||
struct FieldType<_field_name, TaggedUnion<_discriminator_name, VarTypes...>> {
|
||||
using Type = typename FieldType<
|
||||
_field_name, typename TaggedUnion<_discriminator_name,
|
||||
VarTypes...>::VariantType>::Type;
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,39 @@
|
||||
#ifndef RFL_FIND_INDEX_HPP_
|
||||
#define RFL_FIND_INDEX_HPP_
|
||||
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
|
||||
#include "rfl/internal/StringLiteral.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
/// Finds the index of the field signified by _field_name
|
||||
template <StringLiteral _field_name, class _Fields, int I = 0>
|
||||
constexpr static int find_index() {
|
||||
using FieldType =
|
||||
std::decay_t<typename std::tuple_element<I, _Fields>::type>;
|
||||
|
||||
constexpr bool name_i_matches = (FieldType::name_ == _field_name);
|
||||
|
||||
if constexpr (name_i_matches) {
|
||||
return I;
|
||||
} else {
|
||||
constexpr bool out_of_range = I + 1 == std::tuple_size<_Fields>{};
|
||||
|
||||
static_assert(!out_of_range, "Field name not found!");
|
||||
|
||||
if constexpr (out_of_range) {
|
||||
// This is to avoid very confusing error messages.
|
||||
return I;
|
||||
} else {
|
||||
return find_index<_field_name, _Fields, I + 1>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,27 @@
|
||||
#ifndef RFL_INTERNAL_HASFLATTENFIELDS_HPP_
|
||||
#define RFL_INTERNAL_HASFLATTENFIELDS_HPP_
|
||||
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include "rfl/internal/is_flatten_field.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class TupleType, int _i = 0>
|
||||
constexpr bool has_flatten_fields() {
|
||||
if constexpr (_i == std::tuple_size_v<TupleType>) {
|
||||
return false;
|
||||
} else {
|
||||
using T = std::tuple_element_t<_i, TupleType>;
|
||||
return is_flatten_field<T>::value ||
|
||||
has_flatten_fields<TupleType, _i + 1>();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,783 @@
|
||||
#ifndef RFL_INTERNAL_HAS_N_FIELDS_HPP_
|
||||
#define RFL_INTERNAL_HAS_N_FIELDS_HPP_
|
||||
|
||||
#include <cstddef>
|
||||
#include <type_traits>
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <typename T>
|
||||
concept aggregate = std::is_aggregate_v<T>;
|
||||
|
||||
template <typename T, typename... Args>
|
||||
concept constructable = aggregate<T> && requires {
|
||||
T{std::declval<Args>()...};
|
||||
};
|
||||
|
||||
struct any {
|
||||
template <typename T>
|
||||
constexpr operator T() const noexcept;
|
||||
};
|
||||
|
||||
/*The following boilerplate code was generated using a Python script:
|
||||
|
||||
def has_field_template(num: int) -> str:
|
||||
anys = "".join([", any"] * num)
|
||||
return f"""
|
||||
template <class T>
|
||||
constexpr bool has_{num}_fields = constructable<T{anys}>;
|
||||
"""
|
||||
code = "".join((has_field_template(i) for i in range(101)))
|
||||
|
||||
with open("generated_code.cpp", "w", encoding="utf-8") as codefile:
|
||||
codefile.write(code)
|
||||
|
||||
*/
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_0_fields = constructable<T>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_1_fields = constructable<T, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_2_fields = constructable<T, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_3_fields = constructable<T, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_4_fields = constructable<T, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_5_fields = constructable<T, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_6_fields = constructable<T, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_7_fields =
|
||||
constructable<T, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_8_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_9_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_10_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_11_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_12_fields = constructable<T, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_13_fields = constructable<T, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_14_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_15_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_16_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_17_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_18_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_19_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_20_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_21_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_22_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_23_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_24_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_25_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_26_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_27_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_28_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_29_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_30_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_31_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_32_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_33_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_34_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_35_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_36_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_37_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_38_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_39_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_40_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_41_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_42_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_43_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_44_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_45_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_46_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_47_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_48_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_49_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_50_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_51_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_52_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_53_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_54_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_55_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_56_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_57_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_58_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_59_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_60_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_61_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_62_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_63_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_64_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_65_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_66_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_67_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_68_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_69_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_70_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_71_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_72_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_73_fields = constructable<
|
||||
T, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_74_fields = constructable<
|
||||
T, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_75_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_76_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_77_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_78_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_79_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_80_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_81_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_82_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_83_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_84_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_85_fields = constructable<
|
||||
T, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_86_fields = constructable<
|
||||
T, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_87_fields = constructable<
|
||||
T, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_88_fields = constructable<
|
||||
T, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_89_fields = constructable<
|
||||
T, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_90_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_91_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_92_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_93_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_94_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_95_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_96_fields =
|
||||
constructable<T, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_97_fields = constructable<
|
||||
T, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_98_fields = constructable<
|
||||
T, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_99_fields = constructable<
|
||||
T, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
template <class T>
|
||||
constexpr bool has_100_fields = constructable<
|
||||
T, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any, any, any, any, any,
|
||||
any, any, any, any, any, any, any, any, any, any, any>;
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,29 @@
|
||||
#ifndef RFL_INTERNAL_HASREFLECTIONMETHODV_HPP_
|
||||
#define RFL_INTERNAL_HASREFLECTIONMETHODV_HPP_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <typename Wrapper>
|
||||
using reflection_method_t =
|
||||
decltype(std::declval<const Wrapper>().reflection());
|
||||
|
||||
template <typename Wrapper, typename = std::void_t<>>
|
||||
struct has_refl_m : std::false_type {};
|
||||
|
||||
template <typename Wrapper>
|
||||
struct has_refl_m<Wrapper, std::void_t<reflection_method_t<Wrapper>>>
|
||||
: std::true_type {};
|
||||
|
||||
/// Utility parameter for named tuple parsing, can be used by the
|
||||
/// parsers to determine whether a class or struct has a method
|
||||
/// called "reflection".
|
||||
template <typename Wrapper>
|
||||
constexpr bool has_reflection_method_v = has_refl_m<Wrapper>::value;
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,33 @@
|
||||
#ifndef RFL_HASREFLECTIONTYPEV_HPP_
|
||||
#define RFL_HASREFLECTIONTYPEV_HPP_
|
||||
|
||||
#include <cstdint>
|
||||
#include <utility>
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class Wrapper>
|
||||
class HasReflectionType {
|
||||
private:
|
||||
template <class U>
|
||||
static std::int64_t foo(...);
|
||||
|
||||
template <class U>
|
||||
static std::int32_t foo(typename U::ReflectionType*);
|
||||
|
||||
public:
|
||||
static constexpr bool value =
|
||||
sizeof(foo<Wrapper>(nullptr)) == sizeof(std::int32_t);
|
||||
};
|
||||
|
||||
/// Utility parameter for named tuple parsing, can be used by the
|
||||
/// parsers to determine whether a class or struct defines a type
|
||||
/// called "ReflectionType".
|
||||
template <typename Wrapper>
|
||||
constexpr bool has_reflection_type_v = HasReflectionType<Wrapper>::value;
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif // RFL_HASNAMEDTUPLETYPEV_HPP_
|
||||
@@ -0,0 +1,29 @@
|
||||
#ifndef RFL_INTERNAL_ISFIELD_HPP_
|
||||
#define RFL_INTERNAL_ISFIELD_HPP_
|
||||
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include "rfl/Field.hpp"
|
||||
#include "rfl/internal/StringLiteral.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class T>
|
||||
class is_field;
|
||||
|
||||
template <class T>
|
||||
class is_field : public std::false_type {};
|
||||
|
||||
template <StringLiteral _name, class Type>
|
||||
class is_field<Field<_name, Type>> : public std::true_type {};
|
||||
|
||||
template <class T>
|
||||
constexpr bool is_field_v = is_field<T>::value;
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,28 @@
|
||||
#ifndef RFL_INTERNAL_ISFLATTENFIELD_HPP_
|
||||
#define RFL_INTERNAL_ISFLATTENFIELD_HPP_
|
||||
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include "rfl/Flatten.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class T>
|
||||
class is_flatten_field;
|
||||
|
||||
template <class T>
|
||||
class is_flatten_field : public std::false_type {};
|
||||
|
||||
template <class T>
|
||||
class is_flatten_field<Flatten<T>> : public std::true_type {};
|
||||
|
||||
template <class T>
|
||||
constexpr bool is_flatten_field_v = is_flatten_field<T>::value;
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,26 @@
|
||||
#ifndef RFL_INTERNAL_IS_NAMED_TUPLE_HPP_
|
||||
#define RFL_INTERNAL_IS_NAMED_TUPLE_HPP_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "rfl/NamedTuple.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class T>
|
||||
class is_named_tuple;
|
||||
|
||||
template <class T>
|
||||
class is_named_tuple : public std::false_type {};
|
||||
|
||||
template <class... Fields>
|
||||
class is_named_tuple<NamedTuple<Fields...>> : public std::true_type {};
|
||||
|
||||
template <class T>
|
||||
constexpr bool is_named_tuple_v = is_named_tuple<T>::value;
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,43 @@
|
||||
#ifndef RFL_INTERNAL_NO_DUPLICATE_FIELD_NAMES_HPP_
|
||||
#define RFL_INTERNAL_NO_DUPLICATE_FIELD_NAMES_HPP_
|
||||
|
||||
#include <tuple>
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class Fields, int _i = 1, int _j = 0>
|
||||
constexpr inline bool no_duplicate_field_names() {
|
||||
constexpr auto num_fields = std::tuple_size<Fields>{};
|
||||
|
||||
if constexpr (num_fields <= 1) {
|
||||
return true;
|
||||
} else {
|
||||
if constexpr (_i == num_fields) {
|
||||
return true;
|
||||
} else if constexpr (_j == -1) {
|
||||
return no_duplicate_field_names<Fields, _i + 1, _i>();
|
||||
} else {
|
||||
using FieldType1 =
|
||||
std::decay_t<typename std::tuple_element<_i, Fields>::type>;
|
||||
using FieldType2 =
|
||||
std::decay_t<typename std::tuple_element<_j, Fields>::type>;
|
||||
|
||||
constexpr auto field_name_i = FieldType1::name_;
|
||||
constexpr auto field_name_j = FieldType2::name_;
|
||||
|
||||
constexpr bool no_duplicate = (field_name_i != field_name_j);
|
||||
|
||||
static_assert(no_duplicate,
|
||||
"Duplicate field names are not allowed");
|
||||
|
||||
return no_duplicate &&
|
||||
no_duplicate_field_names<Fields, _i, _j - 1>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,83 @@
|
||||
#ifndef RFL_INTERNAL_REMOVEFIELDS_HPP_
|
||||
#define RFL_INTERNAL_REMOVEFIELDS_HPP_
|
||||
|
||||
#include <algorithm>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
|
||||
#include "rfl/NamedTuple.hpp"
|
||||
#include "rfl/define_named_tuple.hpp"
|
||||
#include "rfl/internal/StringLiteral.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
/// Recursively builds a new NamedTuple type from the FieldTypes, leaving out
|
||||
/// the field signified by _name.
|
||||
template <class _OldNamedTupleType, StringLiteral _name,
|
||||
class _NewNamedTupleType, int _i>
|
||||
struct remove_single_field;
|
||||
|
||||
/// Special case - _i == 0
|
||||
template <class _OldNamedTupleType, StringLiteral _name,
|
||||
class _NewNamedTupleType>
|
||||
struct remove_single_field<_OldNamedTupleType, _name, _NewNamedTupleType, 0> {
|
||||
using type = _NewNamedTupleType;
|
||||
};
|
||||
|
||||
/// General case.
|
||||
template <class _OldNamedTupleType, StringLiteral _name,
|
||||
class _NewNamedTupleType, int _i>
|
||||
struct remove_single_field {
|
||||
using OldNamedTupleType = std::decay_t<_OldNamedTupleType>;
|
||||
|
||||
constexpr static int num_fields =
|
||||
std::tuple_size<typename OldNamedTupleType::Fields>{};
|
||||
|
||||
using FieldType = std::decay_t<typename std::tuple_element<
|
||||
num_fields - _i, typename OldNamedTupleType::Fields>::type>;
|
||||
|
||||
using NewNamedTupleType =
|
||||
std::conditional_t<_name == FieldType::name_, _NewNamedTupleType,
|
||||
define_named_tuple_t<_NewNamedTupleType, FieldType>>;
|
||||
|
||||
using type = typename remove_single_field<OldNamedTupleType, _name,
|
||||
NewNamedTupleType, _i - 1>::type;
|
||||
};
|
||||
|
||||
/// Recursively removes all of the fields signified by _head and _tail from the
|
||||
/// NamedTupleType.
|
||||
template <class _NamedTupleType, StringLiteral _head, StringLiteral... _tail>
|
||||
struct remove_fields;
|
||||
|
||||
/// Special case - only head is left.
|
||||
template <class _NamedTupleType, StringLiteral _head>
|
||||
struct remove_fields<_NamedTupleType, _head> {
|
||||
using NamedTupleType = std::decay_t<_NamedTupleType>;
|
||||
|
||||
constexpr static int num_fields =
|
||||
std::tuple_size<typename NamedTupleType::Fields>{};
|
||||
|
||||
using type = typename remove_single_field<NamedTupleType, _head,
|
||||
NamedTuple<>, num_fields>::type;
|
||||
};
|
||||
|
||||
/// General case.
|
||||
template <class _NamedTupleType, StringLiteral _head, StringLiteral... _tail>
|
||||
struct remove_fields {
|
||||
using NamedTupleType = std::decay_t<_NamedTupleType>;
|
||||
|
||||
constexpr static int num_fields =
|
||||
std::tuple_size<typename NamedTupleType::Fields>{};
|
||||
|
||||
using NewNamedTupleType =
|
||||
typename remove_single_field<NamedTupleType, _head, NamedTuple<>,
|
||||
num_fields>::type;
|
||||
|
||||
using type = typename remove_fields<NewNamedTupleType, _tail...>::type;
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif // RFL_REMOVEFIELDS_HPP_
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,10 @@
|
||||
#ifndef RFL_JSON_HPP_
|
||||
#define RFL_JSON_HPP_
|
||||
|
||||
#include "rfl/json/Parser.hpp"
|
||||
#include "rfl/json/Reader.hpp"
|
||||
#include "rfl/json/Writer.hpp"
|
||||
#include "rfl/json/read.hpp"
|
||||
#include "rfl/json/write.hpp"
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef RFL_JSON_PARSER_HPP_
|
||||
#define RFL_JSON_PARSER_HPP_
|
||||
|
||||
#include "rfl/json/Reader.hpp"
|
||||
#include "rfl/json/Writer.hpp"
|
||||
#include "rfl/parsing/Parser.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace json {
|
||||
|
||||
template <class T>
|
||||
using Parser = parsing::Parser<Reader, Writer, T>;
|
||||
|
||||
}
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,164 @@
|
||||
#ifndef RFL_JSON_READER_HPP_
|
||||
#define RFL_JSON_READER_HPP_
|
||||
|
||||
#include <yyjson.h>
|
||||
|
||||
#include <exception>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#include "rfl/Result.hpp"
|
||||
#include "rfl/always_false.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace json {
|
||||
|
||||
struct YYJSONOutputArray {
|
||||
yyjson_mut_val* val_;
|
||||
};
|
||||
|
||||
struct YYJSONOutputObject {
|
||||
yyjson_mut_val* val_;
|
||||
};
|
||||
|
||||
struct YYJSONOutputVar {
|
||||
YYJSONOutputVar(yyjson_mut_val* _val) : val_(_val) {}
|
||||
|
||||
YYJSONOutputVar(YYJSONOutputArray _arr) : val_(_arr.val_) {}
|
||||
|
||||
YYJSONOutputVar(YYJSONOutputObject _obj) : val_(_obj.val_) {}
|
||||
|
||||
yyjson_mut_val* val_;
|
||||
};
|
||||
|
||||
struct Reader {
|
||||
struct YYJSONInputArray {
|
||||
yyjson_val* val_;
|
||||
};
|
||||
|
||||
struct YYJSONInputObject {
|
||||
yyjson_val* val_;
|
||||
};
|
||||
|
||||
struct YYJSONInputVar {
|
||||
yyjson_val* val_;
|
||||
};
|
||||
|
||||
using InputArrayType = YYJSONInputArray;
|
||||
using InputObjectType = YYJSONInputObject;
|
||||
using InputVarType = YYJSONInputVar;
|
||||
|
||||
template <class T, class = void>
|
||||
struct has_from_json_obj : std::false_type {};
|
||||
|
||||
template <class T>
|
||||
struct has_from_json_obj<T, std::enable_if_t<std::is_invocable_r<
|
||||
T, decltype(T::from_json_obj),
|
||||
typename Reader::InputVarType>::value>>
|
||||
: std::true_type {};
|
||||
|
||||
template <class T>
|
||||
static constexpr bool has_custom_constructor = has_from_json_obj<T>::value;
|
||||
|
||||
rfl::Result<InputVarType> get_field(const std::string& _name,
|
||||
InputObjectType* _obj) const noexcept {
|
||||
const auto var =
|
||||
InputVarType(yyjson_obj_get(_obj->val_, _name.c_str()));
|
||||
if (!var.val_) {
|
||||
return rfl::Error("Object contains no field named '" + _name +
|
||||
"'.");
|
||||
}
|
||||
return var;
|
||||
}
|
||||
|
||||
bool is_empty(InputVarType* _var) const noexcept {
|
||||
return !_var->val_ || yyjson_is_null(_var->val_);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> to_basic_type(InputVarType* _var) const noexcept {
|
||||
if constexpr (std::is_same<std::decay_t<T>, std::string>()) {
|
||||
const auto r = yyjson_get_str(_var->val_);
|
||||
if (r == NULL) {
|
||||
return rfl::Error("Could not cast to string.");
|
||||
}
|
||||
return std::string(r);
|
||||
} else if constexpr (std::is_same<std::decay_t<T>, bool>()) {
|
||||
if (!yyjson_is_bool(_var->val_)) {
|
||||
return rfl::Error("Could not cast to boolean.");
|
||||
}
|
||||
return yyjson_get_bool(_var->val_);
|
||||
} else if constexpr (std::is_floating_point<std::decay_t<T>>()) {
|
||||
if (!yyjson_is_num(_var->val_)) {
|
||||
return rfl::Error("Could not cast to double.");
|
||||
}
|
||||
return static_cast<T>(yyjson_get_num(_var->val_));
|
||||
} else if constexpr (std::is_integral<std::decay_t<T>>()) {
|
||||
if (!yyjson_is_int(_var->val_)) {
|
||||
return rfl::Error("Could not cast to int.");
|
||||
}
|
||||
return static_cast<T>(yyjson_get_int(_var->val_));
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
}
|
||||
|
||||
rfl::Result<InputArrayType> to_array(InputVarType* _var) const noexcept {
|
||||
if (!yyjson_is_arr(_var->val_)) {
|
||||
return rfl::Error("Could not cast to array!");
|
||||
}
|
||||
return InputArrayType(_var->val_);
|
||||
}
|
||||
|
||||
std::map<std::string, InputVarType> to_map(
|
||||
InputObjectType* _obj) const noexcept {
|
||||
std::map<std::string, InputVarType> m;
|
||||
yyjson_obj_iter iter;
|
||||
yyjson_obj_iter_init(_obj->val_, &iter);
|
||||
yyjson_val* key;
|
||||
while ((key = yyjson_obj_iter_next(&iter))) {
|
||||
const auto p =
|
||||
std::make_pair(yyjson_get_str(key),
|
||||
InputVarType(yyjson_obj_iter_get_val(key)));
|
||||
m.emplace(std::move(p));
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
rfl::Result<InputObjectType> to_object(InputVarType* _var) const noexcept {
|
||||
if (!yyjson_is_obj(_var->val_)) {
|
||||
return rfl::Error("Could not cast to object!");
|
||||
}
|
||||
return InputObjectType(_var->val_);
|
||||
}
|
||||
|
||||
std::vector<InputVarType> to_vec(InputArrayType* _arr) const noexcept {
|
||||
std::vector<InputVarType> vec;
|
||||
yyjson_val* val;
|
||||
yyjson_arr_iter iter;
|
||||
yyjson_arr_iter_init(_arr->val_, &iter);
|
||||
while ((val = yyjson_arr_iter_next(&iter))) {
|
||||
vec.push_back(InputVarType(val));
|
||||
}
|
||||
return vec;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> use_custom_constructor(InputVarType* _var) const noexcept {
|
||||
try {
|
||||
return T::from_json_obj(*_var);
|
||||
} catch (std::exception& e) {
|
||||
return rfl::Error(e.what());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace json
|
||||
} // namespace rfl
|
||||
|
||||
#endif // JSON_PARSER_HPP_
|
||||
@@ -0,0 +1,93 @@
|
||||
#ifndef RFL_JSON_WRITER_HPP_
|
||||
#define RFL_JSON_WRITER_HPP_
|
||||
|
||||
#include <yyjson.h>
|
||||
|
||||
#include <exception>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#include "rfl/Result.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace json {
|
||||
|
||||
struct Writer {
|
||||
struct YYJSONOutputArray {
|
||||
yyjson_mut_val* val_;
|
||||
};
|
||||
|
||||
struct YYJSONOutputObject {
|
||||
yyjson_mut_val* val_;
|
||||
};
|
||||
|
||||
struct YYJSONOutputVar {
|
||||
YYJSONOutputVar(yyjson_mut_val* _val) : val_(_val) {}
|
||||
|
||||
YYJSONOutputVar(YYJSONOutputArray _arr) : val_(_arr.val_) {}
|
||||
|
||||
YYJSONOutputVar(YYJSONOutputObject _obj) : val_(_obj.val_) {}
|
||||
|
||||
yyjson_mut_val* val_;
|
||||
};
|
||||
|
||||
using OutputArrayType = YYJSONOutputArray;
|
||||
using OutputObjectType = YYJSONOutputObject;
|
||||
using OutputVarType = YYJSONOutputVar;
|
||||
|
||||
Writer(yyjson_mut_doc* _doc) : doc_(_doc) {}
|
||||
|
||||
~Writer() = default;
|
||||
|
||||
void add(const OutputVarType _var, OutputArrayType* _arr) const noexcept {
|
||||
yyjson_mut_arr_add_val(_arr->val_, _var.val_);
|
||||
}
|
||||
|
||||
OutputVarType empty_var() const noexcept {
|
||||
return OutputVarType(yyjson_mut_null(doc_));
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType from_basic_type(const T _var) const noexcept {
|
||||
if constexpr (std::is_same<std::decay_t<T>, std::string>()) {
|
||||
return OutputVarType(yyjson_mut_strcpy(doc_, _var.c_str()));
|
||||
} else if constexpr (std::is_same<std::decay_t<T>, bool>()) {
|
||||
return OutputVarType(yyjson_mut_bool(doc_, _var));
|
||||
} else if constexpr (std::is_floating_point<std::decay_t<T>>()) {
|
||||
return OutputVarType(
|
||||
yyjson_mut_real(doc_, static_cast<double>(_var)));
|
||||
} else {
|
||||
return OutputVarType(yyjson_mut_int(doc_, static_cast<int>(_var)));
|
||||
}
|
||||
}
|
||||
|
||||
OutputArrayType new_array() const noexcept {
|
||||
return OutputArrayType(yyjson_mut_arr(doc_));
|
||||
}
|
||||
|
||||
OutputObjectType new_object() const noexcept {
|
||||
return OutputObjectType(yyjson_mut_obj(doc_));
|
||||
}
|
||||
|
||||
bool is_empty(OutputVarType* _var) const noexcept {
|
||||
return yyjson_mut_is_null(_var->val_);
|
||||
}
|
||||
|
||||
void set_field(const std::string& _name, const OutputVarType& _var,
|
||||
OutputObjectType* _obj) const noexcept {
|
||||
yyjson_mut_obj_add(_obj->val_, yyjson_mut_strcpy(doc_, _name.c_str()),
|
||||
_var.val_);
|
||||
}
|
||||
|
||||
yyjson_mut_doc* doc_;
|
||||
};
|
||||
|
||||
} // namespace json
|
||||
} // namespace rfl
|
||||
|
||||
#endif // JSON_PARSER_HPP_
|
||||
@@ -0,0 +1,31 @@
|
||||
#ifndef RFL_JSON_READ_HPP_
|
||||
#define RFL_JSON_READ_HPP_
|
||||
|
||||
#include <yyjson.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "rfl/json/Parser.hpp"
|
||||
#include "rfl/json/Reader.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace json {
|
||||
|
||||
using InputObjectType = typename Reader::InputObjectType;
|
||||
using InputVarType = typename Reader::InputVarType;
|
||||
|
||||
/// Parses an object from JSON using reflection.
|
||||
template <class T>
|
||||
Result<T> read(const std::string& _json_str) {
|
||||
yyjson_doc* doc = yyjson_read(_json_str.c_str(), _json_str.size(), 0);
|
||||
InputVarType root = InputVarType(yyjson_doc_get_root(doc));
|
||||
const auto r = Reader();
|
||||
const auto result = Parser<T>::read(r, &root);
|
||||
yyjson_doc_free(doc);
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace json
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,30 @@
|
||||
#ifndef RFL_JSON_WRITE_HPP_
|
||||
#define RFL_JSON_WRITE_HPP_
|
||||
|
||||
#include <yyjson.h>
|
||||
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include "rfl/json/Parser.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace json {
|
||||
|
||||
/// Parses an object to JSON.
|
||||
template <class T>
|
||||
std::string write(const T& _obj) {
|
||||
auto w = Writer(yyjson_mut_doc_new(NULL));
|
||||
const auto json_obj = Parser<T>::write(w, _obj);
|
||||
yyjson_mut_doc_set_root(w.doc_, json_obj.val_);
|
||||
const char* json_c_str = yyjson_mut_write(w.doc_, 0, NULL);
|
||||
const auto json_str = std::string(json_c_str);
|
||||
free((void*)json_c_str);
|
||||
yyjson_mut_doc_free(w.doc_);
|
||||
return json_str;
|
||||
}
|
||||
|
||||
} // namespace json
|
||||
} // namespace rfl
|
||||
|
||||
#endif // JSON_PARSER_HPP_
|
||||
@@ -0,0 +1,27 @@
|
||||
#ifndef RFL_MAKENAMEDTUPLE_HPP_
|
||||
#define RFL_MAKENAMEDTUPLE_HPP_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "rfl/NamedTuple.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// Convenience constructor that doesn't require you
|
||||
/// to explitly defined the field types.
|
||||
template <class... FieldTypes>
|
||||
auto make_named_tuple(FieldTypes&&... _args) {
|
||||
return NamedTuple<std::decay_t<FieldTypes>...>(
|
||||
std::forward<FieldTypes>(_args)...);
|
||||
}
|
||||
|
||||
/// Convenience constructor that doesn't require you
|
||||
/// to explitly defined the field types.
|
||||
template <class Head, class... Tail>
|
||||
auto make_named_tuple(const Head& _h, const Tail&... _t) {
|
||||
return NamedTuple<Head, Tail...>(_h, _t...);
|
||||
}
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif // RFL_MAKENAMEDTUPLE_HPP_
|
||||
@@ -0,0 +1,14 @@
|
||||
#ifndef RFL_NAME_T_HPP_
|
||||
#define RFL_NAME_T_HPP_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// Convenience class to retrieve the name of a field.
|
||||
template <class FieldType>
|
||||
using name_t = typename std::decay_t<FieldType>::Name;
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,22 @@
|
||||
#ifndef RFL_NAMED_TUPLE_T_HPP_
|
||||
#define RFL_NAMED_TUPLE_T_HPP_
|
||||
|
||||
#include <functional>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
|
||||
#include "rfl/internal/is_named_tuple.hpp"
|
||||
#include "rfl/to_named_tuple.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// Generates the named tuple that is equivalent to the struct T.
|
||||
/// This is the result you would expect from calling to_named_tuple(my_struct).
|
||||
/// All fields of the struct must be an rfl::Field.
|
||||
template <class T>
|
||||
using named_tuple_t =
|
||||
typename std::invoke_result<decltype(to_named_tuple<T>), T>::type;
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,879 @@
|
||||
#ifndef RFL_PARSING_PARSER_HPP_
|
||||
#define RFL_PARSING_PARSER_HPP_
|
||||
|
||||
#include <cstddef>
|
||||
#include <deque>
|
||||
#include <exception>
|
||||
#include <forward_list>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <set>
|
||||
#include <stdexcept>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#include "rfl/Box.hpp"
|
||||
#include "rfl/Enum.hpp"
|
||||
#include "rfl/Literal.hpp"
|
||||
#include "rfl/NamedTuple.hpp"
|
||||
#include "rfl/Ref.hpp"
|
||||
#include "rfl/Result.hpp"
|
||||
#include "rfl/TaggedUnion.hpp"
|
||||
#include "rfl/field_type.hpp"
|
||||
#include "rfl/from_named_tuple.hpp"
|
||||
#include "rfl/internal/StringLiteral.hpp"
|
||||
#include "rfl/internal/has_reflection_method_v.hpp"
|
||||
#include "rfl/internal/has_reflection_type_v.hpp"
|
||||
#include "rfl/named_tuple_t.hpp"
|
||||
#include "rfl/parsing/is_required.hpp"
|
||||
#include "rfl/to_named_tuple.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
template <class ReaderType, class WriterType, class T>
|
||||
struct Parser;
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/// Default case - anything that cannot be explicitly matched.
|
||||
template <class ReaderType, class WriterType, class T>
|
||||
struct Parser {
|
||||
using InputVarType = typename ReaderType::InputVarType;
|
||||
using OutputVarType = typename WriterType::OutputVarType;
|
||||
|
||||
/// Expresses the variables as type T.
|
||||
static Result<T> read(const ReaderType& _r, InputVarType* _var) noexcept {
|
||||
if constexpr (ReaderType::template has_custom_constructor<T>) {
|
||||
return _r.template use_custom_constructor<T>(_var);
|
||||
} else {
|
||||
if constexpr (internal::has_reflection_type_v<T>) {
|
||||
using ReflectionType = std::decay_t<typename T::ReflectionType>;
|
||||
const auto wrap_in_t = [](auto _named_tuple) {
|
||||
return T(_named_tuple);
|
||||
};
|
||||
return Parser<ReaderType, WriterType, ReflectionType>::read(
|
||||
_r, _var)
|
||||
.transform(wrap_in_t);
|
||||
} else if constexpr (std::is_class_v<T> &&
|
||||
!std::is_same<T, std::string>()) {
|
||||
using NamedTupleType = named_tuple_t<T>;
|
||||
const auto to_struct = [](NamedTupleType&& _n) {
|
||||
return from_named_tuple<T, NamedTupleType>(
|
||||
std::forward<NamedTupleType>(_n));
|
||||
};
|
||||
return Parser<ReaderType, WriterType, NamedTupleType>::read(
|
||||
_r, _var)
|
||||
.transform(to_struct);
|
||||
} else {
|
||||
return _r.template to_basic_type<std::decay_t<T>>(_var);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts the variable to a JSON type.
|
||||
static auto write(const WriterType& _w, const T& _var) noexcept {
|
||||
if constexpr (internal::has_reflection_type_v<T>) {
|
||||
using ReflectionType = std::decay_t<typename T::ReflectionType>;
|
||||
if constexpr (internal::has_reflection_method_v<T>) {
|
||||
return Parser<ReaderType, WriterType, ReflectionType>::write(
|
||||
_w, _var.reflection());
|
||||
} else {
|
||||
const auto& [r] = _var;
|
||||
return Parser<ReaderType, WriterType, ReflectionType>::write(_w,
|
||||
r);
|
||||
}
|
||||
} else if constexpr (std::is_class_v<T> &&
|
||||
!std::is_same<T, std::string>()) {
|
||||
const auto named_tuple = to_named_tuple(_var);
|
||||
using NamedTupleType = std::decay_t<decltype(named_tuple)>;
|
||||
return Parser<ReaderType, WriterType, NamedTupleType>::write(
|
||||
_w, named_tuple);
|
||||
} else {
|
||||
return _w.from_basic_type(_var);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
template <class ReaderType, class WriterType, class T>
|
||||
struct Parser<ReaderType, WriterType, Box<T>> {
|
||||
using InputVarType = typename ReaderType::InputVarType;
|
||||
using OutputVarType = typename WriterType::OutputVarType;
|
||||
|
||||
/// Expresses the variables as type T.
|
||||
static Result<Box<T>> read(const ReaderType& _r,
|
||||
InputVarType* _var) noexcept {
|
||||
const auto to_box = [&](auto&& _t) { return Box<T>::make(_t); };
|
||||
return Parser<ReaderType, WriterType, std::decay_t<T>>::read(_r, _var)
|
||||
.transform(to_box);
|
||||
}
|
||||
|
||||
/// Expresses the variable a a JSON.
|
||||
static OutputVarType write(const WriterType& _w,
|
||||
const Box<T>& _box) noexcept {
|
||||
return Parser<ReaderType, WriterType, std::decay_t<T>>::write(_w,
|
||||
*_box);
|
||||
}
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
template <class ReaderType, class WriterType, class... FieldTypes>
|
||||
struct Parser<ReaderType, WriterType, Enum<FieldTypes...>> {
|
||||
using ResultType = Result<Enum<FieldTypes...>>;
|
||||
|
||||
public:
|
||||
using InputObjectType = typename ReaderType::InputObjectType;
|
||||
using InputVarType = typename ReaderType::InputVarType;
|
||||
|
||||
using OutputObjectType = typename WriterType::OutputObjectType;
|
||||
using OutputVarType = typename WriterType::OutputVarType;
|
||||
|
||||
/// Expresses the variables as type T.
|
||||
static ResultType read(const ReaderType& _r, InputVarType* _var) noexcept {
|
||||
const auto to_map = [&](auto _obj) { return _r.to_map(&_obj); };
|
||||
|
||||
const auto to_result = [&](auto _map) -> ResultType {
|
||||
if (_map.size() != 1) {
|
||||
return Error(
|
||||
"Could not parse Enum: Expected the object to have exactly "
|
||||
"one "
|
||||
"field, but found " +
|
||||
std::to_string(_map.size()) + " fields.");
|
||||
}
|
||||
const auto it = _map.begin();
|
||||
const auto& disc_value = it->first;
|
||||
auto& var = it->second;
|
||||
return find_matching_alternative(_r, disc_value, &var);
|
||||
};
|
||||
|
||||
return _r.to_object(_var).transform(to_map).and_then(to_result);
|
||||
}
|
||||
|
||||
/// Expresses the variables as a JSON type.
|
||||
static OutputVarType write(const WriterType& _w,
|
||||
const Enum<FieldTypes...>& _enum) noexcept {
|
||||
const auto handle = [&](const auto& _field) {
|
||||
using NamedTupleType = NamedTuple<std::decay_t<decltype(_field)>>;
|
||||
return Parser<ReaderType, WriterType, NamedTupleType>::write(
|
||||
_w, NamedTupleType(_field));
|
||||
};
|
||||
return std::visit(handle, _enum.variant_);
|
||||
}
|
||||
|
||||
private:
|
||||
template <int _i = 0>
|
||||
static ResultType find_matching_alternative(const ReaderType& _r,
|
||||
const std::string& _disc_value,
|
||||
InputVarType* _var) noexcept {
|
||||
if constexpr (_i == sizeof...(FieldTypes)) {
|
||||
return Error("Could not parse enum, could not match field named '" +
|
||||
_disc_value + "'.");
|
||||
} else {
|
||||
const auto optional = try_option<_i>(_r, _disc_value, _var);
|
||||
if (optional) {
|
||||
return *optional;
|
||||
} else {
|
||||
return find_matching_alternative<_i + 1>(_r, _disc_value, _var);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Tries to parse one particular option.
|
||||
template <int _i>
|
||||
static std::optional<ResultType> try_option(const ReaderType& _r,
|
||||
const std::string& _disc_value,
|
||||
InputVarType* _var) noexcept {
|
||||
using FieldType = std::decay_t<
|
||||
typename std::tuple_element<_i, std::tuple<FieldTypes...>>::type>;
|
||||
|
||||
using ValueType = std::decay_t<typename FieldType::Type>;
|
||||
|
||||
const auto key = FieldType::name_.str();
|
||||
|
||||
if (key == _disc_value) {
|
||||
const auto to_enum = [](ValueType&& _val) {
|
||||
return Enum<FieldTypes...>(
|
||||
FieldType(std::forward<ValueType>(_val)));
|
||||
};
|
||||
|
||||
const auto embellish_error = [&](const Error& _e) {
|
||||
return Error("Could not parse enum with field '" + _disc_value +
|
||||
"': " + _e.what());
|
||||
};
|
||||
|
||||
return Parser<ReaderType, WriterType, ValueType>::read(_r, _var)
|
||||
.transform(to_enum)
|
||||
.or_else(embellish_error);
|
||||
|
||||
} else {
|
||||
return std::optional<ResultType>();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
template <class ReaderType, class WriterType,
|
||||
internal::StringLiteral... _fields>
|
||||
struct Parser<ReaderType, WriterType, Literal<_fields...>> {
|
||||
using InputVarType = typename ReaderType::InputVarType;
|
||||
using OutputVarType = typename WriterType::OutputVarType;
|
||||
|
||||
/// Expresses the variables as type T.
|
||||
static Result<Literal<_fields...>> read(const ReaderType& _r,
|
||||
InputVarType* _var) noexcept {
|
||||
const auto to_type = [](const auto& _str) {
|
||||
return Literal<_fields...>::from_string(_str);
|
||||
};
|
||||
|
||||
const auto embellish_error = [](const Error& _e) {
|
||||
return Error(std::string("Failed to parse Literal: ") + _e.what());
|
||||
};
|
||||
|
||||
return _r.template to_basic_type<std::string>(_var)
|
||||
.and_then(to_type)
|
||||
.or_else(embellish_error);
|
||||
}
|
||||
|
||||
/// Expresses the variable a a JSON.
|
||||
static OutputVarType write(const WriterType& _w,
|
||||
const Literal<_fields...>& _literal) noexcept {
|
||||
return _w.from_basic_type(_literal.name());
|
||||
}
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
template <class ReaderType, class WriterType, class ValueType>
|
||||
struct Parser<ReaderType, WriterType, std::map<std::string, ValueType>> {
|
||||
public:
|
||||
using InputObjectType = typename ReaderType::InputObjectType;
|
||||
using InputVarType = typename ReaderType::InputVarType;
|
||||
|
||||
using OutputObjectType = typename WriterType::OutputObjectType;
|
||||
using OutputVarType = typename WriterType::OutputVarType;
|
||||
|
||||
/// Expresses the variables as a std::map.
|
||||
static Result<std::map<std::string, ValueType>> read(
|
||||
const ReaderType& _r, InputVarType* _var) noexcept {
|
||||
const auto get_pair = [&](auto _pair) {
|
||||
const auto to_pair = [&](const ValueType& _val) {
|
||||
return std::make_pair(_pair.first, _val);
|
||||
};
|
||||
return Parser<ReaderType, WriterType,
|
||||
std::decay_t<ValueType>>::read(_r, &_pair.second)
|
||||
.transform(to_pair)
|
||||
.value();
|
||||
};
|
||||
|
||||
const auto to_map = [&_r, get_pair](auto _obj) {
|
||||
using namespace std::ranges::views;
|
||||
auto m = _r.to_map(&_obj);
|
||||
auto range = m | transform(get_pair);
|
||||
try {
|
||||
return std::map<std::string, ValueType>(range.begin(),
|
||||
range.end());
|
||||
} catch (std::exception& e) {
|
||||
return Error(e.what());
|
||||
}
|
||||
};
|
||||
|
||||
return _r.to_object(_var).and_then(to_map);
|
||||
}
|
||||
|
||||
/// Transform a std::map into an object
|
||||
static OutputVarType write(
|
||||
const WriterType& _w,
|
||||
const std::map<std::string, ValueType>& _m) noexcept {
|
||||
auto obj = _w.new_object();
|
||||
for (const auto& [k, v] : _m) {
|
||||
_w.set_field(
|
||||
k,
|
||||
Parser<ReaderType, WriterType, std::decay_t<ValueType>>::write(
|
||||
_w, v),
|
||||
&obj);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
template <class ReaderType, class WriterType, class... FieldTypes>
|
||||
struct Parser<ReaderType, WriterType, NamedTuple<FieldTypes...>> {
|
||||
using InputObjectType = typename ReaderType::InputObjectType;
|
||||
using InputVarType = typename ReaderType::InputVarType;
|
||||
|
||||
using OutputObjectType = typename WriterType::OutputObjectType;
|
||||
using OutputVarType = typename WriterType::OutputVarType;
|
||||
|
||||
public:
|
||||
/// Generates a NamedTuple from a JSON Object.
|
||||
static Result<NamedTuple<FieldTypes...>> read(const ReaderType& _r,
|
||||
InputVarType* _var) noexcept {
|
||||
const auto to_map = [&](auto _obj) { return _r.to_map(&_obj); };
|
||||
const auto build = [&](const auto& _map) {
|
||||
return build_named_tuple_recursively(_r, _map);
|
||||
};
|
||||
return _r.to_object(_var).transform(to_map).and_then(build);
|
||||
}
|
||||
|
||||
/// Transforms a NamedTuple into a JSON object.
|
||||
static OutputVarType write(const WriterType& _w,
|
||||
const NamedTuple<FieldTypes...>& _tup) noexcept {
|
||||
auto obj = _w.new_object();
|
||||
build_object_recursively(_w, _tup, &obj);
|
||||
return OutputVarType(obj);
|
||||
}
|
||||
|
||||
private:
|
||||
/// Builds the named tuple field by field.
|
||||
template <class... Args>
|
||||
static Result<NamedTuple<FieldTypes...>> build_named_tuple_recursively(
|
||||
const ReaderType& _r, const std::map<std::string, InputVarType>& _map,
|
||||
Args&&... _args) noexcept {
|
||||
const auto size = sizeof...(Args);
|
||||
|
||||
if constexpr (size == sizeof...(FieldTypes)) {
|
||||
return NamedTuple<FieldTypes...>(_args...);
|
||||
} else {
|
||||
using FieldType = typename std::tuple_element<
|
||||
size, typename NamedTuple<FieldTypes...>::Fields>::type;
|
||||
|
||||
using ValueType = std::decay_t<typename FieldType::Type>;
|
||||
|
||||
const auto key = FieldType::name_.str();
|
||||
|
||||
const auto it = _map.find(key);
|
||||
|
||||
if (it == _map.end()) {
|
||||
if constexpr (is_required<ValueType>()) {
|
||||
return Error("Field named '" + key + "' not found!");
|
||||
} else {
|
||||
return build_named_tuple_recursively(
|
||||
_r, _map, _args..., FieldType(ValueType()));
|
||||
}
|
||||
}
|
||||
|
||||
const auto build = [&](auto&& _value) {
|
||||
return build_named_tuple_recursively(_r, _map, _args...,
|
||||
FieldType(_value));
|
||||
};
|
||||
|
||||
return get_value<ValueType>(_r, *it).and_then(build);
|
||||
}
|
||||
}
|
||||
|
||||
/// Builds the object field by field.
|
||||
template <int _i = 0>
|
||||
static void build_object_recursively(const WriterType& _w,
|
||||
const NamedTuple<FieldTypes...>& _tup,
|
||||
OutputObjectType* _ptr) noexcept {
|
||||
if constexpr (_i >= sizeof...(FieldTypes)) {
|
||||
return;
|
||||
} else {
|
||||
using FieldType =
|
||||
typename std::tuple_element<_i,
|
||||
std::tuple<FieldTypes...>>::type;
|
||||
using ValueType = std::decay_t<typename FieldType::Type>;
|
||||
auto value = Parser<ReaderType, WriterType, ValueType>::write(
|
||||
_w, rfl::get<_i>(_tup));
|
||||
const auto name = FieldType::name_.str();
|
||||
if constexpr (!is_required<ValueType>()) {
|
||||
if (!_w.is_empty(&value)) {
|
||||
_w.set_field(name, value, _ptr);
|
||||
}
|
||||
} else {
|
||||
_w.set_field(name, value, _ptr);
|
||||
}
|
||||
return build_object_recursively<_i + 1>(_w, _tup, _ptr);
|
||||
}
|
||||
}
|
||||
|
||||
/// Retrieves the value from the object. This is mainly needed to generate a
|
||||
/// better error message.
|
||||
template <class ValueType>
|
||||
static auto get_value(const ReaderType& _r,
|
||||
std::pair<std::string, InputVarType> _pair) noexcept {
|
||||
const auto embellish_error = [&](const Error& _e) {
|
||||
return Error("Failed to parse field '" + _pair.first +
|
||||
"': " + _e.what());
|
||||
};
|
||||
return Parser<ReaderType, WriterType, ValueType>::read(_r,
|
||||
&_pair.second)
|
||||
.or_else(embellish_error);
|
||||
}
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
template <class ReaderType, class WriterType, class T>
|
||||
struct Parser<ReaderType, WriterType, std::optional<T>> {
|
||||
using InputVarType = typename ReaderType::InputVarType;
|
||||
using OutputVarType = typename WriterType::OutputVarType;
|
||||
|
||||
/// Expresses the variables as type T.
|
||||
static Result<std::optional<T>> read(const ReaderType& _r,
|
||||
InputVarType* _var) noexcept {
|
||||
if (_r.is_empty(_var)) {
|
||||
return std::optional<T>();
|
||||
}
|
||||
const auto to_opt = [&_r](auto&& _t) {
|
||||
return std::make_optional<T>(_t);
|
||||
};
|
||||
return Parser<ReaderType, WriterType, std::decay_t<T>>::read(_r, _var)
|
||||
.transform(to_opt);
|
||||
}
|
||||
|
||||
/// Expresses the variable a a JSON.
|
||||
static OutputVarType write(const WriterType& _w,
|
||||
const std::optional<T>& _o) noexcept {
|
||||
if (!_o) {
|
||||
return _w.empty_var();
|
||||
}
|
||||
return Parser<ReaderType, WriterType, std::decay_t<T>>::write(_w, *_o);
|
||||
}
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
template <class ReaderType, class WriterType, class T>
|
||||
struct Parser<ReaderType, WriterType, Ref<T>> {
|
||||
using InputVarType = typename ReaderType::InputVarType;
|
||||
using OutputVarType = typename WriterType::OutputVarType;
|
||||
|
||||
/// Expresses the variables as type T.
|
||||
static Result<Ref<T>> read(const ReaderType& _r,
|
||||
InputVarType* _var) noexcept {
|
||||
const auto to_ref = [&](auto&& _t) { return Ref<T>::make(_t); };
|
||||
return Parser<ReaderType, WriterType, std::decay_t<T>>::read(_r, _var)
|
||||
.transform(to_ref);
|
||||
}
|
||||
|
||||
/// Expresses the variable a a JSON.
|
||||
static OutputVarType write(const WriterType& _w,
|
||||
const Ref<T>& _ref) noexcept {
|
||||
return Parser<ReaderType, WriterType, std::decay_t<T>>::write(_w,
|
||||
*_ref);
|
||||
}
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
template <class ReaderType, class WriterType, class T>
|
||||
struct Parser<ReaderType, WriterType, std::shared_ptr<T>> {
|
||||
using InputVarType = typename ReaderType::InputVarType;
|
||||
using OutputVarType = typename WriterType::OutputVarType;
|
||||
|
||||
/// Expresses the variables as type T.
|
||||
static Result<std::shared_ptr<T>> read(const ReaderType& _r,
|
||||
InputVarType* _var) noexcept {
|
||||
if (_r.is_empty(_var)) {
|
||||
return std::shared_ptr<T>();
|
||||
}
|
||||
const auto to_ptr = [](auto&& _t) { return std::make_shared<T>(_t); };
|
||||
return Parser<ReaderType, WriterType, std::decay_t<T>>::read(_r, _var)
|
||||
.transform(to_ptr);
|
||||
}
|
||||
|
||||
/// Expresses the variable a a JSON.
|
||||
static OutputVarType write(const WriterType& _w,
|
||||
const std::shared_ptr<T>& _s) noexcept {
|
||||
if (!_s) {
|
||||
return _w.empty_var();
|
||||
}
|
||||
return Parser<ReaderType, WriterType, std::decay_t<T>>::write(_w, *_s);
|
||||
}
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
template <class ReaderType, class WriterType, class FirstType, class SecondType>
|
||||
struct Parser<ReaderType, WriterType, std::pair<FirstType, SecondType>> {
|
||||
using InputVarType = typename ReaderType::InputVarType;
|
||||
using OutputVarType = typename WriterType::OutputVarType;
|
||||
|
||||
/// Expresses the variables as type T.
|
||||
static Result<std::pair<FirstType, SecondType>> read(
|
||||
const ReaderType& _r, InputVarType* _var) noexcept {
|
||||
const auto to_pair = [&](auto&& _t) {
|
||||
return std::make_pair(std::move(std::get<0>(_t)),
|
||||
std::move(std::get<1>(_t)));
|
||||
};
|
||||
return Parser<ReaderType, WriterType,
|
||||
std::tuple<FirstType, SecondType>>::read(_r, _var)
|
||||
.transform(to_pair);
|
||||
}
|
||||
|
||||
/// Transform a std::pair into an array
|
||||
static OutputVarType write(
|
||||
const WriterType& _w,
|
||||
const std::pair<FirstType, SecondType>& _p) noexcept {
|
||||
const auto tup = std::make_tuple(_p.first, _p.second);
|
||||
return Parser<ReaderType, WriterType,
|
||||
std::tuple<FirstType, SecondType>>::write(_w, tup);
|
||||
}
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
template <class ReaderType, class WriterType,
|
||||
internal::StringLiteral _discriminator, class... AlternativeTypes>
|
||||
struct Parser<ReaderType, WriterType,
|
||||
TaggedUnion<_discriminator, AlternativeTypes...>> {
|
||||
using ResultType = Result<TaggedUnion<_discriminator, AlternativeTypes...>>;
|
||||
|
||||
public:
|
||||
using InputObjectType = typename ReaderType::InputObjectType;
|
||||
using InputVarType = typename ReaderType::InputVarType;
|
||||
|
||||
using OutputObjectType = typename WriterType::OutputObjectType;
|
||||
using OutputVarType = typename WriterType::OutputVarType;
|
||||
|
||||
/// Expresses the variables as type T.
|
||||
static ResultType read(const ReaderType& _r, InputVarType* _var) noexcept {
|
||||
const auto get_disc = [&_r](auto _obj) {
|
||||
return get_discriminator(_r, &_obj);
|
||||
};
|
||||
|
||||
const auto to_result = [&_r, _var](std::string _disc_value) {
|
||||
return find_matching_alternative(_r, _disc_value, _var);
|
||||
};
|
||||
|
||||
return _r.to_object(_var).and_then(get_disc).and_then(to_result);
|
||||
}
|
||||
|
||||
/// Expresses the variables as a JSON type.
|
||||
static OutputVarType write(
|
||||
const WriterType& _w,
|
||||
const TaggedUnion<_discriminator, AlternativeTypes...>&
|
||||
_tagged_union) noexcept {
|
||||
using VariantType =
|
||||
typename TaggedUnion<_discriminator,
|
||||
AlternativeTypes...>::VariantType;
|
||||
return Parser<ReaderType, WriterType, VariantType>::write(
|
||||
_w, _tagged_union.variant_);
|
||||
}
|
||||
|
||||
private:
|
||||
template <int _i = 0>
|
||||
static ResultType find_matching_alternative(const ReaderType& _r,
|
||||
const std::string& _disc_value,
|
||||
InputVarType* _var) noexcept {
|
||||
if constexpr (_i == sizeof...(AlternativeTypes)) {
|
||||
return Error("Could not parse tagged union, could not match " +
|
||||
_discriminator.str() + " '" + _disc_value + "'.");
|
||||
} else {
|
||||
const auto optional = try_option<_i>(_r, _disc_value, _var);
|
||||
if (optional) {
|
||||
return *optional;
|
||||
} else {
|
||||
return find_matching_alternative<_i + 1>(_r, _disc_value, _var);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Retrieves the discriminator.
|
||||
static Result<std::string> get_discriminator(
|
||||
const ReaderType& _r, InputObjectType* _obj) noexcept {
|
||||
const auto embellish_error = [](const auto& _err) {
|
||||
return Error(
|
||||
"Could not parse tagged union: Could not find field '" +
|
||||
_discriminator.str() + "' or type of field was not a string.");
|
||||
};
|
||||
|
||||
const auto to_type = [&_r](auto _var) {
|
||||
return _r.template to_basic_type<std::string>(&_var);
|
||||
};
|
||||
|
||||
return _r.get_field(_discriminator.str(), _obj)
|
||||
.and_then(to_type)
|
||||
.or_else(embellish_error);
|
||||
}
|
||||
|
||||
/// Determines whether the discriminating literal contains the value
|
||||
/// retrieved from the object.
|
||||
template <class T>
|
||||
static inline bool contains_disc_value(
|
||||
const std::string& _disc_value) noexcept {
|
||||
if constexpr (!internal::has_reflection_type_v<T>) {
|
||||
using LiteralType = field_type_t<_discriminator, T>;
|
||||
return LiteralType::contains(_disc_value);
|
||||
} else {
|
||||
using LiteralType =
|
||||
field_type_t<_discriminator, typename T::ReflectionType>;
|
||||
return LiteralType::contains(_disc_value);
|
||||
}
|
||||
}
|
||||
|
||||
/// Tries to parse one particular option.
|
||||
template <int _i>
|
||||
static std::optional<ResultType> try_option(const ReaderType& _r,
|
||||
const std::string& _disc_value,
|
||||
InputVarType* _var) noexcept {
|
||||
using AlternativeType = std::decay_t<
|
||||
std::variant_alternative_t<_i, std::variant<AlternativeTypes...>>>;
|
||||
|
||||
if (contains_disc_value<AlternativeType>(_disc_value)) {
|
||||
const auto to_tagged_union = [](const auto& _val) {
|
||||
return TaggedUnion<_discriminator, AlternativeTypes...>(_val);
|
||||
};
|
||||
|
||||
const auto embellish_error = [&](const Error& _e) {
|
||||
return Error(
|
||||
"Could not parse tagged union with discrimininator " +
|
||||
_discriminator.str() + " '" + _disc_value +
|
||||
"': " + _e.what());
|
||||
};
|
||||
|
||||
return Parser<ReaderType, WriterType, AlternativeType>::read(_r,
|
||||
_var)
|
||||
.transform(to_tagged_union)
|
||||
.or_else(embellish_error);
|
||||
|
||||
} else {
|
||||
return std::optional<ResultType>();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
template <class ReaderType, class WriterType, class... Ts>
|
||||
struct Parser<ReaderType, WriterType, std::tuple<Ts...>> {
|
||||
public:
|
||||
using InputArrayType = typename ReaderType::InputArrayType;
|
||||
using InputVarType = typename ReaderType::InputVarType;
|
||||
|
||||
using OutputArrayType = typename WriterType::OutputArrayType;
|
||||
using OutputVarType = typename WriterType::OutputVarType;
|
||||
|
||||
static Result<std::tuple<Ts...>> read(const ReaderType& _r,
|
||||
InputVarType* _var) noexcept {
|
||||
const auto to_vec = [&](auto _arr) { return _r.to_vec(&_arr); };
|
||||
|
||||
const auto check_size =
|
||||
[](auto _vec) -> Result<std::vector<InputVarType>> {
|
||||
if (_vec.size() != sizeof...(Ts)) {
|
||||
return Error("Expected " + std::to_string(sizeof...(Ts)) +
|
||||
" fields, got " + std::to_string(_vec.size()) +
|
||||
".");
|
||||
}
|
||||
return std::move(_vec);
|
||||
};
|
||||
|
||||
const auto extract = [&_r](auto _vec) {
|
||||
return extract_field_by_field(_r, std::move(_vec));
|
||||
};
|
||||
|
||||
return _r.to_array(_var)
|
||||
.transform(to_vec)
|
||||
.and_then(check_size)
|
||||
.and_then(extract);
|
||||
}
|
||||
|
||||
static OutputVarType write(const WriterType& _w,
|
||||
const std::tuple<Ts...>& _tup) noexcept {
|
||||
auto arr = _w.new_array();
|
||||
to_array<0>(_w, _tup, &arr);
|
||||
return arr;
|
||||
}
|
||||
|
||||
private:
|
||||
/// Extracts values from the array, field by field.
|
||||
template <class... AlreadyExtracted>
|
||||
static Result<std::tuple<Ts...>> extract_field_by_field(
|
||||
const ReaderType& _r, std::vector<InputVarType> _vec,
|
||||
const AlreadyExtracted&... _already_extracted) noexcept {
|
||||
constexpr size_t i = sizeof...(AlreadyExtracted);
|
||||
if constexpr (i == sizeof...(Ts)) {
|
||||
return std::tuple<Ts...>(std::make_tuple(_already_extracted...));
|
||||
} else {
|
||||
const auto extract_next = [&](auto new_entry) {
|
||||
return extract_field_by_field(_r, std::move(_vec),
|
||||
_already_extracted..., new_entry);
|
||||
};
|
||||
return extract_single_field<i>(_r, &_vec).and_then(extract_next);
|
||||
}
|
||||
}
|
||||
|
||||
/// Extracts a single field from a JSON.
|
||||
template <int _i>
|
||||
static auto extract_single_field(const ReaderType& _r,
|
||||
std::vector<InputVarType>* _vec) noexcept {
|
||||
using NewFieldType = std::decay_t<
|
||||
typename std::tuple_element<_i, std::tuple<Ts...>>::type>;
|
||||
return Parser<ReaderType, WriterType, NewFieldType>::read(
|
||||
_r, &((*_vec)[_i]));
|
||||
}
|
||||
|
||||
/// Transforms a tuple to an array.
|
||||
template <int _i>
|
||||
static void to_array(const WriterType& _w, const std::tuple<Ts...>& _tup,
|
||||
OutputArrayType* _ptr) noexcept {
|
||||
if constexpr (_i < sizeof...(Ts)) {
|
||||
using NewFieldType = std::decay_t<
|
||||
typename std::tuple_element<_i, std::tuple<Ts...>>::type>;
|
||||
|
||||
const auto val =
|
||||
Parser<ReaderType, WriterType, NewFieldType>::write(
|
||||
_w, std::get<_i>(_tup));
|
||||
_w.add(val, _ptr);
|
||||
to_array<_i + 1>(_w, _tup, _ptr);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
template <class ReaderType, class WriterType, class... FieldTypes>
|
||||
struct Parser<ReaderType, WriterType, std::variant<FieldTypes...>> {
|
||||
using InputVarType = typename ReaderType::InputVarType;
|
||||
using OutputVarType = typename WriterType::OutputVarType;
|
||||
|
||||
/// Expresses the variables as type T.
|
||||
template <int _i = 0>
|
||||
static Result<std::variant<FieldTypes...>> read(
|
||||
const ReaderType& _r, InputVarType* _var,
|
||||
const std::string _errors = "") noexcept {
|
||||
if constexpr (_i == sizeof...(FieldTypes)) {
|
||||
return Error("Could not parse variant: " + _errors);
|
||||
} else {
|
||||
const auto to_variant = [](const auto& _val) {
|
||||
return std::variant<FieldTypes...>(_val);
|
||||
};
|
||||
|
||||
const auto try_next = [&_r, _var, &_errors](const auto& _err) {
|
||||
auto errors = _errors;
|
||||
errors += std::string("\n -") + _err.what();
|
||||
return read<_i + 1>(_r, _var, errors);
|
||||
};
|
||||
|
||||
using AltType = std::decay_t<
|
||||
std::variant_alternative_t<_i, std::variant<FieldTypes...>>>;
|
||||
|
||||
return Parser<ReaderType, WriterType, AltType>::read(_r, _var)
|
||||
.transform(to_variant)
|
||||
.or_else(try_next);
|
||||
}
|
||||
}
|
||||
|
||||
/// Expresses the variables as a JSON type.
|
||||
template <int _i = 0>
|
||||
static OutputVarType write(
|
||||
const WriterType& _w,
|
||||
const std::variant<FieldTypes...>& _variant) noexcept {
|
||||
using AltType =
|
||||
std::variant_alternative_t<_i, std::variant<FieldTypes...>>;
|
||||
if constexpr (_i + 1 == sizeof...(FieldTypes)) {
|
||||
return Parser<ReaderType, WriterType, std::decay_t<AltType>>::write(
|
||||
_w, std::get<AltType>(_variant));
|
||||
} else {
|
||||
if (std::holds_alternative<AltType>(_variant)) {
|
||||
return Parser<ReaderType, WriterType,
|
||||
std::decay_t<AltType>>::write(_w,
|
||||
std::get<AltType>(
|
||||
_variant));
|
||||
} else {
|
||||
return write<_i + 1>(_w, _variant);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/// This can be used for data structures that would be expressed as array in
|
||||
/// serialized format (std::vector, std::set, std::deque, ...).
|
||||
template <class ReaderType, class WriterType, class VecType>
|
||||
struct VectorParser {
|
||||
public:
|
||||
using InputArrayType = typename ReaderType::InputArrayType;
|
||||
using InputVarType = typename ReaderType::InputVarType;
|
||||
|
||||
using OutputArrayType = typename WriterType::OutputArrayType;
|
||||
using OutputVarType = typename WriterType::OutputVarType;
|
||||
|
||||
using T = typename VecType::value_type;
|
||||
|
||||
static Result<VecType> read(const ReaderType& _r,
|
||||
InputVarType* _var) noexcept {
|
||||
using namespace std::ranges::views;
|
||||
|
||||
const auto get_result = [&](auto& _v) {
|
||||
return Parser<ReaderType, WriterType, std::decay_t<T>>::read(_r,
|
||||
&_v)
|
||||
.value();
|
||||
};
|
||||
|
||||
const auto to_vec = [&](InputArrayType _arr) -> Result<VecType> {
|
||||
auto input_vars = _r.to_vec(&_arr);
|
||||
auto range = input_vars | transform(get_result);
|
||||
try {
|
||||
return Result<VecType>(VecType(range.begin(), range.end()));
|
||||
} catch (std::exception& e) {
|
||||
return Error(e.what());
|
||||
}
|
||||
};
|
||||
|
||||
return _r.to_array(_var).and_then(to_vec);
|
||||
}
|
||||
|
||||
static OutputVarType write(const WriterType& _w,
|
||||
const VecType& _vec) noexcept {
|
||||
auto arr = _w.new_array();
|
||||
for (auto it = _vec.begin(); it != _vec.end(); ++it) {
|
||||
_w.add(
|
||||
Parser<ReaderType, WriterType, std::decay_t<T>>::write(_w, *it),
|
||||
&arr);
|
||||
}
|
||||
return OutputVarType(arr);
|
||||
}
|
||||
};
|
||||
|
||||
template <class ReaderType, class WriterType, class T>
|
||||
struct Parser<ReaderType, WriterType, std::deque<T>>
|
||||
: public VectorParser<ReaderType, WriterType, std::deque<T>> {};
|
||||
|
||||
template <class ReaderType, class WriterType, class T>
|
||||
struct Parser<ReaderType, WriterType, std::forward_list<T>>
|
||||
: public VectorParser<ReaderType, WriterType, std::forward_list<T>> {};
|
||||
|
||||
template <class ReaderType, class WriterType, class T>
|
||||
struct Parser<ReaderType, WriterType, std::list<T>>
|
||||
: public VectorParser<ReaderType, WriterType, std::list<T>> {};
|
||||
|
||||
template <class ReaderType, class WriterType, class K, class V>
|
||||
struct Parser<ReaderType, WriterType, std::map<K, V>>
|
||||
: public VectorParser<ReaderType, WriterType, std::map<K, V>> {};
|
||||
|
||||
template <class ReaderType, class WriterType, class T>
|
||||
struct Parser<ReaderType, WriterType, std::set<T>>
|
||||
: public VectorParser<ReaderType, WriterType, std::set<T>> {};
|
||||
|
||||
template <class ReaderType, class WriterType, class K, class V>
|
||||
struct Parser<ReaderType, WriterType, std::unordered_map<K, V>>
|
||||
: public VectorParser<ReaderType, WriterType, std::unordered_map<K, V>> {};
|
||||
|
||||
template <class ReaderType, class WriterType, class T>
|
||||
struct Parser<ReaderType, WriterType, std::unordered_set<T>>
|
||||
: public VectorParser<ReaderType, WriterType, std::unordered_set<T>> {};
|
||||
|
||||
template <class ReaderType, class WriterType, class T>
|
||||
struct Parser<ReaderType, WriterType, std::vector<T>>
|
||||
: public VectorParser<ReaderType, WriterType, std::vector<T>> {};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
#endif // JSON_PARSER_HPP_
|
||||
@@ -0,0 +1,28 @@
|
||||
#ifndef RFL_PARSING_IS_REQUIRED_HPP_
|
||||
#define RFL_PARSING_IS_REQUIRED_HPP_
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <type_traits>
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
/// Determines whether a field in a named tuple is required.
|
||||
/// General case - most fields are required.
|
||||
template <class T>
|
||||
class is_required;
|
||||
|
||||
template <class T>
|
||||
class is_required : public std::true_type {};
|
||||
|
||||
template <class T>
|
||||
class is_required<std::optional<T>> : public std::false_type {};
|
||||
|
||||
template <class T>
|
||||
class is_required<std::shared_ptr<T>> : public std::false_type {};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,22 @@
|
||||
#ifndef RFL_REMOVEFIELDS_HPP_
|
||||
#define RFL_REMOVEFIELDS_HPP_
|
||||
|
||||
#include <algorithm>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
|
||||
#include "rfl/internal/StringLiteral.hpp"
|
||||
#include "rfl/internal/remove_fields.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// Recursively removes all of the fields signified by _names from the
|
||||
/// NamedTupleType.
|
||||
template <class NamedTupleType, internal::StringLiteral... _names>
|
||||
using remove_fields_t =
|
||||
typename internal::remove_fields<std::decay_t<NamedTupleType>,
|
||||
_names...>::type;
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif // RFL_REMOVEFIELDS_HPP_
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef RFL_REPLACE_HPP_
|
||||
#define RFL_REPLACE_HPP_
|
||||
|
||||
#include "rfl/from_named_tuple.hpp"
|
||||
#include "rfl/internal/is_named_tuple.hpp"
|
||||
#include "rfl/to_named_tuple.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// Replaces one or several fields, returning a new version
|
||||
/// with the non-replaced fields left unchanged.
|
||||
template <class T, class RField, class... OtherRFields>
|
||||
auto replace(const T& _t, RField&& _field, OtherRFields&&... _other_fields) {
|
||||
if constexpr (internal::is_named_tuple_v<T>) {
|
||||
return _t.replace(_field, _other_fields...);
|
||||
} else {
|
||||
return from_named_tuple<T>(
|
||||
to_named_tuple(_t).replace(_field, _other_fields...));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,64 @@
|
||||
#ifndef RFL_TO_NAMED_TUPLE_HPP_
|
||||
#define RFL_TO_NAMED_TUPLE_HPP_
|
||||
|
||||
#include <iostream>
|
||||
#include <tuple>
|
||||
|
||||
#include "rfl/always_false.hpp"
|
||||
#include "rfl/internal/has_flatten_fields.hpp"
|
||||
#include "rfl/internal/is_field.hpp"
|
||||
#include "rfl/internal/is_named_tuple.hpp"
|
||||
#include "rfl/internal/to_field_tuple.hpp"
|
||||
#include "rfl/make_named_tuple.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
template <class FieldTuple, class... Args>
|
||||
auto flatten_field_tuple(FieldTuple&& _t, Args&&... _args) {
|
||||
constexpr auto i = sizeof...(Args);
|
||||
if constexpr (i == std::tuple_size_v<std::decay_t<FieldTuple>>) {
|
||||
return std::tuple_cat(std::forward<Args>(_args)...);
|
||||
} else {
|
||||
using T = std::tuple_element_t<i, std::decay_t<FieldTuple>>;
|
||||
if constexpr (internal::is_flatten_field<T>::value) {
|
||||
return flatten_field_tuple(
|
||||
_t, std::forward<Args>(_args)...,
|
||||
flatten_field_tuple(internal::to_field_tuple(
|
||||
std::move(std::get<i>(_t).get()))));
|
||||
} else {
|
||||
return flatten_field_tuple(_t, std::forward<Args>(_args)...,
|
||||
std::make_tuple(std::get<i>(_t)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Generates the named tuple that is equivalent to the struct _t.
|
||||
/// If _t already is a named tuple, then _t will be returned.
|
||||
/// All fields of the struct must be an rfl::Field.
|
||||
template <class T>
|
||||
auto to_named_tuple(const T& _t) {
|
||||
if constexpr (internal::is_named_tuple_v<T>) {
|
||||
return _t;
|
||||
} else if constexpr (internal::is_field_v<T>) {
|
||||
return make_named_tuple(_t);
|
||||
} else {
|
||||
auto field_tuple = internal::to_field_tuple(_t);
|
||||
|
||||
using FieldTuple = std::decay_t<decltype(field_tuple)>;
|
||||
|
||||
const auto ft_to_nt = []<class... Fields>(Fields&&... _fields) {
|
||||
return make_named_tuple(std::forward<Fields>(_fields)...);
|
||||
};
|
||||
|
||||
if constexpr (!internal::has_flatten_fields<FieldTuple>()) {
|
||||
return std::apply(ft_to_nt, std::move(field_tuple));
|
||||
} else {
|
||||
auto flattened_tuple = flatten_field_tuple(std::move(field_tuple));
|
||||
return std::apply(ft_to_nt, std::move(flattened_tuple));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,41 @@
|
||||
#ifndef RFL_VISIT_HPP_
|
||||
#define RFL_VISIT_HPP_
|
||||
|
||||
#include <variant>
|
||||
|
||||
#include "rfl/Enum.hpp"
|
||||
#include "rfl/Literal.hpp"
|
||||
#include "rfl/TaggedUnion.hpp"
|
||||
#include "rfl/internal/StringLiteral.hpp"
|
||||
#include "rfl/internal/VisitTree.hpp"
|
||||
#include "rfl/internal/VisitorWrapper.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// Implements the visitor pattern for Enums.
|
||||
template <class Visitor, class... Args>
|
||||
inline auto visit(const Visitor& _visitor, const Enum<Args...>& _enum) {
|
||||
return std::visit(_visitor, _enum.variant_);
|
||||
}
|
||||
|
||||
/// Implements the visitor pattern for Literals.
|
||||
template <class Visitor, internal::StringLiteral... _fields, class... Args>
|
||||
inline auto visit(const Visitor& _visitor, const Literal<_fields...> _literal,
|
||||
const Args&... _args) {
|
||||
constexpr int size = sizeof...(_fields);
|
||||
using WrapperType = internal::VisitorWrapper<Visitor, _fields...>;
|
||||
const auto wrapper = WrapperType(&_visitor);
|
||||
return internal::VisitTree::visit<0, size, WrapperType>(
|
||||
wrapper, _literal.value(), _args...);
|
||||
}
|
||||
|
||||
/// Implements the visitor pattern for TaggedUnions.
|
||||
template <class Visitor, internal::StringLiteral _discriminator, class... Args>
|
||||
inline auto visit(const Visitor& _visitor,
|
||||
const TaggedUnion<_discriminator, Args...>& _tagged_union) {
|
||||
return std::visit(_visitor, _tagged_union.variant_);
|
||||
}
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif // RFL_VISIT_HPP_
|
||||
+7690
File diff suppressed because it is too large
Load Diff
+9201
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,17 @@
|
||||
cmake_minimum_required(VERSION 3.4)
|
||||
|
||||
project(reflect-cpp-tests)
|
||||
|
||||
include_directories(
|
||||
"../include"
|
||||
)
|
||||
|
||||
add_executable(
|
||||
reflect-cpp-tests
|
||||
"tests.cpp"
|
||||
"../src/yyjson.c"
|
||||
)
|
||||
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O2")
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20 -Wall -Wno-sign-compare -Wno-missing-braces -Wno-psabi -pthread -fno-strict-aliasing -fwrapv -O2")
|
||||
@@ -0,0 +1,57 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
// Makes sure that the example in the README works as expected.
|
||||
// It would be embarrassing if it didn't...
|
||||
void test_readme_example() {
|
||||
// "firstName", "lastName" and "children" are the field names
|
||||
// as they will appear in the JSON. The C++ standard is
|
||||
// snake case, the JSON standard is camel case, so the names
|
||||
// will not always be identical.
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name;
|
||||
rfl::Field<"children", std::vector<Person>> children;
|
||||
};
|
||||
|
||||
const auto bart = Person{.first_name = "Bart",
|
||||
.last_name = "Simpson",
|
||||
.children = std::vector<Person>()};
|
||||
|
||||
const auto lisa = Person{
|
||||
.first_name = "Lisa",
|
||||
.last_name = "Simpson",
|
||||
.children = rfl::default_value // same as std::vector<Person>()
|
||||
};
|
||||
|
||||
// Returns a deep copy of the original object,
|
||||
// replacing first_name.
|
||||
const auto maggie =
|
||||
rfl::replace(lisa, rfl::make_field<"firstName">(std::string("Maggie")));
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer",
|
||||
.last_name = "Simpson",
|
||||
.children = std::vector<Person>({bart, lisa, maggie})};
|
||||
|
||||
// We can now transform this into a JSON string.
|
||||
const std::string json_string = rfl::json::write(homer);
|
||||
|
||||
std::cout << json_string << std::endl;
|
||||
|
||||
// And we can parse the string to recreate the struct.
|
||||
auto homer2 = rfl::json::read<Person>(json_string).value();
|
||||
|
||||
// Fields can be accessed like this:
|
||||
std::cout << "Hello, my name is " << homer.first_name() << " "
|
||||
<< homer.last_name() << "." << std::endl;
|
||||
|
||||
// Since homer2 is mutable, we can also change the values like this:
|
||||
homer2.first_name = "Marge";
|
||||
|
||||
std::cout << "Hello, my name is " << homer2.first_name() << " "
|
||||
<< homer2.last_name() << "." << std::endl;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
#include "test_readme_example.hpp"
|
||||
|
||||
int main() {
|
||||
test_readme_example();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user