]> code.bitgloo.com Git - clyne/entityx.git/commitdiff
Add dereference operator to ComponentHandle
authorlethal-guitar <lethal_guitar128@web.de>
Wed, 28 Dec 2016 14:50:28 +0000 (15:50 +0100)
committerlethal-guitar <lethal_guitar128@web.de>
Wed, 28 Dec 2016 14:50:28 +0000 (15:50 +0100)
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.

entityx/Entity.h
entityx/Entity_test.cc

index e4a3871b477238e590dce4f7369adcabf978f8ec..7c69e0a50ebff7c11f29cea0ca41e22429003623 100644 (file)
@@ -193,6 +193,9 @@ public:
   C *operator -> ();
   const C *operator -> () const;
 
+  C &operator * ();
+  const C &operator * () const;
+
   C *get();
   const C *get() const;
 
@@ -1045,6 +1048,18 @@ inline const C *ComponentHandle<C, EM>::operator -> () const {
   return manager_->template get_component_ptr<C>(id_);
 }
 
+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());
index 9e75ee3c1ed8c890936dc7aaf4f8b7f248f24e2e..f98579439d00fb68ef7cf47da296bfd687618825 100644 (file)
@@ -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);
+}