diff --git a/include/rfl/json/Reader.hpp b/include/rfl/json/Reader.hpp index e4f7e527..e5ddf40e 100644 --- a/include/rfl/json/Reader.hpp +++ b/include/rfl/json/Reader.hpp @@ -54,10 +54,9 @@ struct Reader { template static constexpr bool has_custom_constructor = has_from_json_obj::value; - rfl::Result get_field(const std::string& _name, - InputObjectType* _obj) const noexcept { - const auto var = - InputVarType(yyjson_obj_get(_obj->val_, _name.c_str())); + rfl::Result get_field( + const std::string& _name, const InputObjectType _obj) const noexcept { + const auto var = InputVarType(yyjson_obj_get(_obj.val_, _name.c_str())); if (!var.val_) { return rfl::Error("Object contains no field named '" + _name + "'."); @@ -65,51 +64,52 @@ struct Reader { return var; } - bool is_empty(const InputVarType& _var) const noexcept { + bool is_empty(const InputVarType _var) const noexcept { return !_var.val_ || yyjson_is_null(_var.val_); } template - rfl::Result to_basic_type(InputVarType* _var) const noexcept { + rfl::Result to_basic_type(const InputVarType _var) const noexcept { if constexpr (std::is_same, std::string>()) { - const auto r = yyjson_get_str(_var->val_); + const auto r = yyjson_get_str(_var.val_); if (r == NULL) { return rfl::Error("Could not cast to string."); } return std::string(r); } else if constexpr (std::is_same, bool>()) { - if (!yyjson_is_bool(_var->val_)) { + if (!yyjson_is_bool(_var.val_)) { return rfl::Error("Could not cast to boolean."); } - return yyjson_get_bool(_var->val_); + return yyjson_get_bool(_var.val_); } else if constexpr (std::is_floating_point>()) { - if (!yyjson_is_num(_var->val_)) { + if (!yyjson_is_num(_var.val_)) { return rfl::Error("Could not cast to double."); } - return static_cast(yyjson_get_num(_var->val_)); + return static_cast(yyjson_get_num(_var.val_)); } else if constexpr (std::is_integral>()) { - if (!yyjson_is_int(_var->val_)) { + if (!yyjson_is_int(_var.val_)) { return rfl::Error("Could not cast to int."); } - return static_cast(yyjson_get_int(_var->val_)); + return static_cast(yyjson_get_int(_var.val_)); } else { static_assert(rfl::always_false_v, "Unsupported type."); } } - rfl::Result to_array(InputVarType* _var) const noexcept { - if (!yyjson_is_arr(_var->val_)) { + rfl::Result to_array( + const InputVarType _var) const noexcept { + if (!yyjson_is_arr(_var.val_)) { return rfl::Error("Could not cast to array!"); } - return InputArrayType(_var->val_); + return InputArrayType(_var.val_); } template std::array to_fields_array( - const FunctionType _fct, InputObjectType* _obj) const noexcept { + const FunctionType _fct, const InputObjectType _obj) const noexcept { std::array f_arr; yyjson_obj_iter iter; - yyjson_obj_iter_init(_obj->val_, &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); @@ -122,10 +122,10 @@ struct Reader { } std::vector> to_map( - InputObjectType* _obj) const noexcept { + const InputObjectType _obj) const noexcept { std::vector> m; yyjson_obj_iter iter; - yyjson_obj_iter_init(_obj->val_, &iter); + yyjson_obj_iter_init(_obj.val_, &iter); yyjson_val* key; while ((key = yyjson_obj_iter_next(&iter))) { auto p = std::make_pair(yyjson_get_str(key), @@ -135,18 +135,19 @@ struct Reader { return m; } - rfl::Result to_object(InputVarType* _var) const noexcept { - if (!yyjson_is_obj(_var->val_)) { + rfl::Result to_object( + const InputVarType _var) const noexcept { + if (!yyjson_is_obj(_var.val_)) { return rfl::Error("Could not cast to object!"); } - return InputObjectType(_var->val_); + return InputObjectType(_var.val_); } - std::vector to_vec(InputArrayType* _arr) const noexcept { + std::vector to_vec(const InputArrayType _arr) const noexcept { std::vector vec; yyjson_val* val; yyjson_arr_iter iter; - yyjson_arr_iter_init(_arr->val_, &iter); + yyjson_arr_iter_init(_arr.val_, &iter); while ((val = yyjson_arr_iter_next(&iter))) { vec.push_back(InputVarType(val)); } @@ -154,9 +155,10 @@ struct Reader { } template - rfl::Result use_custom_constructor(InputVarType* _var) const noexcept { + rfl::Result use_custom_constructor( + const InputVarType _var) const noexcept { try { - return T::from_json_obj(*_var); + return T::from_json_obj(_var); } catch (std::exception& e) { return rfl::Error(e.what()); } diff --git a/include/rfl/json/read.hpp b/include/rfl/json/read.hpp index 0f5ca6d5..82a041e2 100644 --- a/include/rfl/json/read.hpp +++ b/include/rfl/json/read.hpp @@ -20,9 +20,9 @@ Result read(const std::string& _json_str) { using PtrType = std::unique_ptr; yyjson_doc* doc = yyjson_read(_json_str.c_str(), _json_str.size(), 0); const auto ptr = PtrType(doc, yyjson_doc_free); - InputVarType root = InputVarType(yyjson_doc_get_root(doc)); + const auto root = InputVarType(yyjson_doc_get_root(doc)); const auto r = Reader(); - return Parser::read(r, &root); + return Parser::read(r, root); } } // namespace json diff --git a/include/rfl/parsing/CustomParser.hpp b/include/rfl/parsing/CustomParser.hpp index bbe3424e..6c26fc20 100644 --- a/include/rfl/parsing/CustomParser.hpp +++ b/include/rfl/parsing/CustomParser.hpp @@ -15,7 +15,7 @@ template struct CustomParser { static Result read(const ReaderType& _r, - auto* _var) noexcept { + const auto& _var) noexcept { const auto to_class = [](auto&& _h) -> Result { try { if constexpr (internal::has_to_class_method_v) { diff --git a/include/rfl/parsing/IsReader.hpp b/include/rfl/parsing/IsReader.hpp index ce95d882..d3db7adc 100644 --- a/include/rfl/parsing/IsReader.hpp +++ b/include/rfl/parsing/IsReader.hpp @@ -32,7 +32,7 @@ concept IsReader = requires(R r, std::string name, /// Retrieves a particular field from an object. { - r.get_field(name, &obj) + r.get_field(name, obj) } -> std::same_as>; /// Determines whether a variable is empty (the NULL type). @@ -40,40 +40,40 @@ concept IsReader = requires(R r, std::string name, /// Transforms var to a basic type (bool, integral, /// floating point, std::string) - { r.template to_basic_type(&var) } -> std::same_as>; + { r.template to_basic_type(var) } -> std::same_as>; /// 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) + r.template to_fields_array<6>(fct, obj) } -> std::same_as>; /// Casts var as an InputArrayType. { - r.to_array(&var) + r.to_array(var) } -> std::same_as>; /// Iterates through an object and writes the contained key-value pairs into /// a vector. { - r.to_map(&obj) + r.to_map(obj) } -> std::same_as< std::vector>>; /// Casts var as an InputObjectType. { - r.to_object(&var) + r.to_object(var) } -> std::same_as>; /// Iterates through an array and writes the contained vars into /// a vector. - { r.to_vec(&arr) } -> std::same_as>; + { r.to_vec(arr) } -> std::same_as>; /// Uses the custom constructor, if it has been determined that T has one /// (see above). { - r.template use_custom_constructor(&var) + r.template use_custom_constructor(var) } -> std::same_as>; }; diff --git a/include/rfl/parsing/Parser.hpp b/include/rfl/parsing/Parser.hpp index fc2a9da3..d6395ab0 100644 --- a/include/rfl/parsing/Parser.hpp +++ b/include/rfl/parsing/Parser.hpp @@ -62,7 +62,7 @@ struct Parser { using OutputVarType = typename W::OutputVarType; /// Expresses the variables as type T. - static Result read(const R& _r, InputVarType* _var) noexcept { + static Result read(const R& _r, const InputVarType& _var) noexcept { if constexpr (R::template has_custom_constructor) { return _r.template use_custom_constructor(_var); } else { @@ -124,7 +124,7 @@ struct Parser { using OutputVarType = typename W::OutputVarType; /// Expresses the variables as type T. - static Result read(const R& _r, InputVarType* _var) noexcept { + static Result read(const R& _r, const InputVarType& _var) noexcept { static_assert( always_false_v, "Reading into raw pointers is dangerous and therefore unsupported. " @@ -154,9 +154,9 @@ struct Parser> { using OutputArrayType = typename W::OutputArrayType; using OutputVarType = typename W::OutputVarType; - static Result> read(const R& _r, - InputVarType* _var) noexcept { - const auto to_vec = [&](auto _arr) { return _r.to_vec(&_arr); }; + static Result> read( + const R& _r, const InputVarType& _var) noexcept { + const auto to_vec = [&](const auto& _arr) { return _r.to_vec(_arr); }; const auto check_size = [](auto _vec) -> Result> { @@ -202,15 +202,15 @@ struct Parser> { std::move(_already_extracted)..., std::move(new_entry)); }; - return extract_single_field(_r, &_vec).and_then(extract_next); + return extract_single_field(_r, _vec).and_then(extract_next); } } /// Extracts a single field from a JSON. template - static auto extract_single_field(const R& _r, - std::vector* _vec) noexcept { - return Parser::read(_r, &((*_vec)[_i])); + static auto extract_single_field( + const R& _r, const std::vector& _vec) noexcept { + return Parser::read(_r, _vec[_i]); } }; @@ -223,7 +223,7 @@ struct Parser> { using OutputVarType = typename W::OutputVarType; /// Expresses the variables as type T. - static Result> read(const R& _r, InputVarType* _var) noexcept { + static Result> read(const R& _r, const InputVarType& _var) noexcept { const auto to_box = [](auto&& _t) { return Box::make(std::move(_t)); }; @@ -246,7 +246,7 @@ struct Parser> { /// Expresses the variables as type T. static Result> read(const R& _r, - InputVarType* _var) noexcept { + const InputVarType& _var) noexcept { const auto to_type = [](const auto& _str) { return Literal<_fields...>::from_string(_str); }; @@ -283,19 +283,20 @@ struct MapParser { using ValueType = std::decay_t; - static Result read(const R& _r, InputVarType* _var) noexcept { + static Result read(const R& _r, + const InputVarType& _var) noexcept { const auto get_pair = [&](auto& _pair) { const auto to_pair = [&](ValueType&& _val) { return std::make_pair(std::move(_pair.first), std::move(_val)); }; - return Parser>::read(_r, - &_pair.second) + return Parser>::read(_r, _pair.second) .transform(to_pair) .value(); }; - const auto to_map = [&_r, get_pair](auto _obj) -> Result { - auto m = _r.to_map(&_obj); + const auto to_map = [&_r, + get_pair](const auto& _obj) -> Result { + auto m = _r.to_map(_obj); MapType res; try { for (auto& p : m) { @@ -345,8 +346,8 @@ struct Parser> { public: /// Generates a NamedTuple from a JSON Object. - static Result> read(const R& _r, - InputVarType* _var) noexcept { + static Result> read( + const R& _r, const 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); @@ -355,7 +356,7 @@ struct Parser> { }; const auto to_fields_array = [&](auto _obj) { return _r.template to_fields_array(fct, - &_obj); + _obj); }; const auto build = [&](auto _fields_vec) { return build_named_tuple_recursively(_r, _fields_vec); @@ -508,14 +509,13 @@ struct Parser> { /// Retrieves the value from the object. This is mainly needed to /// generate a better error message. template - static auto get_value(const R& _r, InputVarType _var) noexcept { + static auto get_value(const R& _r, const InputVarType _var) noexcept { const auto embellish_error = [&](const Error& _e) { const auto key = FieldType::name_.str(); return Error("Failed to parse field '" + key + "': " + _e.what()); }; using ValueType = std::decay_t; - return Parser::read(_r, &_var).or_else( - embellish_error); + return Parser::read(_r, _var).or_else(embellish_error); } /// Builds the object field by field. @@ -550,8 +550,8 @@ struct Parser> { /// Expresses the variables as type T. static Result> read(const R& _r, - InputVarType* _var) noexcept { - if (_r.is_empty(*_var)) { + const InputVarType& _var) noexcept { + if (_r.is_empty(_var)) { return std::optional(); } const auto to_opt = [](auto&& _t) { return std::make_optional(_t); }; @@ -577,7 +577,7 @@ struct Parser> { using OutputVarType = typename W::OutputVarType; /// Expresses the variables as type T. - static Result> read(const R& _r, InputVarType* _var) noexcept { + static Result> read(const R& _r, const InputVarType& _var) noexcept { const auto to_ref = [&](auto&& _t) { return Ref::make(std::move(_t)); }; @@ -600,7 +600,7 @@ struct Parser> { /// Expresses the variables as type T. static Result> read(const R& _r, - InputVarType* _var) noexcept { + const InputVarType& _var) noexcept { if (_r.is_empty(*_var)) { return std::shared_ptr(); } @@ -630,7 +630,7 @@ struct Parser> { /// Expresses the variables as type T. static Result> read( - const R& _r, InputVarType* _var) noexcept { + const R& _r, const InputVarType& _var) noexcept { const auto to_pair = [&](auto&& _t) { return std::make_pair(std::move(std::get<0>(_t)), std::move(std::get<1>(_t))); @@ -666,9 +666,9 @@ struct Parser> { using OutputVarType = typename W::OutputVarType; /// Expresses the variables as type T. - static ResultType read(const R& _r, InputVarType* _var) noexcept { + static ResultType read(const R& _r, const InputVarType& _var) noexcept { const auto get_disc = [&_r](auto _obj) { - return get_discriminator(_r, &_obj); + return get_discriminator(_r, _obj); }; const auto to_result = [&_r, _var](const std::string& _disc_value) { @@ -690,9 +690,9 @@ struct Parser> { private: template - static ResultType find_matching_alternative(const R& _r, - const std::string& _disc_value, - InputVarType* _var) noexcept { + static ResultType find_matching_alternative( + const R& _r, const std::string& _disc_value, + const InputVarType& _var) noexcept { if constexpr (_i == sizeof...(AlternativeTypes)) { return Error("Could not parse tagged union, could not match " + _discriminator.str() + " '" + _disc_value + "'."); @@ -726,7 +726,7 @@ struct Parser> { /// Retrieves the discriminator. static Result get_discriminator( - const R& _r, InputObjectType* _obj) noexcept { + const R& _r, const InputObjectType& _obj) noexcept { const auto embellish_error = [](const auto& _err) { return Error( "Could not parse tagged union: Could not find field '" + @@ -734,7 +734,7 @@ struct Parser> { }; const auto to_type = [&_r](auto _var) { - return _r.template to_basic_type(&_var); + return _r.template to_basic_type(_var); }; return _r.get_field(_discriminator.str(), _obj) @@ -771,8 +771,8 @@ struct Parser> { using OutputVarType = typename W::OutputVarType; static Result> read(const R& _r, - InputVarType* _var) noexcept { - const auto to_vec = [&](auto _arr) { return _r.to_vec(&_arr); }; + const InputVarType& _var) noexcept { + const auto to_vec = [&](auto _arr) { return _r.to_vec(_arr); }; const auto check_size = [](auto _vec) -> Result> { @@ -817,17 +817,17 @@ struct Parser> { std::move(_already_extracted)..., std::move(new_entry)); }; - return extract_single_field(_r, &_vec).and_then(extract_next); + return extract_single_field(_r, _vec).and_then(extract_next); } } /// Extracts a single field from a JSON. template - static auto extract_single_field(const R& _r, - std::vector* _vec) noexcept { + static auto extract_single_field( + const R& _r, const std::vector& _vec) noexcept { using NewFieldType = std::decay_t< typename std::tuple_element<_i, std::tuple>::type>; - return Parser::read(_r, &((*_vec)[_i])); + return Parser::read(_r, _vec[_i]); } /// Transforms a tuple to an array. @@ -856,8 +856,8 @@ struct Parser> { /// Expresses the variables as type T. static Result> read(const R& _r, - InputVarType* _var) noexcept { - if (_r.is_empty(*_var)) { + const InputVarType& _var) noexcept { + if (_r.is_empty(_var)) { return std::unique_ptr(); } const auto to_ptr = [](auto&& _t) { @@ -893,13 +893,13 @@ struct FieldVariantParser { using OutputVarType = typename W::OutputVarType; /// Expresses the variables as type T. - static ResultType read(const R& _r, InputVarType* _var) noexcept { + static ResultType read(const R& _r, const InputVarType& _var) noexcept { static_assert( internal::no_duplicate_field_names>(), "Externally tagged variants cannot have duplicate field " "names."); - const auto to_map = [&](auto _obj) { return _r.to_map(&_obj); }; + const auto to_map = [&](auto _obj) { return _r.to_map(_obj); }; const auto to_result = [&](auto _map) -> ResultType { if (_map.size() != 1) { @@ -910,8 +910,8 @@ struct FieldVariantParser { } const auto it = _map.begin(); const auto& disc_value = it->first; - auto& var = it->second; - return find_matching_alternative(_r, disc_value, &var); + const auto& var = it->second; + return find_matching_alternative(_r, disc_value, var); }; return _r.to_object(_var).transform(to_map).and_then(to_result); @@ -937,9 +937,9 @@ struct FieldVariantParser { private: template - static ResultType find_matching_alternative(const R& _r, - const std::string& _disc_value, - InputVarType* _var) noexcept { + static ResultType find_matching_alternative( + const R& _r, const std::string& _disc_value, + const InputVarType& _var) noexcept { if constexpr (_i == sizeof...(FieldTypes)) { return Error( "Could not parse std::variant, could not match field named " @@ -985,7 +985,7 @@ struct Parser> { /// Expresses the variables as type T. template static Result> read( - const R& _r, InputVarType* _var, + const R& _r, const InputVarType& _var, const std::string _errors = "") noexcept { if constexpr (_i == 0 && internal::all_fields>()) { @@ -999,7 +999,8 @@ struct Parser> { const auto try_next = [&_r, _var, &_errors](const auto& _err) { auto errors = _errors; - errors += std::string("\n -") + _err.what(); + errors += std::string("\n " + std::to_string(_i + 1) + ")") + + _err.what(); return read<_i + 1>(_r, _var, errors); }; @@ -1045,26 +1046,26 @@ struct VectorParser { using T = typename VecType::value_type; - static Result read(const R& _r, InputVarType* _var) noexcept { + static Result read(const R& _r, + const InputVarType& _var) noexcept { using namespace std::ranges::views; const auto get_elem = [&](auto& _v) { - return Parser>::read(_r, &_v).value(); + return Parser>::read(_r, _v).value(); }; const auto get_pair = [&](auto& _v) { if constexpr (is_map_like()) { using K = std::decay_t; using V = std::decay_t; - return Parser>>::read(_r, - &_v) + return Parser>>::read(_r, _v) .value(); } }; const auto to_container = [&](InputArrayType&& _arr) -> Result { - auto input_vars = _r.to_vec(&_arr); + auto input_vars = _r.to_vec(_arr); VecType vec; if constexpr (is_forward_list()) { for (auto it = input_vars.rbegin(); it != input_vars.rend();