From 2c86d80626ad9eb10eb30851e863bea65db7fc00 Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Tue, 3 Oct 2023 21:35:25 +0200 Subject: [PATCH] Added support for std::array --- README.md | 2 +- include/rfl/parsing/Parser.hpp | 73 ++++++++++++++++++++++++++++++++++ tests/test_array.hpp | 37 +++++++++++++++++ tests/tests.cpp | 2 + 4 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 tests/test_array.hpp diff --git a/README.md b/README.md index 0dbfb060..ebe2724e 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ std::cout << "Hello, my name is " << homer2.first_name() << " " reflect-cpp supports the following containers from the C++ standard library: -- `std::array` (TODO) +- `std::array` - `std::deque` - `std::forward_list` - `std::map` diff --git a/include/rfl/parsing/Parser.hpp b/include/rfl/parsing/Parser.hpp index 305a5baa..a6a28090 100644 --- a/include/rfl/parsing/Parser.hpp +++ b/include/rfl/parsing/Parser.hpp @@ -1,6 +1,7 @@ #ifndef RFL_PARSING_PARSER_HPP_ #define RFL_PARSING_PARSER_HPP_ +#include #include #include #include @@ -113,6 +114,78 @@ struct Parser { // ---------------------------------------------------------------------------- +template +struct Parser> { + public: + using InputArrayType = typename ReaderType::InputArrayType; + using InputVarType = typename ReaderType::InputVarType; + + using OutputArrayType = typename WriterType::OutputArrayType; + using OutputVarType = typename WriterType::OutputVarType; + + static Result> read(const ReaderType& _r, + InputVarType* _var) noexcept { + const auto to_vec = [&](auto _arr) { return _r.to_vec(&_arr); }; + + const auto check_size = + [](auto _vec) -> Result> { + if (_vec.size() != _size) { + return Error("Expected " + std::to_string(_size) + + " fields, got " + std::to_string(_vec.size()) + + "."); + } + return std::move(_vec); + }; + + const auto extract = [&_r](auto _vec) { + return extract_field_by_field(_r, std::move(_vec)); + }; + + return _r.to_array(_var) + .transform(to_vec) + .and_then(check_size) + .and_then(extract); + } + + static OutputVarType write(const WriterType& _w, + const std::array& _arr) noexcept { + auto arr = _w.new_array(); + for (auto it = _arr.begin(); it != _arr.end(); ++it) { + _w.add( + Parser>::write(_w, *it), + &arr); + } + return OutputVarType(arr); + } + + private: + /// Extracts values from the array, field by field. + template + static Result> extract_field_by_field( + const ReaderType& _r, std::vector _vec, + const AlreadyExtracted&... _already_extracted) noexcept { + constexpr size_t i = sizeof...(AlreadyExtracted); + if constexpr (i == _size) { + return std::array({_already_extracted...}); + } else { + const auto extract_next = [&](auto new_entry) { + return extract_field_by_field(_r, std::move(_vec), + _already_extracted..., new_entry); + }; + return extract_single_field(_r, &_vec).and_then(extract_next); + } + } + + /// Extracts a single field from a JSON. + template + static auto extract_single_field(const ReaderType& _r, + std::vector* _vec) noexcept { + return Parser::read(_r, &((*_vec)[_i])); + } +}; + +// ---------------------------------------------------------------------------- + template struct Parser> { using InputVarType = typename ReaderType::InputVarType; diff --git a/tests/test_array.hpp b/tests/test_array.hpp new file mode 100644 index 00000000..827ebfe6 --- /dev/null +++ b/tests/test_array.hpp @@ -0,0 +1,37 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_array() { + std::cout << "test_array" << std::endl; + + struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name = "Simpson"; + rfl::Field<"children", std::shared_ptr>> + children = rfl::default_value; + }; + + const auto bart = Person{.first_name = "Bart"}; + + const auto lisa = Person{.first_name = "Lisa"}; + + const auto maggie = Person{.first_name = "Maggie"}; + + const auto homer = + Person{.first_name = "Homer", + .children = std::make_shared>( + std::array{bart, lisa, maggie})}; + + write_and_read( + homer, + "{\"firstName\":\"Homer\",\"lastName\":\"Simpson\",\"children\":[{" + "\"firstName\":\"Bart\",\"lastName\":\"Simpson\"},{" + "\"firstName\":\"Lisa\",\"lastName\":\"Simpson\"},{" + "\"firstName\":\"Maggie\",\"lastName\":\"Simpson\"}]}"); +} diff --git a/tests/tests.cpp b/tests/tests.cpp index 952bf5b2..a0ec62ba 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -1,3 +1,4 @@ +#include "test_array.hpp" #include "test_custom_class1.hpp" #include "test_custom_class2.hpp" #include "test_custom_class3.hpp" @@ -22,6 +23,7 @@ int main() { test_custom_class1(); test_custom_class2(); test_custom_class3(); + test_array(); return 0; }