diff options
author | Alec Thomas <alec@swapoff.org> | 2014-07-07 12:53:19 +1000 |
---|---|---|
committer | Alec Thomas <alec@swapoff.org> | 2014-07-07 12:54:23 +1000 |
commit | f65ec82158625f6144751c803d2601b02c53b431 (patch) | |
tree | 3f6da2579a7e926c9b97aed3a05aa87fe1722745 | |
parent | e9ae5700c87c530e56edeb04816630a7dc92181e (diff) |
Merge @jarrettchisholm's change plus add some tests. Thanks!
Fixes #37 and #39.
-rw-r--r-- | entityx/Entity.h | 9 | ||||
-rw-r--r-- | entityx/Entity_test.cc | 18 |
2 files changed, 27 insertions, 0 deletions
diff --git a/entityx/Entity.h b/entityx/Entity.h index 51ecb03..7f5a5cb 100644 --- a/entityx/Entity.h +++ b/entityx/Entity.h @@ -126,6 +126,9 @@ public: ComponentHandle<C> assign(Args && ... args); template <typename C> + ComponentHandle<C> assign_from_copy(const C &component); + + template <typename C> void remove(); template <typename C> @@ -775,6 +778,12 @@ ComponentHandle<C> Entity::assign(Args && ... args) { } template <typename C> +ComponentHandle<C> Entity::assign_from_copy(const C &component) { + assert(valid()); + return manager_->assign<C>(id_, std::forward<const C &>(component)); +} + +template <typename C> void Entity::remove() { assert(valid() && has_component<C>()); manager_->remove<C>(id_); diff --git a/entityx/Entity_test.cc b/entityx/Entity_test.cc index 107f0a3..f57bb95 100644 --- a/entityx/Entity_test.cc +++ b/entityx/Entity_test.cc @@ -439,6 +439,24 @@ TEST_CASE_METHOD(EntityManagerFixture, "TestComponentHandleInvalidatedWhenEntity REQUIRE(!position); } +struct CopyVerifier : Component<CopyVerifier> { + CopyVerifier() : copied(false) {} + CopyVerifier(const CopyVerifier &other) { + copied = other.copied + 1; + } + + int copied; +}; + +TEST_CASE_METHOD(EntityManagerFixture, "TestComponentAssignmentFromCopy") { + Entity a = em.create(); + CopyVerifier original; + CopyVerifier::Handle copy = a.assign_from_copy(original); + REQUIRE(copy); + REQUIRE(copy->copied == 1); + a.destroy(); + REQUIRE(!copy); +} TEST_CASE_METHOD(EntityManagerFixture, "TestComponentHandleInvalidatedWhenComponentDestroyed") { Entity a = em.create(); |