Merge pull request #31 from ecatmur/std-tie

Serialization of std::reference_wrapper
This commit is contained in:
liuzicheng1987
2023-12-16 11:29:25 +01:00
committed by GitHub
6 changed files with 521 additions and 1352 deletions
File diff suppressed because it is too large Load Diff
+26
View File
@@ -9,6 +9,7 @@
#include <deque>
#include <exception>
#include <forward_list>
#include <functional>
#include <list>
#include <map>
#include <memory>
@@ -180,6 +181,31 @@ struct Parser<R, W, T*> {
// ----------------------------------------------------------------------------
template <class R, class W, class T>
requires AreReaderAndWriter<R, W, std::reference_wrapper<T>>
struct Parser<R, W, std::reference_wrapper<T>> {
using InputVarType = typename R::InputVarType;
using OutputVarType = typename W::OutputVarType;
static Result<std::reference_wrapper<T>> read(
const R& _r, const InputVarType& _var) noexcept {
static_assert(always_false_v<T>,
"Reading into std::reference_wrapper is dangerous and "
"therefore unsupported. "
"Please consider using std::unique_ptr, rfl::Box, "
"std::shared_ptr, or"
"rfl::Ref instead.");
return Error("Unsupported.");
}
static OutputVarType write(const W& _w,
const std::reference_wrapper<T> _ref) noexcept {
return Parser<R, W, std::decay_t<T>>::write(_w, _ref.get());
}
};
// ----------------------------------------------------------------------------
template <class R, class W, class T, size_t _size>
requires AreReaderAndWriter<R, W, std::array<T, _size>>
struct Parser<R, W, std::array<T, _size>> {
+1
View File
@@ -60,6 +60,7 @@ add_executable(
"test_save_load.cpp"
"test_set.cpp"
"test_size.cpp"
"test_std_ref.cpp"
"test_string_map.cpp"
"test_string_unordered_map.cpp"
"test_tagged_union.cpp"
+35
View File
@@ -0,0 +1,35 @@
#include "test_std_ref.hpp"
#include <functional>
#include <iostream>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <source_location>
#include <string>
namespace test_std_ref {
struct StdRefStruct {
std::reference_wrapper<int> a;
};
void test() {
std::cout << std::source_location::current().function_name() << std::endl;
int i = 10;
StdRefStruct struct_ = {.a = std::ref(i)};
const auto json_string = rfl::json::write(struct_);
const std::string& expected = R"({"a":10})";
if (json_string != expected) {
std::cout << "Test failed on write. Expected:" << std::endl
<< expected << std::endl
<< "Got: " << std::endl
<< json_string << std::endl
<< std::endl;
return;
}
std::cout << "OK" << std::endl << std::endl;
}
} // namespace test_std_ref
+3
View File
@@ -0,0 +1,3 @@
namespace test_std_ref {
void test();
}
+2
View File
@@ -48,6 +48,7 @@
#include "test_save_load.hpp"
#include "test_set.hpp"
#include "test_size.hpp"
#include "test_std_ref.hpp"
#include "test_string_map.hpp"
#include "test_string_unordered_map.hpp"
#include "test_tagged_union.hpp"
@@ -93,6 +94,7 @@ int main() {
test_unordered_multimap::test();
test_multiset::test();
test_unordered_multiset::test();
test_std_ref::test();
test_string_map::test();
test_string_unordered_map::test();
test_alphanumeric_map::test();