Added support for std::array

This commit is contained in:
Patrick Urbanke
2023-10-03 21:35:25 +02:00
parent 58d1765abc
commit 2c86d80626
4 changed files with 113 additions and 1 deletions
+1 -1
View File
@@ -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`
+73
View File
@@ -1,6 +1,7 @@
#ifndef RFL_PARSING_PARSER_HPP_
#define RFL_PARSING_PARSER_HPP_
#include <array>
#include <cstddef>
#include <deque>
#include <exception>
@@ -113,6 +114,78 @@ struct Parser {
// ----------------------------------------------------------------------------
template <class ReaderType, class WriterType, class T, size_t _size>
struct Parser<ReaderType, WriterType, std::array<T, _size>> {
public:
using InputArrayType = typename ReaderType::InputArrayType;
using InputVarType = typename ReaderType::InputVarType;
using OutputArrayType = typename WriterType::OutputArrayType;
using OutputVarType = typename WriterType::OutputVarType;
static Result<std::array<T, _size>> 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<std::vector<InputVarType>> {
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<T, _size>& _arr) noexcept {
auto arr = _w.new_array();
for (auto it = _arr.begin(); it != _arr.end(); ++it) {
_w.add(
Parser<ReaderType, WriterType, std::decay_t<T>>::write(_w, *it),
&arr);
}
return OutputVarType(arr);
}
private:
/// Extracts values from the array, field by field.
template <class... AlreadyExtracted>
static Result<std::array<T, _size>> extract_field_by_field(
const ReaderType& _r, std::vector<InputVarType> _vec,
const AlreadyExtracted&... _already_extracted) noexcept {
constexpr size_t i = sizeof...(AlreadyExtracted);
if constexpr (i == _size) {
return std::array<T, _size>({_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<i>(_r, &_vec).and_then(extract_next);
}
}
/// Extracts a single field from a JSON.
template <int _i>
static auto extract_single_field(const ReaderType& _r,
std::vector<InputVarType>* _vec) noexcept {
return Parser<ReaderType, WriterType, T>::read(_r, &((*_vec)[_i]));
}
};
// ----------------------------------------------------------------------------
template <class ReaderType, class WriterType, class T>
struct Parser<ReaderType, WriterType, Box<T>> {
using InputVarType = typename ReaderType::InputVarType;
+37
View File
@@ -0,0 +1,37 @@
#include <array>
#include <iostream>
#include <memory>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <string>
#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<std::array<Person, 3>>>
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<Person, 3>>(
std::array<Person, 3>{bart, lisa, maggie})};
write_and_read(
homer,
"{\"firstName\":\"Homer\",\"lastName\":\"Simpson\",\"children\":[{"
"\"firstName\":\"Bart\",\"lastName\":\"Simpson\"},{"
"\"firstName\":\"Lisa\",\"lastName\":\"Simpson\"},{"
"\"firstName\":\"Maggie\",\"lastName\":\"Simpson\"}]}");
}
+2
View File
@@ -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;
}