mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 18:56:19 +08:00
@@ -24,6 +24,7 @@
|
||||
#include "rfl/Ref.hpp"
|
||||
#include "rfl/Rename.hpp"
|
||||
#include "rfl/Size.hpp"
|
||||
#include "rfl/Skip.hpp"
|
||||
#include "rfl/SnakeCaseToCamelCase.hpp"
|
||||
#include "rfl/SnakeCaseToPascalCase.hpp"
|
||||
#include "rfl/TaggedUnion.hpp"
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef RFL_SKIP_HPP_
|
||||
#define RFL_SKIP_HPP_
|
||||
|
||||
#include "internal/Skip.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
template <class T>
|
||||
using Skip = internal::Skip<T, true, true>;
|
||||
|
||||
template <class T>
|
||||
using SkipSerialization = internal::Skip<T, true, false>;
|
||||
|
||||
template <class T>
|
||||
using SkipDeserialization = internal::Skip<T, false, true>;
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
#ifndef RFL_INTERNAL_SKIP_HPP_
|
||||
#define RFL_INTERNAL_SKIP_HPP_
|
||||
|
||||
#include <optional>
|
||||
|
||||
namespace rfl::internal {
|
||||
|
||||
template <class T, bool _skip_serialization, bool _skip_deserialization>
|
||||
class Skip {
|
||||
private:
|
||||
using SelfType = Skip<T, _skip_serialization, _skip_deserialization>;
|
||||
|
||||
public:
|
||||
static constexpr bool skip_serialization_ = _skip_serialization;
|
||||
static constexpr bool skip_deserialization_ = _skip_deserialization;
|
||||
|
||||
/// The underlying type.
|
||||
using Type = T;
|
||||
using ReflectionType = std::optional<T>;
|
||||
|
||||
Skip() : value_(Type()) {}
|
||||
|
||||
Skip(const Type& _value) : value_(_value) {}
|
||||
|
||||
Skip(ReflectionType&& _value) noexcept
|
||||
: value_(_value ? std::move(*_value) : Type()) {}
|
||||
|
||||
Skip(const ReflectionType& _value) : value_(_value ? *_value : Type()) {}
|
||||
|
||||
Skip(Type&& _value) noexcept : value_(std::move(_value)) {}
|
||||
|
||||
Skip(SelfType&& _skip) noexcept = default;
|
||||
|
||||
Skip(const SelfType& _skip) = default;
|
||||
|
||||
template <class U, bool _skip_s, bool _skip_d>
|
||||
Skip(const Skip<U, _skip_s, _skip_d>& _other) : value_(_other.get()) {}
|
||||
|
||||
template <class U, bool _skip_s, bool _skip_d>
|
||||
Skip(Skip<U, _skip_s, _skip_d>&& _other) : value_(_other.get()) {}
|
||||
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>,
|
||||
bool>::type = true>
|
||||
Skip(const U& _value) : value_(_value) {}
|
||||
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>,
|
||||
bool>::type = true>
|
||||
Skip(U&& _value) noexcept : value_(std::forward<U>(_value)) {}
|
||||
|
||||
template <class U, bool _skip_s, bool _skip_d,
|
||||
typename std::enable_if<std::is_convertible_v<U, Type>,
|
||||
bool>::type = true>
|
||||
Skip(const Skip<U, _skip_s, _skip_d>& _skip) : value_(_skip.value()) {}
|
||||
|
||||
/// Assigns the underlying object to its default value.
|
||||
template <class U = Type,
|
||||
typename std::enable_if<std::is_default_constructible_v<U>,
|
||||
bool>::type = true>
|
||||
Skip(const Default& _default) : value_(Type()) {}
|
||||
|
||||
~Skip() = default;
|
||||
|
||||
/// Returns the underlying object.
|
||||
Type& get() { return value_; }
|
||||
|
||||
/// Returns the underlying object.
|
||||
const Type& get() const { return value_; }
|
||||
|
||||
/// Returns the underlying object.
|
||||
Type& operator()() { return value_; }
|
||||
|
||||
/// Returns the underlying object.
|
||||
const Type& operator()() const { return value_; }
|
||||
|
||||
/// Assigns the underlying object.
|
||||
auto& operator=(const Type& _value) {
|
||||
value_ = _value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
auto& operator=(Type&& _value) noexcept {
|
||||
value_ = std::move(_value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>,
|
||||
bool>::type = true>
|
||||
auto& operator=(const U& _value) {
|
||||
value_ = _value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object to its default value.
|
||||
template <class U = Type,
|
||||
typename std::enable_if<std::is_default_constructible_v<U>,
|
||||
bool>::type = true>
|
||||
auto& operator=(const Default& _default) {
|
||||
value_ = Type();
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
SelfType& operator=(const SelfType& _other) = default;
|
||||
|
||||
/// Assigns the underlying object.
|
||||
SelfType& operator=(SelfType&& _other) = default;
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class U, bool _skip_s, bool _skip_d>
|
||||
auto& operator=(const Skip<U, _skip_s, _skip_d>& _skip) {
|
||||
value_ = _skip.get();
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class U, bool _skip_s, bool _skip_d>
|
||||
auto& operator=(Skip<U, _skip_s, _skip_d>&& _skip) {
|
||||
value_ = std::forward<T>(_skip.value_);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Returns the ReflectionType - necessary for the serialization to work.
|
||||
ReflectionType reflection() const { return value_; }
|
||||
|
||||
/// Assigns the underlying object.
|
||||
void set(const Type& _value) { value_ = _value; }
|
||||
|
||||
/// Assigns the underlying object.
|
||||
void set(Type&& _value) { value_ = std::move(_value); }
|
||||
|
||||
/// Returns the underlying object.
|
||||
Type& value() { return value_; }
|
||||
|
||||
/// Returns the underlying object.
|
||||
const Type& value() const { return value_; }
|
||||
|
||||
private:
|
||||
/// The underlying value
|
||||
T value_;
|
||||
};
|
||||
|
||||
} // namespace rfl::internal
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,26 @@
|
||||
#ifndef RFL_INTERNAL_ISSKIP_HPP_
|
||||
#define RFL_INTERNAL_ISSKIP_HPP_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "Skip.hpp"
|
||||
|
||||
namespace rfl::internal {
|
||||
|
||||
template <class T>
|
||||
class is_skip;
|
||||
|
||||
template <class T>
|
||||
class is_skip : public std::false_type {};
|
||||
|
||||
template <class T, bool _skip_serialization, bool _skip_deserialization>
|
||||
class is_skip<Skip<T, _skip_serialization, _skip_deserialization>>
|
||||
: public std::true_type {};
|
||||
|
||||
template <class T>
|
||||
constexpr bool is_skip_v =
|
||||
is_skip<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
|
||||
|
||||
} // namespace rfl::internal
|
||||
|
||||
#endif
|
||||
@@ -14,6 +14,7 @@
|
||||
#include "../internal/is_array.hpp"
|
||||
#include "../internal/is_attribute.hpp"
|
||||
#include "../internal/is_basic_type.hpp"
|
||||
#include "../internal/is_skip.hpp"
|
||||
#include "../internal/strings/replace_all.hpp"
|
||||
#include "../to_view.hpp"
|
||||
#include "AreReaderAndWriter.hpp"
|
||||
@@ -99,9 +100,11 @@ struct NamedTupleParser {
|
||||
} else {
|
||||
using F =
|
||||
std::tuple_element_t<_i, typename NamedTuple<FieldTypes...>::Fields>;
|
||||
_values[std::string(F::name())] =
|
||||
Parser<R, W, typename F::Type, ProcessorsType>::to_schema(
|
||||
_definitions);
|
||||
using U = typename F::Type;
|
||||
if constexpr (!internal::is_skip_v<U>) {
|
||||
_values[std::string(F::name())] =
|
||||
Parser<R, W, U, ProcessorsType>::to_schema(_definitions);
|
||||
}
|
||||
return to_schema<_i + 1>(_definitions, _values);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include "Parser_rfl_array.hpp"
|
||||
#include "Parser_rfl_variant.hpp"
|
||||
#include "Parser_shared_ptr.hpp"
|
||||
#include "Parser_skip.hpp"
|
||||
#include "Parser_string_view.hpp"
|
||||
#include "Parser_tagged_union.hpp"
|
||||
#include "Parser_tuple.hpp"
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
#ifndef RFL_PARSING_PARSER_SKIP_HPP_
|
||||
#define RFL_PARSING_PARSER_SKIP_HPP_
|
||||
|
||||
#include <map>
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "../internal/Skip.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
#include "schema/Type.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class R, class W, class T, bool _skip_serialization,
|
||||
bool _skip_deserialization, class ProcessorsType>
|
||||
requires AreReaderAndWriter<
|
||||
R, W, internal::Skip<T, _skip_serialization, _skip_deserialization>>
|
||||
struct Parser<R, W,
|
||||
internal::Skip<T, _skip_serialization, _skip_deserialization>,
|
||||
ProcessorsType> {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
static Result<internal::Skip<T, _skip_serialization, _skip_deserialization>>
|
||||
read(const R& _r, const InputVarType& _var) noexcept {
|
||||
if constexpr (_skip_deserialization) {
|
||||
return internal::Skip<T, _skip_serialization, _skip_deserialization>(
|
||||
std::remove_cvref_t<T>());
|
||||
} else {
|
||||
const auto to_skip = [&](auto&& _t) {
|
||||
return internal::Skip<T, _skip_serialization, _skip_deserialization>(
|
||||
std::move(_t));
|
||||
};
|
||||
return Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::read(_r,
|
||||
_var)
|
||||
.transform(to_skip);
|
||||
}
|
||||
}
|
||||
|
||||
template <class P>
|
||||
static void write(const W& _w,
|
||||
const internal::Skip<T, _skip_serialization,
|
||||
_skip_deserialization>& _skip,
|
||||
const P& _parent) noexcept {
|
||||
if constexpr (_skip_serialization) {
|
||||
using ReflectionType = std::remove_cvref_t<typename internal::Skip<
|
||||
T, _skip_serialization, _skip_deserialization>::ReflectionType>;
|
||||
Parser<R, W, ReflectionType, ProcessorsType>::write(_w, ReflectionType(),
|
||||
_parent);
|
||||
} else {
|
||||
Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::write(
|
||||
_w, _skip.value(), _parent);
|
||||
}
|
||||
}
|
||||
|
||||
static schema::Type to_schema(
|
||||
std::map<std::string, schema::Type>* _definitions) {
|
||||
return Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::to_schema(
|
||||
_definitions);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "../internal/has_reflection_method_v.hpp"
|
||||
#include "../internal/has_reflection_type_v.hpp"
|
||||
#include "../internal/is_rename.hpp"
|
||||
#include "../internal/is_skip.hpp"
|
||||
#include "is_map_like.hpp"
|
||||
#include "is_vector_like.hpp"
|
||||
|
||||
@@ -17,6 +18,8 @@ static const bool is_empty(const T& _var) {
|
||||
using Type = std::remove_cvref_t<T>;
|
||||
if constexpr (std::is_pointer_v<Type>) {
|
||||
return !_var || is_empty(*_var);
|
||||
} else if constexpr (internal::is_skip_v<Type>) {
|
||||
return Type::skip_serialization_;
|
||||
} else if constexpr (internal::has_reflection_type_v<Type>) {
|
||||
if constexpr (internal::has_reflection_method_v<Type>) {
|
||||
return is_empty(_var.reflection());
|
||||
|
||||
Reference in New Issue
Block a user