Explorar o código

sigh mixin: refine auto signals

Michele Caini hai 1 ano
pai
achega
b5fbc68baa
Modificáronse 4 ficheiros con 55 adicións e 80 borrados
  1. 1 0
      TODO
  2. 9 18
      src/entt/entity/mixin.hpp
  3. 45 0
      test/entt/entity/sigh_mixin.cpp
  4. 0 62
      test/entt/entity/storage_signals.cpp

+ 1 - 0
TODO

@@ -37,3 +37,4 @@ TODO:
 * view and view iterator specializations for multi, single and filtered elements
 * organizer support to groups
 * meta range: move id to meta objects and return plain types (?), then remove id from meta base and meta ctor too
+* doc: automatic signal registration

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

@@ -14,34 +14,25 @@ namespace entt {
 /*! @cond TURN_OFF_DOXYGEN */
 namespace internal {
 
-template<typename T, typename Reg, typename = void>
+template<typename, typename, typename = void>
 struct has_on_construct final: std::false_type {};
 
-template<typename T, typename Reg>
-struct has_on_construct<
-    T, Reg,
-    std::void_t<decltype(T::on_construct(std::declval<Reg &>(),
-                                         std::declval<Reg &>().create()))>>
+template<typename Type, typename Registry>
+struct has_on_construct<Type, Registry, std::void_t<decltype(Type::on_construct(std::declval<Registry &>(), std::declval<Registry>().create()))>>
     : std::true_type {};
 
-template<typename T, typename Reg, typename = void>
+template<typename, typename, typename = void>
 struct has_on_update final: std::false_type {};
 
-template<typename T, typename Reg>
-struct has_on_update<
-    T, Reg,
-    std::void_t<decltype(T::on_update(std::declval<Reg &>(),
-                                      std::declval<Reg &>().create()))>>
+template<typename Type, typename Registry>
+struct has_on_update<Type, Registry, std::void_t<decltype(Type::on_update(std::declval<Registry &>(), std::declval<Registry>().create()))>>
     : std::true_type {};
 
-template<typename T, typename Reg, typename = void>
+template<typename, typename, typename = void>
 struct has_on_destroy final: std::false_type {};
 
-template<typename T, typename Reg>
-struct has_on_destroy<
-    T, Reg,
-    std::void_t<decltype(T::on_destroy(std::declval<Reg &>(),
-                                       std::declval<Reg &>().create()))>>
+template<typename Type, typename Registry>
+struct has_on_destroy<Type, Registry, std::void_t<decltype(Type::on_destroy(std::declval<Registry &>(), std::declval<Registry>().create()))>>
     : std::true_type {};
 
 } // namespace internal

+ 45 - 0
test/entt/entity/sigh_mixin.cpp

@@ -18,6 +18,24 @@
 #include "../../common/throwing_allocator.hpp"
 #include "../../common/throwing_type.hpp"
 
+struct auto_signal final {
+    inline static void on_construct(const entt::registry &, entt::entity) {
+        ++created;
+    }
+
+    inline static void on_update(entt::registry &, const entt::entity) {
+        ++updated;
+    }
+
+    inline static void on_destroy(entt::registry &, entt::entity) {
+        ++destroyed;
+    }
+
+    inline static std::size_t created{};
+    inline static std::size_t updated{};
+    inline static std::size_t destroyed{};
+};
+
 template<typename Registry>
 void listener(std::size_t &counter, Registry &, typename Registry::entity_type) {
     ++counter;
@@ -625,3 +643,30 @@ TEST(SighMixin, ThrowingComponent) {
     ASSERT_EQ(on_construct, 2u);
     ASSERT_EQ(on_destroy, 3u);
 }
+
+TEST(SighMixin, AutoSignal) {
+    entt::registry registry;
+    auto const entity = registry.create();
+
+    registry.emplace<auto_signal>(entity);
+    registry.replace<auto_signal>(entity);
+    registry.erase<auto_signal>(entity);
+
+    ASSERT_EQ(auto_signal::created, 1u);
+    ASSERT_EQ(auto_signal::updated, 1u);
+    ASSERT_EQ(auto_signal::destroyed, 1u);
+
+    ASSERT_TRUE(registry.storage<auto_signal>().empty());
+    ASSERT_TRUE(registry.valid(entity));
+
+    registry.emplace<auto_signal>(entity);
+    registry.replace<auto_signal>(entity);
+    registry.destroy(entity);
+
+    ASSERT_EQ(auto_signal::created, 2u);
+    ASSERT_EQ(auto_signal::updated, 2u);
+    ASSERT_EQ(auto_signal::destroyed, 2u);
+
+    ASSERT_TRUE(registry.storage<auto_signal>().empty());
+    ASSERT_FALSE(registry.valid(entity));
+}

+ 0 - 62
test/entt/entity/storage_signals.cpp

@@ -1,62 +0,0 @@
-
-#include <gtest/gtest.h>
-#include <entt/entity/registry.hpp>
-
-struct count_tracker final {
-    inline static void on_construct(entt::registry &, entt::entity) {
-        ++created;
-    }
-
-    inline static void on_update(entt::registry &, entt::entity) {
-        ++updated;
-    }
-
-    inline static void on_destroy(entt::registry &, entt::entity) {
-        ++destroyed;
-    }
-
-    inline static std::size_t created = 0;
-    inline static std::size_t updated = 0;
-    inline static std::size_t destroyed = 0;
-
-    inline static std::size_t alive() {
-        return created - destroyed;
-    }
-};
-
-template<typename Type>
-struct StorageSignals: testing::Test {
-    using type = Type;
-};
-
-using StorageSignalsTypes = ::testing::Types<count_tracker>;
-
-TYPED_TEST_SUITE(StorageSignals, StorageSignalsTypes, );
-
-TYPED_TEST(StorageSignals, AutoSignals) {
-    using value_type = typename TestFixture::type;
-
-    entt::registry registry;
-    auto const id = registry.create();
-
-    registry.emplace<count_tracker>(id);
-
-    ASSERT_EQ(count_tracker::created, 1);
-    ASSERT_EQ(count_tracker::updated, 0);
-    ASSERT_EQ(count_tracker::destroyed, 0);
-    ASSERT_EQ(count_tracker::alive(), 1);
-
-    registry.replace<count_tracker>(id);
-
-    ASSERT_EQ(count_tracker::created, 1);
-    ASSERT_EQ(count_tracker::updated, 1);
-    ASSERT_EQ(count_tracker::destroyed, 0);
-    ASSERT_EQ(count_tracker::alive(), 1);
-
-    registry.remove<count_tracker>(id);
-
-    ASSERT_EQ(count_tracker::created, 1);
-    ASSERT_EQ(count_tracker::updated, 1);
-    ASSERT_EQ(count_tracker::destroyed, 1);
-    ASSERT_EQ(count_tracker::alive(), 0);
-}