Просмотр исходного кода

reactive mixin: return *this from on_<op> functions

Michele Caini 1 год назад
Родитель
Сommit
805cbf9c79
3 измененных файлов с 29 добавлено и 19 удалено
  1. 13 12
      docs/md/entity.md
  2. 9 3
      src/entt/entity/mixin.hpp
  3. 7 4
      test/entt/entity/reactive_mixin.cpp

+ 13 - 12
docs/md/entity.md

@@ -508,14 +508,13 @@ Here the choice boils down to three main events affecting all elements (entities
 or components), namely creation, update or destruction:
 
 ```cpp
-// observe position component construction
-storage.on_construct<position>();
-
-// observe velocity component update
-storage.on_update<velocity>();
-
-// observe renderable component destruction
-storage.on_destroy<renderable>();
+storage
+    // observe position component construction
+    .on_construct<position>()
+    // observe velocity component update
+    .on_update<velocity>()
+    // observe renderable component destruction
+    .on_destroy<renderable>();
 ```
 
 It goes without saying that it's possible to observe multiple events of the same
@@ -524,8 +523,9 @@ For example, to know which entities have been assigned or updated a component of
 a certain type:
 
 ```cpp
-storage.on_construct<my_type>();
-storage.on_update<my_type>();
+storage
+    .on_construct<my_type>()
+    .on_update<my_type>();
 ```
 
 Note that all configurations are in _or_ and never in _and_. Therefore, to track
@@ -556,8 +556,9 @@ options:
   // ...
 
   my_reactive_storage storage{};
-  storage.on_construct<position, &callback>();
-  storage.on_construct<velocity, &callback>();
+  storage
+      .on_construct<position, &callback>()
+      .on_construct<velocity, &callback>();
 
   // ...
 

+ 9 - 3
src/entt/entity/mixin.hpp

@@ -452,10 +452,12 @@ public:
      * @tparam Clazz Type of element to _react_ to.
      * @tparam Candidate Function to use to _react_ to the event.
      * @param id Optional name used to map the storage within the registry.
+     * @return This mixin.
      */
     template<typename Clazz, auto Candidate = &basic_reactive_mixin::emplace_element>
-    void on_construct(const id_type id = type_hash<Clazz>::value()) {
+    basic_reactive_mixin &on_construct(const id_type id = type_hash<Clazz>::value()) {
         owner_or_assert().template storage<Clazz>(id).on_construct().template connect<Candidate>(*this);
+        return *this;
     }
 
     /**
@@ -463,10 +465,12 @@ public:
      * @tparam Clazz Type of element to _react_ to.
      * @tparam Candidate Function to use to _react_ to the event.
      * @param id Optional name used to map the storage within the registry.
+     * @return This mixin.
      */
     template<typename Clazz, auto Candidate = &basic_reactive_mixin::emplace_element>
-    void on_update(const id_type id = type_hash<Clazz>::value()) {
+    basic_reactive_mixin &on_update(const id_type id = type_hash<Clazz>::value()) {
         owner_or_assert().template storage<Clazz>(id).on_update().template connect<Candidate>(*this);
+        return *this;
     }
 
     /**
@@ -474,10 +478,12 @@ public:
      * @tparam Clazz Type of element to _react_ to.
      * @tparam Candidate Function to use to _react_ to the event.
      * @param id Optional name used to map the storage within the registry.
+     * @return This mixin.
      */
     template<typename Clazz, auto Candidate = &basic_reactive_mixin::emplace_element>
-    void on_destroy(const id_type id = type_hash<Clazz>::value()) {
+    basic_reactive_mixin &on_destroy(const id_type id = type_hash<Clazz>::value()) {
         owner_or_assert().template storage<Clazz>(id).on_destroy().template connect<Candidate>(*this);
+        return *this;
     }
 
     /**

+ 7 - 4
test/entt/entity/reactive_mixin.cpp

@@ -72,8 +72,7 @@ TYPED_TEST(ReactiveMixin, Move) {
     const std::array entity{registry.create(), registry.create()};
 
     pool.bind(registry);
-    pool.template on_construct<test::empty>();
-    pool.template on_update<test::empty>();
+    pool.template on_construct<test::empty>().template on_update<test::empty>();
     registry.emplace<test::empty>(entity[0u]);
 
     static_assert(std::is_move_constructible_v<decltype(pool)>, "Move constructible type required");
@@ -394,8 +393,7 @@ TYPED_TEST(ReactiveMixin, EntityLifecycle) {
     const entt::entity entity{registry.create()};
 
     pool.bind(registry);
-    pool.template on_construct<test::empty>();
-    pool.template on_destroy<entt::entity, &remove<decltype(pool)>>();
+    pool.template on_construct<test::empty>().template on_destroy<entt::entity, &remove<decltype(pool)>>();
 
     ASSERT_FALSE(pool.contains(entity));
 
@@ -463,7 +461,12 @@ TYPED_TEST(ReactiveMixin, CustomRegistry) {
     entt::basic_reactive_mixin<entt::basic_storage<value_type, test::entity>, registry_type> pool;
     const std::array entity{registry.create(), registry.create()};
 
+    ASSERT_FALSE(pool);
+
     pool.bind(static_cast<entt::basic_registry<test::entity> &>(registry));
+
+    ASSERT_TRUE(pool);
+
     pool.template on_construct<test::empty>();
     registry.insert<test::empty>(entity.begin(), entity.end());