diff --git a/include/rfl/NamedTuple.hpp b/include/rfl/NamedTuple.hpp index 14f7f811..d1dd877d 100644 --- a/include/rfl/NamedTuple.hpp +++ b/include/rfl/NamedTuple.hpp @@ -149,17 +149,6 @@ class NamedTuple { } } - /// Template specialization for std::tuple, so we can pass fields from other - /// named tuples. - template - auto add(const std::tuple& _tuple, Tail&&... _tail) const { - if constexpr (sizeof...(Tail) > 0) { - return add_tuple(_tuple).add(std::forward(_tail)...); - } else { - return add_tuple(std::forward>(_tuple)); - } - } - /// Template specialization for NamedTuple, so we can pass fields from other /// named tuples. template @@ -178,21 +167,6 @@ class NamedTuple { std::forward(_tail)...); } - /// Template specialization for NamedTuple, so we can pass fields from other - /// named tuples. - template - auto add(const NamedTuple& _named_tuple, Tail&&... _tail) { - return add(_named_tuple.fields(), std::forward(_tail)...); - } - - /// Template specialization for NamedTuple, so we can pass fields from other - /// named tuples. - template - auto add(const NamedTuple& _named_tuple, - Tail&&... _tail) const { - return add(_named_tuple.fields(), std::forward(_tail)...); - } - /// Returns a tuple containing the fields. Fields fields() { return make_fields(); } @@ -345,7 +319,7 @@ class NamedTuple { template constexpr auto add_tuple(std::tuple&& _tuple) { const auto a = [this](auto&&... _fields) { - return add(std::forward(_fields)...); + return this->add(std::forward(_fields)...); }; return std::apply(a, std::forward>(_tuple)); } @@ -355,7 +329,7 @@ class NamedTuple { template constexpr auto add_tuple(std::tuple&& _tuple) const { const auto a = [this](auto&&... _fields) { - return add(std::forward(_fields)...); + return this->add(std::forward(_fields)...); }; return std::apply(a, std::forward>(_tuple)); } @@ -458,7 +432,7 @@ class NamedTuple { template auto replace_tuple(std::tuple&& _tuple) { const auto r = [this](auto&&... _fields) { - return replace(std::forward(_fields)...); + return this->replace(std::forward(_fields)...); }; return std::apply(r, std::forward>(_tuple)); } @@ -468,7 +442,7 @@ class NamedTuple { template auto replace_tuple(std::tuple&& _tuple) const { const auto r = [this](auto&&... _fields) { - return replace(std::forward(_fields)...); + return this->replace(std::forward(_fields)...); }; return std::apply(r, std::forward>(_tuple)); } diff --git a/include/rfl/Result.hpp b/include/rfl/Result.hpp index d12b6b19..9400e20e 100644 --- a/include/rfl/Result.hpp +++ b/include/rfl/Result.hpp @@ -144,7 +144,7 @@ class Result { /// them within a std::range. const T* end() const noexcept { const auto get_ptr = - [this](const TOrError& _t_or_err) -> const T* { + [](const TOrError& _t_or_err) -> const T* { if constexpr (!std::is_same()) { return &_t_or_err + 1; } else { @@ -158,7 +158,7 @@ class Result { /// or std::nullopt otherwise. std::optional error() const noexcept { const auto get_err = - [this]( + []( const TOrError& _t_or_err) -> std::optional { if constexpr (!std::is_same()) { return std::nullopt; @@ -284,11 +284,11 @@ class Result { /// Returns the value if the result does not contain an error, throws an /// exceptions if not. Similar to .unwrap() in Rust. - T& value() { + T value() { const auto handle_variant = - [&](TOrError& _t_or_err) -> T& { + [&](TOrError& _t_or_err) -> T { if constexpr (!std::is_same()) { - return _t_or_err; + return std::forward(_t_or_err); } else { throw std::runtime_error(_t_or_err.what()); } @@ -310,6 +310,19 @@ class Result { return std::visit(handle_variant, t_or_err_); } + /// Returns the value or a default. + T value_or(const T& _default) noexcept { + const auto handle_variant = + [&](const TOrError& _t_or_err) -> T { + if constexpr (!std::is_same()) { + return std::forward(_t_or_err); + } else { + return _default; + } + }; + return std::visit(handle_variant, t_or_err_); + } + /// Returns the value or a default. T value_or(const T& _default) const noexcept { const auto handle_variant = diff --git a/include/rfl/internal/move_from_named_tuple.hpp b/include/rfl/internal/move_from_named_tuple.hpp index 8f09a016..821c5c0b 100644 --- a/include/rfl/internal/move_from_named_tuple.hpp +++ b/include/rfl/internal/move_from_named_tuple.hpp @@ -50,7 +50,7 @@ template auto move_from_pointers(Pointers& _ptrs, Args&&... _args) { constexpr auto i = sizeof...(Args); if constexpr (i == std::tuple_size_v>) { - return T(std::move(_args)...); + return T{std::move(_args)...}; } else { using FieldType = std::tuple_element_t>; diff --git a/include/rfl/json/Reader.hpp b/include/rfl/json/Reader.hpp index 7a3e5ba8..03fd8bfe 100644 --- a/include/rfl/json/Reader.hpp +++ b/include/rfl/json/Reader.hpp @@ -18,34 +18,19 @@ namespace rfl { namespace json { -struct YYJSONOutputArray { - yyjson_mut_val* val_; -}; - -struct YYJSONOutputObject { - yyjson_mut_val* val_; -}; - -struct YYJSONOutputVar { - YYJSONOutputVar(yyjson_mut_val* _val) : val_(_val) {} - - YYJSONOutputVar(YYJSONOutputArray _arr) : val_(_arr.val_) {} - - YYJSONOutputVar(YYJSONOutputObject _obj) : val_(_obj.val_) {} - - yyjson_mut_val* val_; -}; - struct Reader { struct YYJSONInputArray { + YYJSONInputArray(yyjson_val* _val) : val_(_val) {} yyjson_val* val_; }; struct YYJSONInputObject { + YYJSONInputObject(yyjson_val* _val) : val_(_val) {} yyjson_val* val_; }; struct YYJSONInputVar { + YYJSONInputVar(yyjson_val* _val) : val_(_val) {} yyjson_val* val_; }; diff --git a/include/rfl/json/Writer.hpp b/include/rfl/json/Writer.hpp index 2bc1e83d..bd26e0fe 100644 --- a/include/rfl/json/Writer.hpp +++ b/include/rfl/json/Writer.hpp @@ -19,10 +19,12 @@ namespace json { struct Writer { struct YYJSONOutputArray { + YYJSONOutputArray(yyjson_mut_val* _val) : val_(_val) {} yyjson_mut_val* val_; }; struct YYJSONOutputObject { + YYJSONOutputObject(yyjson_mut_val* _val) : val_(_val) {} yyjson_mut_val* val_; }; diff --git a/include/rfl/parsing/Parser.hpp b/include/rfl/parsing/Parser.hpp index fda9f122..d0769427 100644 --- a/include/rfl/parsing/Parser.hpp +++ b/include/rfl/parsing/Parser.hpp @@ -221,7 +221,7 @@ struct Parser> { /// Expresses the variables as type T. static Result> read(const ReaderType& _r, InputVarType* _var) noexcept { - const auto to_box = [&](auto&& _t) { + const auto to_box = [](auto&& _t) { return Box::make(std::move(_t)); }; return Parser>::read(_r, _var) @@ -444,9 +444,7 @@ struct Parser> { if (_r.is_empty(_var)) { return std::optional(); } - const auto to_opt = [&_r](auto&& _t) { - return std::make_optional(_t); - }; + const auto to_opt = [](auto&& _t) { return std::make_optional(_t); }; return Parser>::read(_r, _var) .transform(to_opt); } @@ -922,7 +920,8 @@ struct Parser> { // ---------------------------------------------------------------------------- /// This can be used for data structures that would be expressed as array in -/// serialized format (std::vector, std::set, std::deque, ...). +/// serialized format (std::vector, std::set, std::deque, ...), +/// but also includes map-like types, when the key is not of type std::string. template struct VectorParser { public: @@ -938,23 +937,32 @@ struct VectorParser { InputVarType* _var) noexcept { using namespace std::ranges::views; - const auto get_result = [&](auto& _v) { - return std::move( - Parser>::read(_r, &_v) - .value()); + const auto get_elem = [&](auto& _v) { + return Parser>::read(_r, + &_v) + .value(); }; - const auto to_vec = [&](InputArrayType _arr) -> Result { + const auto to_container = + [&](InputArrayType&& _arr) -> Result { auto input_vars = _r.to_vec(&_arr); - auto range = input_vars | transform(get_result); + VecType vec; + for (auto it = input_vars.begin(); it != input_vars.end(); ++it) { + vec.emplace_back(get_elem(*it)); + } + return vec; + }; + + const auto to_result = [&](InputArrayType&& _arr) -> Result { try { - return Result(VecType(range.begin(), range.end())); + return Result( + to_container(std::forward(_arr))); } catch (std::exception& e) { return Error(e.what()); } }; - return _r.to_array(_var).and_then(to_vec); + return _r.to_array(_var).and_then(to_result); } static OutputVarType write(const WriterType& _w, diff --git a/tests/test_box.hpp b/tests/test_box.hpp index 1ff2730c..9317efdd 100644 --- a/tests/test_box.hpp +++ b/tests/test_box.hpp @@ -32,10 +32,10 @@ void test_box() { auto leaf2 = DecisionTree::Leaf{.value = 5.0}; - auto node = - DecisionTree::Node{.critical_value = 10.0, - .lesser = rfl::make_box(leaf1), - .greater = rfl::make_box(leaf2)}; + auto node = DecisionTree::Node{ + .critical_value = 10.0, + .lesser = rfl::make_box(DecisionTree{leaf1}), + .greater = rfl::make_box(DecisionTree{leaf2})}; const DecisionTree tree{.leaf_or_node = std::move(node)}; diff --git a/tests/test_ref.hpp b/tests/test_ref.hpp index 8c353f5d..b7fe3e90 100644 --- a/tests/test_ref.hpp +++ b/tests/test_ref.hpp @@ -32,10 +32,10 @@ void test_ref() { const auto leaf2 = DecisionTree::Leaf{.value = 5.0}; - auto node = - DecisionTree::Node{.critical_value = 10.0, - .lesser = rfl::make_ref(leaf1), - .greater = rfl::make_ref(leaf2)}; + auto node = DecisionTree::Node{ + .critical_value = 10.0, + .lesser = rfl::make_ref(DecisionTree{leaf1}), + .greater = rfl::make_ref(DecisionTree{leaf2})}; const DecisionTree tree{.leaf_or_node = std::move(node)}; diff --git a/tests/test_unique_ptr2.hpp b/tests/test_unique_ptr2.hpp index aae61b15..4fab614c 100644 --- a/tests/test_unique_ptr2.hpp +++ b/tests/test_unique_ptr2.hpp @@ -32,10 +32,10 @@ void test_unique_ptr2() { auto leaf2 = DecisionTree::Leaf{.value = 5.0}; - auto node = - DecisionTree::Node{.critical_value = 10.0, - .lesser = std::make_unique(leaf1), - .greater = std::make_unique(leaf2)}; + auto node = DecisionTree::Node{ + .critical_value = 10.0, + .lesser = std::make_unique(DecisionTree{leaf1}), + .greater = std::make_unique(DecisionTree{leaf2})}; const DecisionTree tree{.leaf_or_node = std::move(node)};