mirror of
https://github.com/fraillt/bitsery.git
synced 2026-06-08 08:13:56 +00:00
extensions for tuple, variant and chrono types
This commit is contained in:
@@ -20,7 +20,7 @@
|
||||
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
#SOFTWARE.
|
||||
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
project(bitsery_tests CXX)
|
||||
|
||||
find_package(GTest 1.8 REQUIRED)
|
||||
@@ -31,11 +31,6 @@ endif()
|
||||
|
||||
file(GLOB TestSourceFiles ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
|
||||
|
||||
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
|
||||
message(WARNING "extension tests for optional is disable for VS, because VS currenty doesn't have <optional>")
|
||||
list(REMOVE_ITEM TestSourceFiles ${CMAKE_CURRENT_SOURCE_DIR}/serialization_ext_std_optional.cpp)
|
||||
endif()
|
||||
|
||||
enable_testing()
|
||||
|
||||
foreach (TestFile ${TestSourceFiles})
|
||||
@@ -46,8 +41,9 @@ foreach (TestFile ${TestSourceFiles})
|
||||
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
|
||||
target_compile_options(${TestName} PRIVATE -Wextra -Wno-missing-braces -Wpedantic -Weffc++ -Wno-c++14-extensions)
|
||||
endif()
|
||||
gtest_discover_tests(${TestName})
|
||||
|
||||
add_test(NAME ${TestName} COMMAND $<TARGET_FILE:${TestName}>)
|
||||
# add_test(NAME ${TestName} COMMAND $<TARGET_FILE:${TestName}>)
|
||||
endforeach()
|
||||
|
||||
#======================= setup development environment ====================
|
||||
|
||||
@@ -35,6 +35,17 @@
|
||||
#include <bitsery/flexible/set.h>
|
||||
#include <bitsery/flexible/unordered_set.h>
|
||||
#include <bitsery/flexible/memory.h>
|
||||
#include <bitsery/flexible/chrono.h>
|
||||
#if __cplusplus > 201402L
|
||||
#include <bitsery/flexible/tuple.h>
|
||||
#include <bitsery/flexible/variant.h>
|
||||
#else
|
||||
#if defined(_MSC_VER)
|
||||
#pragma message("tuple and variant only works with c++17")
|
||||
#else
|
||||
#warning "tuple and variant only works with c++17"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include "serialization_test_utils.h"
|
||||
@@ -388,6 +399,33 @@ TEST(FlexibleSyntax, StdSmartPtr) {
|
||||
EXPECT_THAT(*resUnique1, Eq(*dataUnique1));
|
||||
}
|
||||
|
||||
TEST(FlexibleSyntax, StdDuration) {
|
||||
std::chrono::duration<int64_t, std::milli> t1{54654};
|
||||
EXPECT_TRUE(procArchive(t1) == t1);
|
||||
}
|
||||
|
||||
TEST(FlexibleSyntax, StdTimePoint) {
|
||||
using Duration = std::chrono::duration<double, std::milli>;
|
||||
using TP = std::chrono::time_point<std::chrono::system_clock, Duration>;
|
||||
|
||||
TP data{Duration{874656.4798}};
|
||||
EXPECT_TRUE(procArchive(data) == data);
|
||||
}
|
||||
|
||||
#if __cplusplus > 201402L
|
||||
|
||||
TEST(FlexibleSyntax, StdTuple) {
|
||||
std::tuple<int, std::string, std::vector<char>> t1{5,"hello hello", {'A','B','C'}};
|
||||
EXPECT_TRUE(procArchive(t1) == t1);
|
||||
}
|
||||
|
||||
TEST(FlexibleSyntax, StdVariant) {
|
||||
std::variant<float, std::string, std::chrono::milliseconds> t1{std::string("hello hello")};
|
||||
EXPECT_TRUE(procArchive(t1) == t1);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
TEST(FlexibleSyntax, NestedTypes) {
|
||||
std::unordered_map<std::string, std::vector<std::string>> t1;
|
||||
t1.emplace("my key", std::vector<std::string>{"very", "nice", "string"});
|
||||
|
||||
@@ -139,6 +139,24 @@ TEST(DeserializeNonDefaultConstructible, ResultStdForwardListShouldShrink) {
|
||||
EXPECT_THAT(res.begin(), Eq(res.end()));
|
||||
|
||||
}
|
||||
|
||||
{
|
||||
// also check if correctly expands if source is bigger than destination
|
||||
SerializationContext ctx{};
|
||||
std::forward_list<NonDefaultConstructible> data{};
|
||||
data.push_front(NonDefaultConstructible{1});
|
||||
data.push_front(NonDefaultConstructible{14});
|
||||
std::forward_list<NonDefaultConstructible> res{};
|
||||
|
||||
ctx.createSerializer().container(data, 10);
|
||||
ctx.createDeserializer().container(res, 10);
|
||||
|
||||
auto resIt = res.begin();
|
||||
for (auto it = data.begin(); it != data.end(); ++it, ++resIt) {
|
||||
EXPECT_THAT(*resIt, Eq(*it));
|
||||
}
|
||||
EXPECT_THAT(resIt, Eq(res.end()));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(DeserializeNonDefaultConstructible, StdSet) {
|
||||
|
||||
@@ -26,7 +26,6 @@
|
||||
#include "serialization_test_utils.h"
|
||||
|
||||
#include <bitsery/traits/array.h>
|
||||
#include <iostream>
|
||||
#include <bitset>
|
||||
#include <chrono>
|
||||
|
||||
|
||||
81
tests/serialization_ext_std_chrono.cpp
Normal file
81
tests/serialization_ext_std_chrono.cpp
Normal file
@@ -0,0 +1,81 @@
|
||||
//MIT License
|
||||
//
|
||||
//Copyright (c) 2019 Mindaugas Vinkelis
|
||||
//
|
||||
//Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
//of this software and associated documentation files (the "Software"), to deal
|
||||
//in the Software without restriction, including without limitation the rights
|
||||
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
//copies of the Software, and to permit persons to whom the Software is
|
||||
//furnished to do so, subject to the following conditions:
|
||||
//
|
||||
//The above copyright notice and this permission notice shall be included in all
|
||||
//copies or substantial portions of the Software.
|
||||
//
|
||||
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
//SOFTWARE.
|
||||
|
||||
#include <bitsery/ext/std_chrono.h>
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include "serialization_test_utils.h"
|
||||
|
||||
using StdDuration = bitsery::ext::StdDuration;
|
||||
using StdTimePoint = bitsery::ext::StdTimePoint;
|
||||
|
||||
using testing::Eq;
|
||||
|
||||
TEST(SerializeExtensionStdChrono, IntegralDuration) {
|
||||
SerializationContext ctx1;
|
||||
using Hours = std::chrono::duration<int32_t, std::ratio<60>>;
|
||||
|
||||
Hours data{43};
|
||||
Hours res{};
|
||||
|
||||
ctx1.createSerializer().ext4b(data, StdDuration{});
|
||||
ctx1.createDeserializer().ext4b(res, StdDuration{});
|
||||
EXPECT_THAT(res, Eq(data));
|
||||
}
|
||||
|
||||
TEST(SerializeExtensionStdChrono, IntegralTimePoint) {
|
||||
SerializationContext ctx1;
|
||||
using Duration = std::chrono::duration<int64_t, std::milli>;
|
||||
using TP = std::chrono::time_point<std::chrono::system_clock, Duration>;
|
||||
|
||||
TP data{Duration{243}};
|
||||
TP res{};
|
||||
|
||||
ctx1.createSerializer().ext8b(data, StdTimePoint{});
|
||||
ctx1.createDeserializer().ext8b(res, StdTimePoint{});
|
||||
EXPECT_THAT(res, Eq(data));
|
||||
}
|
||||
|
||||
TEST(SerializeExtensionStdChrono, FloatDuration) {
|
||||
SerializationContext ctx1;
|
||||
using Hours = std::chrono::duration<float, std::ratio<60>>;
|
||||
|
||||
Hours data{43.5f};
|
||||
Hours res{};
|
||||
|
||||
ctx1.createSerializer().ext4b(data, StdDuration{});
|
||||
ctx1.createDeserializer().ext4b(res, StdDuration{});
|
||||
EXPECT_THAT(res, Eq(data));
|
||||
}
|
||||
|
||||
TEST(SerializeExtensionStdChrono, FloatTimePoint) {
|
||||
SerializationContext ctx1;
|
||||
using Duration = std::chrono::duration<double, std::milli>;
|
||||
using TP = std::chrono::time_point<std::chrono::system_clock, Duration>;
|
||||
|
||||
TP data{Duration{243457.4}};
|
||||
TP res{};
|
||||
|
||||
ctx1.createSerializer().ext8b(data, StdTimePoint{});
|
||||
ctx1.createDeserializer().ext8b(res, StdTimePoint{});
|
||||
EXPECT_THAT(res, Eq(data));
|
||||
}
|
||||
@@ -26,11 +26,9 @@
|
||||
|
||||
#if __cplusplus > 201402L
|
||||
|
||||
#include <optional>
|
||||
#include <bitsery/ext/std_optional.h>
|
||||
#include <bitsery/ext/value_range.h>
|
||||
|
||||
|
||||
using StdOptional = bitsery::ext::StdOptional;
|
||||
|
||||
using BPSer = SerializationContext::TSerializer::BPEnabledType;
|
||||
@@ -122,6 +120,10 @@ TEST(SerializeExtensionStdOptional, NoAlignAfterStateWriteRead) {
|
||||
EXPECT_THAT(t1.value(), Eq(r1.value()));
|
||||
}
|
||||
|
||||
|
||||
|
||||
#else
|
||||
#if defined(_MSC_VER)
|
||||
#pragma message("Tests for StdOptional requires C++17")
|
||||
#else
|
||||
#warning "Tests for StdOptional requires C++17"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
129
tests/serialization_ext_std_tuple.cpp
Normal file
129
tests/serialization_ext_std_tuple.cpp
Normal file
@@ -0,0 +1,129 @@
|
||||
//MIT License
|
||||
//
|
||||
//Copyright (c) 2019 Mindaugas Vinkelis
|
||||
//
|
||||
//Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
//of this software and associated documentation files (the "Software"), to deal
|
||||
//in the Software without restriction, including without limitation the rights
|
||||
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
//copies of the Software, and to permit persons to whom the Software is
|
||||
//furnished to do so, subject to the following conditions:
|
||||
//
|
||||
//The above copyright notice and this permission notice shall be included in all
|
||||
//copies or substantial portions of the Software.
|
||||
//
|
||||
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
//SOFTWARE.
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include "serialization_test_utils.h"
|
||||
|
||||
using testing::Eq;
|
||||
|
||||
#if __cplusplus > 201402L
|
||||
|
||||
#include <bitsery/ext/std_tuple.h>
|
||||
|
||||
template<typename T, size_t N>
|
||||
using OverloadValue = bitsery::ext::OverloadValue<T, N>;
|
||||
|
||||
TEST(SerializeExtensionStdTuple, UseDefaultSerializeFunction) {
|
||||
std::tuple<MyStruct1, MyStruct2> t1{MyStruct1{-789, 45}, MyStruct2{MyStruct2::MyEnum::V3, MyStruct1{}}};
|
||||
std::tuple<MyStruct1, MyStruct2> r1{};
|
||||
SerializationContext ctx;
|
||||
ctx.createSerializer().ext(t1, bitsery::ext::StdTuple{});
|
||||
ctx.createDeserializer().ext(r1, bitsery::ext::StdTuple{});
|
||||
EXPECT_THAT(t1, Eq(r1));
|
||||
}
|
||||
|
||||
TEST(SerializeExtensionStdTuple, ValueTypesCanBeSerializedWithLambdaAndOrCallableObject) {
|
||||
std::tuple<float, int32_t> t1{123.456f, -898754656};
|
||||
std::tuple<float, int32_t> r1{};
|
||||
SerializationContext ctx;
|
||||
auto exec = [](auto& s, auto& o) {
|
||||
using S = decltype(s);
|
||||
s.ext(o, bitsery::ext::StdTuple{
|
||||
[](auto& s1, float& o1) {
|
||||
s1.value4b(o1);
|
||||
},
|
||||
OverloadValue<int32_t, 4>{}
|
||||
});
|
||||
};
|
||||
exec(ctx.createSerializer(), t1);
|
||||
exec(ctx.createDeserializer(), r1);
|
||||
EXPECT_THAT(t1, Eq(r1));
|
||||
}
|
||||
|
||||
TEST(SerializeExtensionStdTuple, CanOverloadDefaultSerializeFunction) {
|
||||
std::tuple<MyStruct1, MyStruct2> t1{MyStruct1{-789, 45}, MyStruct2{MyStruct2::MyEnum::V3, MyStruct1{}}};
|
||||
std::tuple<MyStruct1, MyStruct2> r1{};
|
||||
SerializationContext ctx;
|
||||
auto exec = [](auto& s, auto& o) {
|
||||
using S = decltype(s);
|
||||
s.ext(o, bitsery::ext::StdTuple{
|
||||
[](auto& s1, MyStruct1& o1) {
|
||||
s1.value4b(o1.i1);
|
||||
//do not serialize other element, it should be 0 (default)
|
||||
},
|
||||
});
|
||||
};
|
||||
exec(ctx.createSerializer(), t1);
|
||||
exec(ctx.createDeserializer(), r1);
|
||||
EXPECT_THAT(std::get<1>(t1), Eq(std::get<1>(r1)));
|
||||
EXPECT_THAT(std::get<0>(t1).i1, Eq(std::get<0>(r1).i1));
|
||||
EXPECT_THAT(std::get<0>(t1).i2, ::testing::Ne(std::get<0>(r1).i2));
|
||||
}
|
||||
|
||||
TEST(SerializeExtensionStdTuple, EmptyTuple) {
|
||||
std::tuple<> t1{};
|
||||
std::tuple<> r1{};
|
||||
SerializationContext ctx;
|
||||
ctx.createSerializer().ext(t1, bitsery::ext::StdTuple{});
|
||||
ctx.createDeserializer().ext(r1, bitsery::ext::StdTuple{});
|
||||
EXPECT_THAT(t1, Eq(r1));
|
||||
}
|
||||
|
||||
struct NonDefaultConstructable {
|
||||
explicit NonDefaultConstructable(float x) : _x{x} {}
|
||||
|
||||
float _x;
|
||||
|
||||
bool operator==(const NonDefaultConstructable& rhs) const {
|
||||
return _x == rhs._x;
|
||||
}
|
||||
|
||||
private:
|
||||
friend class bitsery::Access;
|
||||
|
||||
NonDefaultConstructable() : _x{0.0f} {};
|
||||
};
|
||||
|
||||
TEST(SerializeExtensionStdTuple, NonDefaultConstructable) {
|
||||
std::tuple<NonDefaultConstructable> t1{34};
|
||||
std::tuple<NonDefaultConstructable> r1{8};
|
||||
SerializationContext ctx;
|
||||
ctx.createSerializer().ext(t1, bitsery::ext::StdTuple{
|
||||
[](auto& s, NonDefaultConstructable& v) {
|
||||
s.value4b(v._x);
|
||||
},
|
||||
});
|
||||
ctx.createDeserializer().ext(r1, bitsery::ext::StdTuple{
|
||||
[](auto& s, NonDefaultConstructable& v) {
|
||||
s.value4b(v._x);
|
||||
},
|
||||
});
|
||||
EXPECT_THAT(t1, Eq(r1));
|
||||
}
|
||||
|
||||
#else
|
||||
#if defined(_MSC_VER)
|
||||
#pragma message("Tests for StdTuple requires C++17")
|
||||
#else
|
||||
#warning "Tests for StdTuple requires C++17"
|
||||
#endif
|
||||
#endif
|
||||
178
tests/serialization_ext_std_variant.cpp
Normal file
178
tests/serialization_ext_std_variant.cpp
Normal file
@@ -0,0 +1,178 @@
|
||||
//MIT License
|
||||
//
|
||||
//Copyright (c) 2019 Mindaugas Vinkelis
|
||||
//
|
||||
//Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
//of this software and associated documentation files (the "Software"), to deal
|
||||
//in the Software without restriction, including without limitation the rights
|
||||
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
//copies of the Software, and to permit persons to whom the Software is
|
||||
//furnished to do so, subject to the following conditions:
|
||||
//
|
||||
//The above copyright notice and this permission notice shall be included in all
|
||||
//copies or substantial portions of the Software.
|
||||
//
|
||||
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
//SOFTWARE.
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include "serialization_test_utils.h"
|
||||
|
||||
|
||||
#if __cplusplus > 201402L
|
||||
|
||||
using testing::Eq;
|
||||
|
||||
#include <bitsery/ext/std_variant.h>
|
||||
|
||||
template<typename T, size_t N>
|
||||
using OverloadValue = bitsery::ext::OverloadValue<T, N>;
|
||||
|
||||
TEST(SerializeExtensionStdVariant, UseSerializeFunction) {
|
||||
|
||||
std::variant<MyStruct1, MyStruct2> t1{MyStruct1{978, 15}};
|
||||
std::variant<MyStruct1, MyStruct2> r1{MyStruct2{}};
|
||||
SerializationContext ctx;
|
||||
ctx.createSerializer().ext(t1, bitsery::ext::StdVariant{});
|
||||
ctx.createDeserializer().ext(r1, bitsery::ext::StdVariant{});
|
||||
EXPECT_THAT(t1, Eq(r1));
|
||||
}
|
||||
|
||||
TEST(SerializeExtensionStdVariant, WhenTwoIndicesWithSameTypeThenDeserializeCorrectIndex) {
|
||||
|
||||
std::variant<MyStruct1, MyStruct2, MyStruct1> t1{std::in_place_index_t<2>{}, MyStruct1{978, 15}};
|
||||
std::variant<MyStruct1, MyStruct2, MyStruct1> r1{MyStruct2{}};
|
||||
SerializationContext ctx;
|
||||
ctx.createSerializer().ext(t1, bitsery::ext::StdVariant{});
|
||||
ctx.createDeserializer().ext(r1, bitsery::ext::StdVariant{});
|
||||
EXPECT_THAT(t1, Eq(r1));
|
||||
}
|
||||
|
||||
TEST(SerializeExtensionStdVariant, ValueTypesCanBeSerializedWithLambda) {
|
||||
|
||||
std::variant<float, char, MyStruct1> t1{5.6f};
|
||||
std::variant<float, char, MyStruct1> r1{MyStruct1{}};
|
||||
SerializationContext ctx;
|
||||
auto fncFloat = [](auto& s, float& v) {
|
||||
s.value4b(v);
|
||||
};
|
||||
auto fncChar = [](auto& s, char& v) {
|
||||
s.value1b(v);
|
||||
};
|
||||
ctx.createSerializer().ext(t1, bitsery::ext::StdVariant{fncFloat, fncChar});
|
||||
ctx.createDeserializer().ext(r1, bitsery::ext::StdVariant{fncFloat, fncChar});
|
||||
EXPECT_THAT(t1, Eq(r1));
|
||||
}
|
||||
|
||||
TEST(SerializeExtensionStdVariant, ValueTypesCanBeSerializedWithLambdaAndOrCallableObject) {
|
||||
std::variant<float, char, MyStruct1> t1{'Z'};
|
||||
std::variant<float, char, MyStruct1> r1{MyStruct1{}};
|
||||
SerializationContext ctx;
|
||||
auto fncFloat = [](auto& s, float& v) {
|
||||
s.value4b(v);
|
||||
};
|
||||
|
||||
auto& ser = ctx.createSerializer();
|
||||
ser.ext(t1, bitsery::ext::StdVariant{fncFloat, OverloadValue<char, 1>{}});
|
||||
auto& des = ctx.createDeserializer();
|
||||
des.ext(r1, bitsery::ext::StdVariant{fncFloat, OverloadValue<char, 1>{}});
|
||||
EXPECT_THAT(t1, Eq(r1));
|
||||
}
|
||||
|
||||
TEST(SerializeExtensionStdVariant, CanOverloadDefaultSerializationFunction) {
|
||||
std::variant<MyStruct2, MyStruct1, int32_t> t1{MyStruct1{5, 9}};
|
||||
std::variant<MyStruct2, MyStruct1, int32_t> r1{MyStruct1{}};
|
||||
SerializationContext ctx;
|
||||
auto exec = [](auto& s, std::variant<MyStruct2, MyStruct1, int32_t>& o) {
|
||||
using S = decltype(s);
|
||||
s.ext(o, bitsery::ext::StdVariant{
|
||||
[](S& s, MyStruct1& v) {
|
||||
s.value4b(v.i1);
|
||||
//do not serialize other element, it should be 0 (default)
|
||||
},
|
||||
OverloadValue<int32_t, 4>{}
|
||||
});
|
||||
};
|
||||
|
||||
exec(ctx.createSerializer(), t1);
|
||||
exec(ctx.createDeserializer(), r1);
|
||||
EXPECT_THAT(std::get<1>(r1).i2, Eq(0));
|
||||
}
|
||||
|
||||
|
||||
struct NonDefaultConstructable {
|
||||
explicit NonDefaultConstructable(float x) : _x{x} {}
|
||||
|
||||
float _x;
|
||||
|
||||
bool operator==(const NonDefaultConstructable& rhs) const {
|
||||
return _x == rhs._x;
|
||||
}
|
||||
|
||||
private:
|
||||
friend class bitsery::Access;
|
||||
|
||||
NonDefaultConstructable() : _x{0.0f} {};
|
||||
};
|
||||
|
||||
TEST(SerializeExtensionStdVariant, CanUseNonDefaultConstructableTypes) {
|
||||
std::variant<NonDefaultConstructable, MyStruct1, int32_t> t1{NonDefaultConstructable{123.456f}};
|
||||
std::variant<NonDefaultConstructable, MyStruct1, int32_t> r1{MyStruct1{}};
|
||||
SerializationContext ctx;
|
||||
|
||||
auto exec = [](auto& s, std::variant<NonDefaultConstructable, MyStruct1, int32_t>& o) {
|
||||
using S = decltype(s);
|
||||
s.ext(o, bitsery::ext::StdVariant{
|
||||
[](S& s, NonDefaultConstructable& v) {
|
||||
s.value4b(v._x);
|
||||
},
|
||||
OverloadValue<int32_t, 4>{}
|
||||
});
|
||||
};
|
||||
|
||||
exec(ctx.createSerializer(), t1);
|
||||
exec(ctx.createDeserializer(), r1);
|
||||
|
||||
EXPECT_THAT(t1, Eq(r1));
|
||||
}
|
||||
|
||||
|
||||
TEST(SerializeExtensionStdVariant, CorrectlyHandleMonoState) {
|
||||
std::variant<std::monostate, NonDefaultConstructable, MyStruct1> t1{};
|
||||
std::variant<std::monostate, NonDefaultConstructable, MyStruct1> r1{};
|
||||
SerializationContext ctx;
|
||||
|
||||
auto exec = [](auto& s, auto& o) {
|
||||
using S = decltype(s);
|
||||
s.ext(o, bitsery::ext::StdVariant{
|
||||
[](S& s, NonDefaultConstructable& v) {
|
||||
s.value4b(v._x);
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
exec(ctx.createSerializer(), t1);
|
||||
exec(ctx.createDeserializer(), r1);
|
||||
|
||||
EXPECT_THAT(t1, Eq(r1));
|
||||
std::variant<std::monostate> t2{};
|
||||
std::variant<std::monostate> r2{};
|
||||
SerializationContext ctx1;
|
||||
ctx1.createSerializer().ext(t2, bitsery::ext::StdVariant{});
|
||||
ctx1.createDeserializer().ext(r2, bitsery::ext::StdVariant{});
|
||||
EXPECT_THAT(t2, Eq(r2));
|
||||
|
||||
}
|
||||
|
||||
#else
|
||||
#if defined(_MSC_VER)
|
||||
#pragma message("Tests for StdVariant requires C++17")
|
||||
#else
|
||||
#warning "Tests for StdVariant requires C++17"
|
||||
#endif
|
||||
#endif
|
||||
Reference in New Issue
Block a user