mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 02:36:18 +08:00
Refined the concepts and added comments
This commit is contained in:
+10
-10
@@ -33,6 +33,7 @@ struct Reader {
|
||||
};
|
||||
|
||||
struct YYJSONInputVar {
|
||||
YYJSONInputVar() : val_(nullptr) {}
|
||||
YYJSONInputVar(yyjson_val* _val) : val_(_val) {}
|
||||
yyjson_val* val_;
|
||||
};
|
||||
@@ -64,8 +65,8 @@ struct Reader {
|
||||
return var;
|
||||
}
|
||||
|
||||
bool is_empty(InputVarType* _var) const noexcept {
|
||||
return !_var->val_ || yyjson_is_null(_var->val_);
|
||||
bool is_empty(const InputVarType& _var) const noexcept {
|
||||
return !_var.val_ || yyjson_is_null(_var.val_);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
@@ -103,19 +104,18 @@ struct Reader {
|
||||
return InputArrayType(_var->val_);
|
||||
}
|
||||
|
||||
template <size_t size>
|
||||
std::array<std::optional<InputVarType>, size> to_fields_array(
|
||||
const std::unordered_map<std::string_view, size_t>& _field_indices,
|
||||
InputObjectType* _obj) const noexcept {
|
||||
std::array<std::optional<InputVarType>, size> f_arr;
|
||||
template <size_t size, class FunctionType>
|
||||
std::array<InputVarType, size> to_fields_array(
|
||||
const FunctionType _fct, InputObjectType* _obj) const noexcept {
|
||||
std::array<InputVarType, size> f_arr;
|
||||
yyjson_obj_iter iter;
|
||||
yyjson_obj_iter_init(_obj->val_, &iter);
|
||||
yyjson_val* key;
|
||||
while ((key = yyjson_obj_iter_next(&iter))) {
|
||||
const char* k = yyjson_get_str(key);
|
||||
const auto it = _field_indices.find(std::string_view(k));
|
||||
if (it != _field_indices.end()) {
|
||||
f_arr[it->second] = InputVarType(yyjson_obj_iter_get_val(key));
|
||||
const auto ix = _fct(std::string_view(k));
|
||||
if (ix != -1) {
|
||||
f_arr[ix] = InputVarType(yyjson_obj_iter_get_val(key));
|
||||
}
|
||||
}
|
||||
return f_arr;
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include "rfl/Result.hpp"
|
||||
#include "rfl/always_false.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace json {
|
||||
@@ -55,7 +56,7 @@ struct Writer {
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType from_basic_type(const T _var) const noexcept {
|
||||
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>()) {
|
||||
@@ -63,8 +64,10 @@ struct Writer {
|
||||
} else if constexpr (std::is_floating_point<std::decay_t<T>>()) {
|
||||
return OutputVarType(
|
||||
yyjson_mut_real(doc_, static_cast<double>(_var)));
|
||||
} else {
|
||||
} else if constexpr (std::is_integral<std::decay_t<T>>()) {
|
||||
return OutputVarType(yyjson_mut_int(doc_, static_cast<int>(_var)));
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,8 +79,8 @@ struct Writer {
|
||||
return OutputObjectType(yyjson_mut_obj(doc_));
|
||||
}
|
||||
|
||||
bool is_empty(OutputVarType* _var) const noexcept {
|
||||
return yyjson_mut_is_null(_var->val_);
|
||||
bool is_empty(const OutputVarType& _var) const noexcept {
|
||||
return yyjson_mut_is_null(_var.val_);
|
||||
}
|
||||
|
||||
void set_field(const std::string& _name, const OutputVarType& _var,
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class R, class W, class T>
|
||||
concept AreReaderAndWriter = IsReader<R, T> && IsWriter<W>;
|
||||
concept AreReaderAndWriter = IsReader<R, T> && IsWriter<W, T>;
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
#ifndef RFL_PARSING_ISREADER_HPP_
|
||||
#define RFL_PARSING_ISREADER_HPP_
|
||||
|
||||
#include <array>
|
||||
#include <concepts>
|
||||
#include <unordered_map>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include "rfl/Result.hpp"
|
||||
|
||||
@@ -10,26 +14,64 @@ namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class R, class T>
|
||||
concept IsReader = requires(R r, typename R::InputArrayType arr,
|
||||
concept IsReader = requires(R r, std::string name,
|
||||
std::function<std::int16_t(std::string_view)> fct,
|
||||
typename R::InputArrayType arr,
|
||||
typename R::InputObjectType obj,
|
||||
typename R::InputVarType var) {
|
||||
/// Any Reader needs to define the following:
|
||||
///
|
||||
/// 1) An InputArrayType, which must be an array-like data structure.
|
||||
/// 2) An InputObjectType, which must contain key-value pairs.
|
||||
/// 3) An InputVarType, which must be able to represent either
|
||||
/// InputArrayType, InputObjectType or a basic type (bool, integral,
|
||||
/// floating point, std::string).
|
||||
/// 4) A static constexpr bool has_custom_constructor, that determines
|
||||
/// whether the class in question as a custom constructor, which might
|
||||
/// be called something like from_json_obj(...).
|
||||
|
||||
/// Retrieves a particular field from an object.
|
||||
{
|
||||
r.get_field(name, &obj)
|
||||
} -> std::same_as<rfl::Result<typename R::InputVarType>>;
|
||||
|
||||
/// Determines whether a variable is empty (the NULL type).
|
||||
{ r.is_empty(var) } -> std::same_as<bool>;
|
||||
|
||||
/// Transforms var to a basic type (bool, integral,
|
||||
/// floating point, std::string)
|
||||
{ r.template to_basic_type<T>(&var) } -> std::same_as<rfl::Result<T>>;
|
||||
|
||||
/// fct is a function that turns the field name into the field index of the
|
||||
/// struct. It returns -1, if the fields does not exist on the struct. This
|
||||
/// returns an std::array that can be used to build up the struct.
|
||||
{
|
||||
r.template to_fields_array<6>(fct, &obj)
|
||||
} -> std::same_as<std::array<typename R::InputVarType, 6>>;
|
||||
|
||||
/// Casts var as an InputArrayType.
|
||||
{
|
||||
r.to_array(&var)
|
||||
} -> std::same_as<rfl::Result<typename R::InputArrayType>>;
|
||||
|
||||
/// Iterates through an object and writes the contained key-value pairs into
|
||||
/// a vector.
|
||||
{
|
||||
r.to_map(&obj)
|
||||
} -> std::same_as<
|
||||
std::vector<std::pair<std::string, typename R::InputVarType>>>;
|
||||
|
||||
/// Casts var as an InputObjectType.
|
||||
{
|
||||
r.to_object(&var)
|
||||
} -> std::same_as<rfl::Result<typename R::InputObjectType>>;
|
||||
|
||||
/// Iterates through an array and writes the contained vars into
|
||||
/// a vector.
|
||||
{ r.to_vec(&arr) } -> std::same_as<std::vector<typename R::InputVarType>>;
|
||||
|
||||
/// Uses the custom constructor, if it has been determined that T has one
|
||||
/// (see above).
|
||||
{
|
||||
r.template use_custom_constructor<T>(&var)
|
||||
} -> std::same_as<rfl::Result<T>>;
|
||||
|
||||
@@ -8,21 +8,39 @@
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class W>
|
||||
concept IsWriter = requires(W w, std::string name,
|
||||
template <class W, class T>
|
||||
concept IsWriter = requires(W w, T t, std::string name,
|
||||
typename W::OutputArrayType arr,
|
||||
typename W::OutputObjectType obj,
|
||||
typename W::OutputVarType var) {
|
||||
/// Any Writer needs to define the following:
|
||||
///
|
||||
/// 1) An OutputArrayType, which must be an array-like data structure.
|
||||
/// 2) An OutputObjectType, which must contain key-value pairs.
|
||||
/// 3) An OutputVarType, which must be able to represent either
|
||||
/// OutputArrayType, OutputObjectType or a basic type (bool, integral,
|
||||
/// floating point, std::string).
|
||||
|
||||
/// Appends var to the end of arr, thus mutating it.
|
||||
{ w.add(var, &arr) } -> std::same_as<void>;
|
||||
|
||||
/// Returns an empty var (usually the NULL type).
|
||||
{ w.empty_var() } -> std::same_as<typename W::OutputVarType>;
|
||||
|
||||
/// Generates a var from a basic type (std::string, bool, floating point or
|
||||
/// integral).
|
||||
{ w.from_basic_type(t) } -> std::same_as<typename W::OutputVarType>;
|
||||
|
||||
/// Generates a new, empty array.
|
||||
{ w.new_array() } -> std::same_as<typename W::OutputArrayType>;
|
||||
|
||||
/// Generates a new, empty object.
|
||||
{ w.new_object() } -> std::same_as<typename W::OutputObjectType>;
|
||||
|
||||
{ w.is_empty(&var) } -> std::same_as<bool>;
|
||||
/// Determines whether the var is empty (the NULL type).
|
||||
{ w.is_empty(var) } -> std::same_as<bool>;
|
||||
|
||||
/// Adds a new field to obj, thus mutating it.
|
||||
{ w.set_field(name, var, &obj) } -> std::same_as<void>;
|
||||
};
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <deque>
|
||||
#include <exception>
|
||||
#include <forward_list>
|
||||
@@ -346,9 +347,15 @@ struct Parser<R, W, NamedTuple<FieldTypes...>> {
|
||||
/// Generates a NamedTuple from a JSON Object.
|
||||
static Result<NamedTuple<FieldTypes...>> read(const R& _r,
|
||||
InputVarType* _var) noexcept {
|
||||
const auto& indices = field_indices();
|
||||
const auto fct = [&](const std::string_view& _str) -> std::int16_t {
|
||||
const auto it = indices.find(_str);
|
||||
return it != indices.end() ? it->second
|
||||
: static_cast<std::uint16_t>(-1);
|
||||
};
|
||||
const auto to_fields_array = [&](auto _obj) {
|
||||
return _r.template to_fields_array<sizeof...(FieldTypes)>(
|
||||
field_indices(), &_obj);
|
||||
return _r.template to_fields_array<sizeof...(FieldTypes)>(fct,
|
||||
&_obj);
|
||||
};
|
||||
const auto build = [&](auto _fields_vec) {
|
||||
return build_named_tuple_recursively(_r, _fields_vec);
|
||||
@@ -369,8 +376,7 @@ struct Parser<R, W, NamedTuple<FieldTypes...>> {
|
||||
template <class... Args>
|
||||
static Result<NamedTuple<FieldTypes...>> build_named_tuple_recursively(
|
||||
const R& _r,
|
||||
const std::array<std::optional<InputVarType>, sizeof...(FieldTypes)>&
|
||||
_fields_vec,
|
||||
const std::array<InputVarType, sizeof...(FieldTypes)>& _fields_vec,
|
||||
Args&&... _args) noexcept {
|
||||
constexpr auto i = sizeof...(Args);
|
||||
|
||||
@@ -384,7 +390,7 @@ struct Parser<R, W, NamedTuple<FieldTypes...>> {
|
||||
|
||||
const auto& f = std::get<i>(_fields_vec);
|
||||
|
||||
if (!f) {
|
||||
if (_r.is_empty(f)) {
|
||||
if constexpr (is_required<ValueType>()) {
|
||||
const auto key = FieldType::name_.str();
|
||||
return collect_errors<i + 1>(
|
||||
@@ -409,7 +415,7 @@ struct Parser<R, W, NamedTuple<FieldTypes...>> {
|
||||
_r, _fields_vec, std::vector<Error>({std::move(_error)}));
|
||||
};
|
||||
|
||||
return get_value<FieldType>(_r, *f)
|
||||
return get_value<FieldType>(_r, f)
|
||||
.or_else(handle_error)
|
||||
.and_then(build);
|
||||
}
|
||||
@@ -420,8 +426,7 @@ struct Parser<R, W, NamedTuple<FieldTypes...>> {
|
||||
template <int _i>
|
||||
static Error collect_errors(
|
||||
const R& _r,
|
||||
const std::array<std::optional<InputVarType>, sizeof...(FieldTypes)>&
|
||||
_fields_vec,
|
||||
const std::array<InputVarType, sizeof...(FieldTypes)>& _fields_vec,
|
||||
std::vector<Error> _errors) noexcept {
|
||||
if constexpr (_i == sizeof...(FieldTypes)) {
|
||||
if (_errors.size() == 1) {
|
||||
@@ -443,7 +448,7 @@ struct Parser<R, W, NamedTuple<FieldTypes...>> {
|
||||
|
||||
const auto& f = std::get<_i>(_fields_vec);
|
||||
|
||||
if (!f) {
|
||||
if (_r.is_empty(f)) {
|
||||
if constexpr (is_required<ValueType>()) {
|
||||
const auto key = FieldType::name_.str();
|
||||
_errors.emplace_back(
|
||||
@@ -459,7 +464,7 @@ struct Parser<R, W, NamedTuple<FieldTypes...>> {
|
||||
return _error;
|
||||
};
|
||||
|
||||
get_value<FieldType>(_r, *f).or_else(add_error_if_applicable);
|
||||
get_value<FieldType>(_r, f).or_else(add_error_if_applicable);
|
||||
|
||||
return collect_errors<_i + 1>(_r, _fields_vec, std::move(_errors));
|
||||
}
|
||||
@@ -480,7 +485,7 @@ struct Parser<R, W, NamedTuple<FieldTypes...>> {
|
||||
auto value = Parser<R, W, ValueType>::write(_w, rfl::get<_i>(_tup));
|
||||
const auto name = FieldType::name_.str();
|
||||
if constexpr (!is_required<ValueType>()) {
|
||||
if (!_w.is_empty(&value)) {
|
||||
if (!_w.is_empty(value)) {
|
||||
_w.set_field(name, value, _ptr);
|
||||
}
|
||||
} else {
|
||||
@@ -523,14 +528,16 @@ struct Parser<R, W, NamedTuple<FieldTypes...>> {
|
||||
typename std::tuple_element<_i,
|
||||
std::tuple<FieldTypes...>>::type;
|
||||
const auto name = FieldType::name_.string_view();
|
||||
Parser<R, W, NamedTuple<FieldTypes...>>::field_indices_[name] = _i;
|
||||
Parser<R, W, NamedTuple<FieldTypes...>>::field_indices_[name] =
|
||||
static_cast<std::int16_t>(_i);
|
||||
set_field_indices<_i + 1>();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
/// Maps each of the field names to an index signifying their order.
|
||||
static inline std::unordered_map<std::string_view, size_t> field_indices_;
|
||||
static inline std::unordered_map<std::string_view, std::int16_t>
|
||||
field_indices_;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
@@ -544,7 +551,7 @@ struct Parser<R, W, std::optional<T>> {
|
||||
/// Expresses the variables as type T.
|
||||
static Result<std::optional<T>> read(const R& _r,
|
||||
InputVarType* _var) noexcept {
|
||||
if (_r.is_empty(_var)) {
|
||||
if (_r.is_empty(*_var)) {
|
||||
return std::optional<T>();
|
||||
}
|
||||
const auto to_opt = [](auto&& _t) { return std::make_optional<T>(_t); };
|
||||
@@ -594,7 +601,7 @@ struct Parser<R, W, std::shared_ptr<T>> {
|
||||
/// Expresses the variables as type T.
|
||||
static Result<std::shared_ptr<T>> read(const R& _r,
|
||||
InputVarType* _var) noexcept {
|
||||
if (_r.is_empty(_var)) {
|
||||
if (_r.is_empty(*_var)) {
|
||||
return std::shared_ptr<T>();
|
||||
}
|
||||
const auto to_ptr = [](auto&& _t) {
|
||||
@@ -850,7 +857,7 @@ struct Parser<R, W, std::unique_ptr<T>> {
|
||||
/// Expresses the variables as type T.
|
||||
static Result<std::unique_ptr<T>> read(const R& _r,
|
||||
InputVarType* _var) noexcept {
|
||||
if (_r.is_empty(_var)) {
|
||||
if (_r.is_empty(*_var)) {
|
||||
return std::unique_ptr<T>();
|
||||
}
|
||||
const auto to_ptr = [](auto&& _t) {
|
||||
|
||||
Reference in New Issue
Block a user