diff --git a/src/EC/Manager.hpp b/src/EC/Manager.hpp index d791aad..89614a9 100644 --- a/src/EC/Manager.hpp +++ b/src/EC/Manager.hpp @@ -172,10 +172,10 @@ namespace EC return; } - Component component(args...); + Component component(std::forward(args)...); std::get(entities[entityID]).template getComponentBit() = true; - std::get >(componentsStorage)[std::get(entities[entityID])] = component; + std::get >(componentsStorage)[std::get(entities[entityID])] = std::move(component); } template diff --git a/src/EC/Meta/ForEach.hpp b/src/EC/Meta/ForEach.hpp index e295d03..34b8e51 100644 --- a/src/EC/Meta/ForEach.hpp +++ b/src/EC/Meta/ForEach.hpp @@ -17,7 +17,7 @@ namespace EC template constexpr void forEachHelper(Function&& function, TTuple tuple, std::index_sequence) { - return (void)std::initializer_list{(function(std::get(tuple)), 0)...}; + return (void)std::initializer_list{(function(std::move(std::get(tuple))), 0)...}; } template diff --git a/src/test/ECTest.cpp b/src/test/ECTest.cpp index 3fd03cd..f766510 100644 --- a/src/test/ECTest.cpp +++ b/src/test/ECTest.cpp @@ -2,8 +2,9 @@ #include #include - #include +#include + #include #include @@ -35,6 +36,26 @@ using EmptyList = EC::Meta::TypeList<>; using MixedList = EC::Meta::TypeList; +typedef std::unique_ptr C0Ptr; + +struct Base +{ + virtual int getInt() + { + return 0; + } +}; + +struct Derived : public Base +{ + virtual int getInt() override + { + return 1; + } +}; + +typedef std::unique_ptr TestPtr; + TEST(EC, Bitset) { { @@ -134,3 +155,55 @@ TEST(EC, Manager) manager.cleanup(); } +TEST(EC, MoveComponentWithUniquePtr) +{ + { + EC::Manager, EC::Meta::TypeList<> > manager; + + std::size_t e = manager.addEntity(); + + { + C0Ptr ptr = std::make_unique(5, 10); + manager.addComponent(e, std::move(ptr)); + } + + int x = 0; + int y = 0; + manager.forMatchingSignature >([&x, &y] (std::size_t eID, C0Ptr& ptr) { + x = ptr->x; + y = ptr->y; + }); + EXPECT_EQ(5, x); + EXPECT_EQ(10, y); + } + { + EC::Manager, EC::Meta::TypeList<> > manager; + + std::size_t e = manager.addEntity(); + + { + TestPtr ptrBase = std::make_unique(); + manager.addComponent(e, std::move(ptrBase)); + } + + int result = 0; + + auto getResultFunction = [&result] (std::size_t eID, TestPtr& ptr) { + result = ptr->getInt(); + }; + + manager.forMatchingSignature >(getResultFunction); + + EXPECT_EQ(0, result); + + { + TestPtr ptrDerived = std::make_unique(); + manager.addComponent(e, std::move(ptrDerived)); + } + + manager.forMatchingSignature >(getResultFunction); + + EXPECT_EQ(1, result); + } +} +