aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlethal-guitar <lethal_guitar128@web.de>2016-12-28 15:50:28 +0100
committerlethal-guitar <lethal_guitar128@web.de>2016-12-28 15:50:28 +0100
commitb8352b0fbe7c3d61cd0452900dea9ea963400520 (patch)
tree41f76c7c07906904ed4af8ed978fa14fe0518aac
parent4fe45348467145dbf0c6403dde4b730de5e575e1 (diff)
Add dereference operator to ComponentHandle
This makes it possible to write code like this: auto& componentRef = *someEntity.component<Position>(); This was already possible previously, but required calling .get() on the component handle.
-rw-r--r--entityx/Entity.h15
-rw-r--r--entityx/Entity_test.cc10
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);
+}