From ec441e05616b3e9a5a384236b8c0ff536ed859e5 Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Sat, 20 Oct 2012 21:05:51 -0400 Subject: Convert EntityManager::unpack() to use boost::shared_ptr. --- entityx/Entity.h | 14 +++++++------- entityx/Entity_test.cc | 20 ++++++++++---------- entityx/System_test.cc | 8 ++++---- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/entityx/Entity.h b/entityx/Entity.h index 85ee2b7..6c0ab55 100644 --- a/entityx/Entity.h +++ b/entityx/Entity.h @@ -379,13 +379,13 @@ class EntityManager : boost::noncopyable { * * Useful for fast bulk iterations. * - * Position *p; - * Direction *d; + * boost::shared_ptr p; + * boost::shared_ptr d; * unpack(e, p, d); */ template - void unpack(Entity id, A *&a) { - a = component(id).get(); + void unpack(Entity id, boost::shared_ptr &a) { + a = component(id); } /** @@ -395,12 +395,12 @@ class EntityManager : boost::noncopyable { * * Useful for fast bulk iterations. * - * Position *p; - * Direction *d; + * boost::shared_ptr p; + * boost::shared_ptr d; * unpack(e, p, d); */ template - void unpack(Entity id, A *&a, B *&b, Args *& ... args) { + void unpack(Entity id, boost::shared_ptr &a, boost::shared_ptr &b, Args & ... args) { unpack(id, a); unpack(id, b, args ...); } diff --git a/entityx/Entity_test.cc b/entityx/Entity_test.cc index 962d858..33f3773 100644 --- a/entityx/Entity_test.cc +++ b/entityx/Entity_test.cc @@ -155,7 +155,7 @@ TEST_F(EntityManagerTest, TestGetEntitiesWithComponentAndUnpacking) { Entity e = em.create(); Entity f = em.create(); Entity g = em.create(); - std::vector, boost::shared_ptr>> position_directions; + std::vector, shared_ptr>> position_directions; position_directions.push_back(std::make_pair( em.assign(e, 1.0f, 2.0f), em.assign(e, 3.0f, 4.0f))); @@ -183,23 +183,23 @@ TEST_F(EntityManagerTest, TestUnpack) { auto p = em.assign(e); auto d = em.assign(e); - Position *up; - Direction *ud; + shared_ptr up; + shared_ptr ud; em.unpack(e, up, ud); - ASSERT_EQ(p.get(), up); - ASSERT_EQ(d.get(), ud); + ASSERT_EQ(p, up); + ASSERT_EQ(d, ud); } TEST_F(EntityManagerTest, TestUnpackNullMissing) { Entity e = em.create(); auto p = em.assign(e); - Position *up = reinterpret_cast(0Xdeadbeef); - Direction *ud = reinterpret_cast(0Xdeadbeef); - ASSERT_EQ(reinterpret_cast(0xdeadbeef), ud); + struct NullDeleter {template void operator()(T*) {} }; + shared_ptr up(reinterpret_cast(0Xdeadbeef), NullDeleter()); + shared_ptr ud(reinterpret_cast(0Xdeadbeef), NullDeleter()); em.unpack(e, up, ud); - ASSERT_EQ(p.get(), up); - ASSERT_EQ(nullptr, ud); + ASSERT_EQ(p, up); + ASSERT_EQ(shared_ptr(), ud); } TEST_F(EntityManagerTest, TestComponentIdsDiffer) { diff --git a/entityx/System_test.cc b/entityx/System_test.cc index 43f5ea9..0c54dab 100644 --- a/entityx/System_test.cc +++ b/entityx/System_test.cc @@ -39,8 +39,8 @@ class MovementSystem : public System { void update(EntityManager &es, EventManager &events, double) override { EntityManager::View entities = es.entities_with_components(); - Position *position; - Direction *direction; + shared_ptr position; + shared_ptr direction; for (auto entity : entities) { es.unpack(entity, position, direction); position->x += direction->x; @@ -102,8 +102,8 @@ TEST_F(SystemManagerTest, TestApplySystem) { world.sm().configure(); world.sm().update(0.0); - Position *position; - Direction *direction; + shared_ptr position; + shared_ptr direction; for (auto entity : world.entities) { world.em().unpack(entity, position, direction); if (position && direction) { -- cgit v1.2.3