diff options
author | Alec Thomas <alec@swapoff.org> | 2016-12-29 08:38:36 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-12-29 08:38:36 +1100 |
commit | 08479b0d3e648daefbcbd3814a421d9be623ac20 (patch) | |
tree | 41f76c7c07906904ed4af8ed978fa14fe0518aac | |
parent | 4fe45348467145dbf0c6403dde4b730de5e575e1 (diff) | |
parent | b8352b0fbe7c3d61cd0452900dea9ea963400520 (diff) |
Merge pull request #169 from lethal-guitar/component-operator-star
Add dereference operator to ComponentHandle
-rw-r--r-- | entityx/Entity.h | 15 | ||||
-rw-r--r-- | entityx/Entity_test.cc | 10 |
2 files changed, 25 insertions, 0 deletions
diff --git a/entityx/Entity.h b/entityx/Entity.h index e4a3871..7c69e0a 100644 --- a/entityx/Entity.h +++ b/entityx/Entity.h @@ -193,6 +193,9 @@ public: C *operator -> (); const C *operator -> () const; + C &operator * (); + const C &operator * () const; + C *get(); const C *get() const; @@ -1046,6 +1049,18 @@ inline const C *ComponentHandle<C, EM>::operator -> () const { } template <typename C, typename EM> +inline C &ComponentHandle<C, EM>::operator * () { + assert(valid()); + return *manager_->template get_component_ptr<C>(id_); +} + +template <typename C, typename EM> +inline const C &ComponentHandle<C, EM>::operator * () const { + assert(valid()); + return *manager_->template get_component_ptr<C>(id_); +} + +template <typename C, typename EM> inline C *ComponentHandle<C, EM>::get() { assert(valid()); return manager_->template get_component_ptr<C>(id_); diff --git a/entityx/Entity_test.cc b/entityx/Entity_test.cc index 9e75ee3..f985794 100644 --- a/entityx/Entity_test.cc +++ b/entityx/Entity_test.cc @@ -668,3 +668,13 @@ TEST_CASE_METHOD(EntityManagerFixture, "TestViewEach") { }); REQUIRE(count == 1); } + +TEST_CASE_METHOD(EntityManagerFixture, "TestComponentDereference") { + Entity a = em.create(); + a.assign<Position>(10, 5); + auto& positionRef = *a.component<Position>(); + REQUIRE(positionRef.x == 10); + REQUIRE(positionRef.y == 5); + positionRef.y = 20; + REQUIRE(a.component<Position>()->y == 20); +} |