Skip to content

Commit

Permalink
Remove Result<c-array>.
Browse files Browse the repository at this point in the history
  • Loading branch information
hazby2002 committed Jan 2, 2024
1 parent 804e0ed commit c6d3012
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 33 deletions.
11 changes: 0 additions & 11 deletions include/rfl/Result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 5 additions & 4 deletions include/rfl/flexbuf/read.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <vector>

#include "../Result.hpp"
#include "../wrap_in_rfl_array_t.hpp"
#include "Parser.hpp"

namespace rfl {
Expand All @@ -16,28 +17,28 @@ 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);
}

/// 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());
Expand Down
7 changes: 4 additions & 3 deletions include/rfl/json/read.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <istream>
#include <string>

#include "../wrap_in_rfl_array_t.hpp"
#include "Parser.hpp"
#include "Reader.hpp"

Expand All @@ -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);
Expand All @@ -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);
Expand Down
29 changes: 17 additions & 12 deletions include/rfl/parsing/IsReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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>>;
Expand All @@ -57,21 +60,23 @@ 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.
{ 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>>;
{
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
Expand Down
7 changes: 4 additions & 3 deletions include/rfl/xml/read.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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) {
Expand All @@ -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);
Expand Down

0 comments on commit c6d3012

Please sign in to comment.