diff options
author | Alec Thomas <alec@swapoff.org> | 2013-03-02 09:58:29 -0800 |
---|---|---|
committer | Alec Thomas <alec@swapoff.org> | 2013-03-02 09:58:29 -0800 |
commit | 87aee298f82ca64211753faee46a66166923685a (patch) | |
tree | cb9a37384064996bc71b96c8aad5dd0fe2454497 | |
parent | 87785361dc273e8ae8ed14cc6d55a81ba9c647af (diff) | |
parent | 2c78e9edf9042acab9660f09ae6119ca21e5aacf (diff) |
Merge pull request #2 from larspensjo/ReuseEntity
Bug in allocation of freed entities.
-rw-r--r-- | entityx/Entity.h | 12 | ||||
-rw-r--r-- | entityx/Entity_test.cc | 12 |
2 files changed, 19 insertions, 5 deletions
diff --git a/entityx/Entity.h b/entityx/Entity.h index 6d046c3..ae28443 100644 --- a/entityx/Entity.h +++ b/entityx/Entity.h @@ -1,10 +1,10 @@ /** * Copyright (C) 2012 Alec Thomas <alec@swapoff.org> * All rights reserved. - * + * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. - * + * * Author: Alec Thomas <alec@swapoff.org> */ @@ -227,7 +227,7 @@ class EntityManager : boost::noncopyable { return true; } - EntityManager &manager_; + EntityManager &manager_; const std::vector<Predicate> predicates_; std::vector<boost::function<void (Entity::Id)>> unpackers_; Entity::Id i_; @@ -246,7 +246,7 @@ class EntityManager : boost::noncopyable { template <typename A> View &unpack_to(boost::shared_ptr<A> &a) { unpackers_.push_back(Unpacker<A>(manager_, a)); - // This resulted in a segfault under clang 4.1 on OSX. No idea why. + // This resulted in a segfault under clang 4.1 on OSX. No idea why. /*unpackers_.push_back([&a, this](Entity::Id id) { a = manager_.component<A>(id); });*/ @@ -299,7 +299,9 @@ class EntityManager : boost::noncopyable { id = id_counter_++; accomodate_entity(id); } else { - id = *free_list_.erase(free_list_.begin()); + auto it = free_list_.begin(); + id = *it; + free_list_.erase(it); } event_manager_.emit<EntityCreatedEvent>(*this, id); return Entity(*this, id); diff --git a/entityx/Entity_test.cc b/entityx/Entity_test.cc index 67026e8..d042b4b 100644 --- a/entityx/Entity_test.cc +++ b/entityx/Entity_test.cc @@ -75,8 +75,20 @@ class EntityManagerTest : public ::testing::Test { TEST_F(EntityManagerTest, TestCreateEntity) { + ASSERT_TRUE(em.size() == 0); Entity e = em.create(); ASSERT_TRUE(em.exists(e)); + ASSERT_TRUE(em.size() == 1); +} + +TEST_F(EntityManagerTest, TestEntityReuse) { + Entity e1 = em.create(); + auto id = e1.id(); + em.destroy(e1); + ASSERT_TRUE(!em.exists(e1)); + Entity e2 = em.create(); + // It is assumed that the allocation will reuse the same entity id. + ASSERT_EQ(e2.id(), id); } TEST_F(EntityManagerTest, TestComponentConstruction) { |