Files
reflect-cpp/tests/json/test_std_ref.cpp
T
Ed Catmur f429bd994a Serialization of std::reference_wrapper
Only for serialization - similarly to pointers, this should not be used for deserialization.

As discussed https://github.com/getml/reflect-cpp/pull/20#issuecomment-1857491501 this requires changing the implementation of bind_to_tuple, since std::ref() collapses on a std::reference_wrapper.
2023-12-16 05:31:13 +00:00

36 lines
837 B
C++

#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