mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 00:26:19 +08:00
Remove Result<c-array>.
This commit is contained in:
@@ -342,17 +342,6 @@ class Result {
|
||||
std::variant<T, Error> t_or_err_;
|
||||
};
|
||||
|
||||
/// This is simply to get the compilation to pass.
|
||||
template <class T, std::size_t _n>
|
||||
struct Result<T[_n]> : public Result<internal::to_std_array_t<T[_n]>> {
|
||||
using StdArray = internal::to_std_array_t<T[_n]>;
|
||||
using Base = Result<StdArray>;
|
||||
using Base::Base;
|
||||
|
||||
Result(const Base& other) : Base(other) {}
|
||||
Result(Base&& other) : Base(std::move(other)) {}
|
||||
};
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../wrap_in_rfl_array_t.hpp"
|
||||
#include "Parser.hpp"
|
||||
|
||||
namespace rfl {
|
||||
@@ -16,14 +17,14 @@ using InputVarType = typename Reader::InputVarType;
|
||||
|
||||
/// Parses an object from flexbuf var.
|
||||
template <class T>
|
||||
Result<T> read(const InputVarType& _obj) {
|
||||
Result<wrap_in_rfl_array_t<T>> 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<T> read(const char* _bytes, const size_t _size) {
|
||||
Result<wrap_in_rfl_array_t<T>> read(const char* _bytes, const size_t _size) {
|
||||
const InputVarType root =
|
||||
flexbuffers::GetRoot(reinterpret_cast<const uint8_t*>(_bytes), _size);
|
||||
return read<T>(root);
|
||||
@@ -31,13 +32,13 @@ Result<T> read(const char* _bytes, const size_t _size) {
|
||||
|
||||
/// Parses an object from flexbuf using reflection.
|
||||
template <class T>
|
||||
Result<T> read(const std::vector<char>& _bytes) {
|
||||
Result<wrap_in_rfl_array_t<T>> read(const std::vector<char>& _bytes) {
|
||||
return read<T>(_bytes.data(), _bytes.size());
|
||||
}
|
||||
|
||||
/// Parses an object directly from a stream.
|
||||
template <class T>
|
||||
Result<T> read(std::istream& _stream) {
|
||||
Result<wrap_in_rfl_array_t<T>> 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());
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <istream>
|
||||
#include <string>
|
||||
|
||||
#include "../wrap_in_rfl_array_t.hpp"
|
||||
#include "Parser.hpp"
|
||||
#include "Reader.hpp"
|
||||
|
||||
@@ -17,14 +18,14 @@ using InputVarType = typename Reader::InputVarType;
|
||||
|
||||
/// Parses an object from a JSON var.
|
||||
template <class T>
|
||||
Result<T> read(const InputVarType& _obj) {
|
||||
Result<wrap_in_rfl_array_t<T>> 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<T> read(const std::string& _json_str) {
|
||||
Result<wrap_in_rfl_array_t<T>> 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);
|
||||
@@ -33,7 +34,7 @@ Result<T> read(const std::string& _json_str) {
|
||||
|
||||
/// Parses an object from a stringstream.
|
||||
template <class T>
|
||||
Result<T> read(std::istream& _stream) {
|
||||
Result<wrap_in_rfl_array_t<T>> read(std::istream& _stream) {
|
||||
const auto json_str = std::string(std::istreambuf_iterator<char>(_stream),
|
||||
std::istreambuf_iterator<char>());
|
||||
return read<T>(json_str);
|
||||
|
||||
@@ -10,16 +10,17 @@
|
||||
#include <string_view>
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../internal/is_basic_type.hpp"
|
||||
#include "../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.
|
||||
@@ -34,21 +35,23 @@ concept IsReader = requires(R r, std::string name,
|
||||
/// 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>;
|
||||
|
||||
/// 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>>;
|
||||
{
|
||||
r.template to_basic_type<wrap_in_rfl_array_t<T>>(var)
|
||||
} -> std::same_as<rfl::Result<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>>;
|
||||
@@ -57,13 +60,13 @@ concept IsReader = requires(R r, std::string name,
|
||||
/// 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.
|
||||
@@ -71,7 +74,9 @@ concept IsReader = requires(R r, std::string name,
|
||||
|
||||
/// 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>>;
|
||||
{
|
||||
r.template use_custom_constructor<wrap_in_rfl_array_t<T>>(var)
|
||||
} -> std::same_as<rfl::Result<wrap_in_rfl_array_t<T>>>;
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
#include "../internal/get_type_name.hpp"
|
||||
#include "../internal/remove_namespaces.hpp"
|
||||
#include "../wrap_in_rfl_array_t.hpp"
|
||||
#include "Parser.hpp"
|
||||
#include "Reader.hpp"
|
||||
|
||||
@@ -17,14 +18,14 @@ using InputVarType = typename Reader::InputVarType;
|
||||
|
||||
/// Parses an object from a XML var.
|
||||
template <class T>
|
||||
Result<T> read(const InputVarType& _var) {
|
||||
Result<wrap_in_rfl_array_t<T>> 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<T> read(const std::string& _xml_str) {
|
||||
Result<wrap_in_rfl_array_t<T>> read(const std::string& _xml_str) {
|
||||
pugi::xml_document doc;
|
||||
const auto result = doc.load_string(_xml_str.c_str());
|
||||
if (!result) {
|
||||
@@ -37,7 +38,7 @@ Result<T> read(const std::string& _xml_str) {
|
||||
|
||||
/// Parses an object from a stringstream.
|
||||
template <class T>
|
||||
Result<T> read(std::istream& _stream) {
|
||||
Result<wrap_in_rfl_array_t<T>> read(std::istream& _stream) {
|
||||
const auto xml_str = std::string(std::istreambuf_iterator<char>(_stream),
|
||||
std::istreambuf_iterator<char>());
|
||||
return read<T>(xml_str);
|
||||
|
||||
Reference in New Issue
Block a user