1
0
mirror of https://github.com/wqking/eventpp.git synced 2024-12-25 23:30:49 +08:00

Refactored: removed unnecessary copy constructor in AnyData

This commit is contained in:
wqking 2023-05-04 09:10:24 +08:00
parent aea43b534c
commit ca34f0ef37
2 changed files with 25 additions and 30 deletions

View File

@ -129,25 +129,12 @@ public:
}
}
template <typename T>
AnyData(const T & object) : functions(anydata_internal_::getAnyDataFunctions<T>()), buffer() {
static_assert(sizeof(T) <= maxSize, "AnyData: object size must not be greater than maxSize");
new (buffer.data()) T(object);
}
template <typename T>
AnyData(T & object) : functions(anydata_internal_::getAnyDataFunctions<T>()), buffer() {
static_assert(sizeof(T) <= maxSize, "AnyData: object size must not be greater than maxSize");
new (buffer.data()) T(object);
}
template <typename T>
AnyData(T && object) : functions(anydata_internal_::getAnyDataFunctions<T>()), buffer() {
static_assert(sizeof(T) <= maxSize, "AnyData: object size must not be greater than maxSize");
using U = typename std::remove_reference<T>::type;
static_assert(sizeof(U) <= maxSize, "AnyData: object size must not be greater than maxSize");
new (buffer.data()) T(std::move(object));
new (buffer.data()) U(std::forward<T>(object));
}
AnyData(const AnyData & other) : functions(other.functions), buffer() {

View File

@ -63,6 +63,9 @@ TEST_CASE("AnyData, unique_ptr")
Data data2(data);
REQUIRE(data2.isType<Ptr>());
REQUIRE(*data2.get<Ptr>() == 5);
Data data3(Data(Ptr(new int(8))));
REQUIRE(data3.isType<Ptr>());
REQUIRE(*data3.get<Ptr>() == 8);
}
TEST_CASE("AnyData, shared_ptr")
@ -71,21 +74,26 @@ TEST_CASE("AnyData, shared_ptr")
using Data = eventpp::AnyData<sizeof(Ptr)>;
Ptr ptr(std::make_shared<int>(8));
REQUIRE(ptr.use_count() == 1);
Data data(ptr);
REQUIRE(ptr.use_count() == 2);
REQUIRE(data.isType<Ptr>());
REQUIRE(*data.get<Ptr>() == 8);
Data data2(data);
REQUIRE(ptr.use_count() == 3);
REQUIRE(data2.isType<Ptr>());
REQUIRE(*data2.get<Ptr>() == 8);
REQUIRE(*data.get<Ptr>() == 8);
REQUIRE(*ptr == 8);
{
Data data(ptr);
REQUIRE(ptr.use_count() == 2);
REQUIRE(data.isType<Ptr>());
REQUIRE(*data.get<Ptr>() == 8);
Data data2(data);
REQUIRE(ptr.use_count() == 3);
REQUIRE(data2.isType<Ptr>());
REQUIRE(*data2.get<Ptr>() == 8);
REQUIRE(*data.get<Ptr>() == 8);
REQUIRE(*ptr == 8);
*ptr = 5;
REQUIRE(*data2.get<Ptr>() == 5);
REQUIRE(*data.get<Ptr>() == 5);
REQUIRE(*ptr == 5);
*ptr = 5;
REQUIRE(*data2.get<Ptr>() == 5);
REQUIRE(*data.get<Ptr>() == 5);
REQUIRE(*ptr == 5);
REQUIRE(ptr.use_count() == 3);
}
REQUIRE(ptr.use_count() == 1);
}
enum class EventType {