mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 10:46:18 +08:00
Moved Array.hpp and wrap_in_rfl_array_t into internal
This commit is contained in:
@@ -7,9 +7,9 @@
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include "Array.hpp"
|
||||
#include "Literal.hpp"
|
||||
#include "default.hpp"
|
||||
#include "internal/Array.hpp"
|
||||
#include "internal/StringLiteral.hpp"
|
||||
#include "internal/to_std_array.hpp"
|
||||
|
||||
@@ -139,7 +139,7 @@ template <internal::StringLiteral _name, class T>
|
||||
inline auto make_field(T&& _value) {
|
||||
using T0 = std::remove_cvref_t<T>;
|
||||
if constexpr (std::is_array_v<T0>) {
|
||||
return Field<_name, T0>(rfl::Array<T0>(std::forward<T>(_value)));
|
||||
return Field<_name, T0>(internal::Array<T0>(std::forward<T>(_value)));
|
||||
} else {
|
||||
return Field<_name, T0>(std::forward<T>(_value));
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
#include <vector>
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../wrap_in_rfl_array_t.hpp"
|
||||
#include "Parser.hpp"
|
||||
|
||||
namespace rfl {
|
||||
@@ -17,14 +16,14 @@ using InputVarType = typename Reader::InputVarType;
|
||||
|
||||
/// Parses an object from flexbuf var.
|
||||
template <class T>
|
||||
Result<wrap_in_rfl_array_t<T>> read(const InputVarType& _obj) {
|
||||
auto read(const InputVarType& _obj) {
|
||||
const auto r = Reader();
|
||||
return Parser<T>::read(r, _obj);
|
||||
}
|
||||
|
||||
/// Parses an object from flexbuf using reflection.
|
||||
template <class T>
|
||||
Result<wrap_in_rfl_array_t<T>> read(const char* _bytes, const size_t _size) {
|
||||
auto read(const char* _bytes, const size_t _size) {
|
||||
const InputVarType root =
|
||||
flexbuffers::GetRoot(reinterpret_cast<const uint8_t*>(_bytes), _size);
|
||||
return read<T>(root);
|
||||
@@ -32,13 +31,13 @@ Result<wrap_in_rfl_array_t<T>> read(const char* _bytes, const size_t _size) {
|
||||
|
||||
/// Parses an object from flexbuf using reflection.
|
||||
template <class T>
|
||||
Result<wrap_in_rfl_array_t<T>> read(const std::vector<char>& _bytes) {
|
||||
auto read(const std::vector<char>& _bytes) {
|
||||
return read<T>(_bytes.data(), _bytes.size());
|
||||
}
|
||||
|
||||
/// Parses an object directly from a stream.
|
||||
template <class T>
|
||||
Result<wrap_in_rfl_array_t<T>> read(std::istream& _stream) {
|
||||
auto read(std::istream& _stream) {
|
||||
std::istreambuf_iterator<char> begin(_stream), end;
|
||||
const auto bytes = std::vector<char>(begin, end);
|
||||
return read<T>(bytes.data(), bytes.size());
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
#ifndef RFL_ARRAY_HPP_
|
||||
#define RFL_ARRAY_HPP_
|
||||
#ifndef RFL_INTERNAL_ARRAY_HPP_
|
||||
#define RFL_INTERNAL_ARRAY_HPP_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "internal/to_std_array.hpp"
|
||||
#include "to_std_array.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class T>
|
||||
requires std::is_array_v<T>
|
||||
struct Array {
|
||||
using Type = T;
|
||||
using StdArrayType = internal::to_std_array_t<T>;
|
||||
using StdArrayType = to_std_array_t<T>;
|
||||
|
||||
Array() = default;
|
||||
Array(const StdArrayType &_arr) : arr_(_arr) {}
|
||||
@@ -22,6 +23,7 @@ struct Array {
|
||||
StdArrayType arr_;
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <functional>
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Array.hpp"
|
||||
#include "Array.hpp"
|
||||
#include "is_flatten_field.hpp"
|
||||
#include "is_named_tuple.hpp"
|
||||
#include "ptr_tuple_t.hpp"
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
#ifndef RFL_WRAP_IN_RFL_ARRAY_T_
|
||||
#define RFL_WRAP_IN_RFL_ARRAY_T_
|
||||
#ifndef RFL_INTERNAL_WRAP_IN_RFL_ARRAY_T_
|
||||
#define RFL_INTERNAL_WRAP_IN_RFL_ARRAY_T_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "Array.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class T>
|
||||
struct wrap_in_rfl_array {
|
||||
@@ -13,7 +14,7 @@ struct wrap_in_rfl_array {
|
||||
};
|
||||
|
||||
template <class T>
|
||||
requires std::is_array_v<T>
|
||||
requires std::is_array_v<T>
|
||||
struct wrap_in_rfl_array<T> {
|
||||
using type = Array<T>;
|
||||
};
|
||||
@@ -21,6 +22,7 @@ struct wrap_in_rfl_array<T> {
|
||||
template <class T>
|
||||
using wrap_in_rfl_array_t = typename wrap_in_rfl_array<T>::type;
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -6,7 +6,6 @@
|
||||
#include <istream>
|
||||
#include <string>
|
||||
|
||||
#include "../wrap_in_rfl_array_t.hpp"
|
||||
#include "Parser.hpp"
|
||||
#include "Reader.hpp"
|
||||
|
||||
@@ -18,14 +17,14 @@ using InputVarType = typename Reader::InputVarType;
|
||||
|
||||
/// Parses an object from a JSON var.
|
||||
template <class T>
|
||||
Result<wrap_in_rfl_array_t<T>> read(const InputVarType& _obj) {
|
||||
auto read(const InputVarType& _obj) {
|
||||
const auto r = Reader();
|
||||
return Parser<T>::read(r, _obj);
|
||||
}
|
||||
|
||||
/// Parses an object from JSON using reflection.
|
||||
template <class T>
|
||||
Result<wrap_in_rfl_array_t<T>> read(const std::string& _json_str) {
|
||||
auto read(const std::string& _json_str) {
|
||||
using PtrType = std::unique_ptr<yyjson_doc, void (*)(yyjson_doc*)>;
|
||||
yyjson_doc* doc = yyjson_read(_json_str.c_str(), _json_str.size(), 0);
|
||||
const auto ptr = PtrType(doc, yyjson_doc_free);
|
||||
@@ -34,7 +33,7 @@ Result<wrap_in_rfl_array_t<T>> read(const std::string& _json_str) {
|
||||
|
||||
/// Parses an object from a stringstream.
|
||||
template <class T>
|
||||
Result<wrap_in_rfl_array_t<T>> read(std::istream& _stream) {
|
||||
auto read(std::istream& _stream) {
|
||||
const auto json_str = std::string(std::istreambuf_iterator<char>(_stream),
|
||||
std::istreambuf_iterator<char>());
|
||||
return read<T>(json_str);
|
||||
|
||||
@@ -8,8 +8,8 @@
|
||||
#include "NamedTuple.hpp"
|
||||
#include "internal/is_named_tuple.hpp"
|
||||
#include "internal/ptr_named_tuple_t.hpp"
|
||||
#include "internal/wrap_in_rfl_array_t.hpp"
|
||||
#include "to_named_tuple.hpp"
|
||||
#include "wrap_in_rfl_array_t.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
@@ -19,8 +19,8 @@ struct remove_ptr;
|
||||
template <internal::StringLiteral _name, class T>
|
||||
struct remove_ptr<Field<_name, T>> {
|
||||
using FieldType =
|
||||
Field<_name,
|
||||
wrap_in_rfl_array_t<std::remove_cvref_t<std::remove_pointer_t<T>>>>;
|
||||
Field<_name, internal::wrap_in_rfl_array_t<
|
||||
std::remove_cvref_t<std::remove_pointer_t<T>>>>;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
|
||||
@@ -11,16 +11,17 @@
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../internal/is_basic_type.hpp"
|
||||
#include "../wrap_in_rfl_array_t.hpp"
|
||||
#include "../internal/wrap_in_rfl_array_t.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class R, class T>
|
||||
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) {
|
||||
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.
|
||||
@@ -35,7 +36,7 @@ concept IsReader = requires(
|
||||
/// Retrieves a particular field from an object.
|
||||
{
|
||||
r.get_field(name, obj)
|
||||
} -> std::same_as<rfl::Result<typename R::InputVarType>>;
|
||||
} -> 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>;
|
||||
@@ -43,15 +44,15 @@ concept IsReader = requires(
|
||||
/// Transforms var to a basic type (bool, integral,
|
||||
/// floating point, std::string)
|
||||
{
|
||||
r.template to_basic_type<wrap_in_rfl_array_t<T>>(var)
|
||||
} -> std::same_as<rfl::Result<wrap_in_rfl_array_t<T>>>;
|
||||
r.template to_basic_type<internal::wrap_in_rfl_array_t<T>>(var)
|
||||
} -> std::same_as<rfl::Result<internal::wrap_in_rfl_array_t<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<std::optional<typename R::InputVarType>, 6>>;
|
||||
} -> std::same_as<std::array<std::optional<typename R::InputVarType>, 6>>;
|
||||
|
||||
/// Casts var as an InputArrayType.
|
||||
{ r.to_array(var) } -> std::same_as<rfl::Result<typename R::InputArrayType>>;
|
||||
@@ -60,13 +61,13 @@ concept IsReader = requires(
|
||||
/// a vector.
|
||||
{
|
||||
r.to_map(obj)
|
||||
} -> std::same_as<
|
||||
std::vector<std::pair<std::string, typename R::InputVarType>>>;
|
||||
} -> 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>>;
|
||||
} -> std::same_as<rfl::Result<typename R::InputObjectType>>;
|
||||
|
||||
/// Iterates through an array and writes the contained vars into
|
||||
/// a vector.
|
||||
@@ -75,8 +76,8 @@ concept IsReader = requires(
|
||||
/// Uses the custom constructor, if it has been determined that T has one
|
||||
/// (see above).
|
||||
{
|
||||
r.template use_custom_constructor<wrap_in_rfl_array_t<T>>(var)
|
||||
} -> std::same_as<rfl::Result<wrap_in_rfl_array_t<T>>>;
|
||||
r.template use_custom_constructor<internal::wrap_in_rfl_array_t<T>>(var)
|
||||
} -> std::same_as<rfl::Result<internal::wrap_in_rfl_array_t<T>>>;
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Array.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "../internal/Array.hpp"
|
||||
#include "../internal/to_std_array.hpp"
|
||||
#include "Parent.hpp"
|
||||
#include "Parser_array.hpp"
|
||||
@@ -27,9 +27,9 @@ struct Parser<R, W, T[_size]> {
|
||||
using ParentType = Parent<W>;
|
||||
using CArray = T[_size];
|
||||
|
||||
static Result<Array<T[_size]>> read(const R& _r,
|
||||
const InputVarType& _var) noexcept {
|
||||
using StdArray = internal::to_std_array_t<T[_size]>;
|
||||
static Result<internal::Array<CArray>> read(
|
||||
const R& _r, const InputVarType& _var) noexcept {
|
||||
using StdArray = internal::to_std_array_t<CArray>;
|
||||
return Parser<R, W, StdArray>::read(_r, _var);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Array.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "../internal/Array.hpp"
|
||||
#include "../internal/to_std_array.hpp"
|
||||
#include "Parser_array.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
@@ -14,8 +14,8 @@ namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class R, class W, class T>
|
||||
requires AreReaderAndWriter<R, W, Array<T>>
|
||||
struct Parser<R, W, Array<T>> {
|
||||
requires AreReaderAndWriter<R, W, internal::Array<T>>
|
||||
struct Parser<R, W, internal::Array<T>> {
|
||||
public:
|
||||
using InputArrayType = typename R::InputArrayType;
|
||||
using InputVarType = typename R::InputVarType;
|
||||
@@ -25,12 +25,13 @@ struct Parser<R, W, Array<T>> {
|
||||
|
||||
using StdArray = internal::to_std_array_t<T>;
|
||||
|
||||
static Result<Array<T>> read(const R& _r, const InputVarType& _var) noexcept {
|
||||
static Result<internal::Array<T>> read(const R& _r,
|
||||
const InputVarType& _var) noexcept {
|
||||
return Parser<R, W, StdArray>::read(_r, _var);
|
||||
}
|
||||
|
||||
template <class P>
|
||||
static void write(const W& _w, const Array<T>& _arr,
|
||||
static void write(const W& _w, const internal::Array<T>& _arr,
|
||||
const P& _parent) noexcept {
|
||||
Parser<R, W, StdArray>::write(_w, _arr.arr_, _parent);
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
|
||||
#include "../internal/get_type_name.hpp"
|
||||
#include "../internal/remove_namespaces.hpp"
|
||||
#include "../wrap_in_rfl_array_t.hpp"
|
||||
#include "Parser.hpp"
|
||||
#include "Reader.hpp"
|
||||
|
||||
@@ -18,14 +17,14 @@ using InputVarType = typename Reader::InputVarType;
|
||||
|
||||
/// Parses an object from a XML var.
|
||||
template <class T>
|
||||
Result<wrap_in_rfl_array_t<T>> read(const InputVarType& _var) {
|
||||
auto read(const InputVarType& _var) {
|
||||
const auto r = Reader();
|
||||
return Parser<T>::read(r, _var);
|
||||
}
|
||||
|
||||
/// Parses an object from XML using reflection.
|
||||
template <class T>
|
||||
Result<wrap_in_rfl_array_t<T>> read(const std::string& _xml_str) {
|
||||
auto read(const std::string& _xml_str) {
|
||||
pugi::xml_document doc;
|
||||
const auto result = doc.load_string(_xml_str.c_str());
|
||||
if (!result) {
|
||||
@@ -38,7 +37,7 @@ Result<wrap_in_rfl_array_t<T>> read(const std::string& _xml_str) {
|
||||
|
||||
/// Parses an object from a stringstream.
|
||||
template <class T>
|
||||
Result<wrap_in_rfl_array_t<T>> read(std::istream& _stream) {
|
||||
auto read(std::istream& _stream) {
|
||||
const auto xml_str = std::string(std::istreambuf_iterator<char>(_stream),
|
||||
std::istreambuf_iterator<char>());
|
||||
return read<T>(xml_str);
|
||||
|
||||
@@ -16,14 +16,14 @@ using InputVarType = typename Reader::InputVarType;
|
||||
|
||||
/// Parses an object from a YAML var.
|
||||
template <class T>
|
||||
Result<T> read(const InputVarType& _var) {
|
||||
auto read(const InputVarType& _var) {
|
||||
const auto r = Reader();
|
||||
return Parser<T>::read(r, _var);
|
||||
}
|
||||
|
||||
/// Parses an object from YAML using reflection.
|
||||
template <class T>
|
||||
Result<T> read(const std::string& _yaml_str) {
|
||||
auto read(const std::string& _yaml_str) {
|
||||
try {
|
||||
const auto var = InputVarType(YAML::Load(_yaml_str));
|
||||
return read<T>(var);
|
||||
@@ -34,7 +34,7 @@ Result<T> read(const std::string& _yaml_str) {
|
||||
|
||||
/// Parses an object from a stringstream.
|
||||
template <class T>
|
||||
Result<T> read(std::istream& _stream) {
|
||||
auto read(std::istream& _stream) {
|
||||
const auto yaml_str = std::string(std::istreambuf_iterator<char>(_stream),
|
||||
std::istreambuf_iterator<char>());
|
||||
return read<T>(yaml_str);
|
||||
|
||||
Reference in New Issue
Block a user