Added support for components of type UniquePtr

This commit is contained in:
Stephen Seo 2016-04-06 19:32:30 +09:00
parent 6fbdbcebde
commit 577132554e
3 changed files with 77 additions and 4 deletions

View file

@ -172,10 +172,10 @@ namespace EC
return;
}
Component component(args...);
Component component(std::forward<Args>(args)...);
std::get<BitsetType>(entities[entityID]).template getComponentBit<Component>() = true;
std::get<std::vector<Component> >(componentsStorage)[std::get<std::size_t>(entities[entityID])] = component;
std::get<std::vector<Component> >(componentsStorage)[std::get<std::size_t>(entities[entityID])] = std::move(component);
}
template <typename Component>

View file

@ -17,7 +17,7 @@ namespace EC
template <typename Function, typename TTuple, std::size_t... Indices>
constexpr void forEachHelper(Function&& function, TTuple tuple, std::index_sequence<Indices...>)
{
return (void)std::initializer_list<int>{(function(std::get<Indices>(tuple)), 0)...};
return (void)std::initializer_list<int>{(function(std::move(std::get<Indices>(tuple))), 0)...};
}
template <typename TTypeList, typename Function>

View file

@ -2,8 +2,9 @@
#include <gtest/gtest.h>
#include <iostream>
#include <tuple>
#include <memory>
#include <EC/Meta/Meta.hpp>
#include <EC/EC.hpp>
@ -35,6 +36,26 @@ using EmptyList = EC::Meta::TypeList<>;
using MixedList = EC::Meta::TypeList<C2, T1>;
typedef std::unique_ptr<C0> C0Ptr;
struct Base
{
virtual int getInt()
{
return 0;
}
};
struct Derived : public Base
{
virtual int getInt() override
{
return 1;
}
};
typedef std::unique_ptr<Base> TestPtr;
TEST(EC, Bitset)
{
{
@ -134,3 +155,55 @@ TEST(EC, Manager)
manager.cleanup();
}
TEST(EC, MoveComponentWithUniquePtr)
{
{
EC::Manager<EC::Meta::TypeList<C0Ptr>, EC::Meta::TypeList<> > manager;
std::size_t e = manager.addEntity();
{
C0Ptr ptr = std::make_unique<C0>(5, 10);
manager.addComponent<C0Ptr>(e, std::move(ptr));
}
int x = 0;
int y = 0;
manager.forMatchingSignature<EC::Meta::TypeList<C0Ptr> >([&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<TestPtr>, EC::Meta::TypeList<> > manager;
std::size_t e = manager.addEntity();
{
TestPtr ptrBase = std::make_unique<Base>();
manager.addComponent<TestPtr>(e, std::move(ptrBase));
}
int result = 0;
auto getResultFunction = [&result] (std::size_t eID, TestPtr& ptr) {
result = ptr->getInt();
};
manager.forMatchingSignature<EC::Meta::TypeList<TestPtr> >(getResultFunction);
EXPECT_EQ(0, result);
{
TestPtr ptrDerived = std::make_unique<Derived>();
manager.addComponent<TestPtr>(e, std::move(ptrDerived));
}
manager.forMatchingSignature<EC::Meta::TypeList<TestPtr> >(getResultFunction);
EXPECT_EQ(1, result);
}
}