mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 00:26:19 +08:00
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.
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user