Michele Caini 6 lat temu
rodzic
commit
1442a3853c
3 zmienionych plików z 13 dodań i 10 usunięć
  1. 2 0
      TODO
  2. 9 10
      src/entt/entity/registry.hpp
  3. 2 0
      test/entt/entity/registry.cpp

+ 2 - 0
TODO

@@ -25,3 +25,5 @@
 * multi component registry::remove and some others?
 * move CONTRIBUTING.md within doc (for GitHub)
 * reactive systems
+
+* test snapshot for empty types

+ 9 - 10
src/entt/entity/registry.hpp

@@ -64,8 +64,8 @@ class basic_registry {
 
     template<typename Component>
     struct pool_handler: storage<Entity, Component> {
-        sigh<void(basic_registry &, const Entity, Component &)> on_construct;
-        sigh<void(basic_registry &, const Entity, Component &)> on_replace;
+        sigh<void(basic_registry &, const Entity, std::conditional_t<std::is_empty_v<Component>, const Component &, Component &>)> on_construct;
+        sigh<void(basic_registry &, const Entity, std::conditional_t<std::is_empty_v<Component>, const Component &, Component &>)> on_replace;
         sigh<void(basic_registry &, const Entity)> on_destroy;
         void *group{};
 
@@ -78,10 +78,9 @@ class basic_registry {
         template<typename... Args>
         decltype(auto) assign(basic_registry &registry, const Entity entt, Args &&... args) {
             if constexpr(std::is_empty_v<Component>) {
-                Component component{std::forward<Args>(args)...};
                 storage<Entity, Component>::construct(entt);
-                on_construct.publish(registry, entt, component);
-                return std::move(component);
+                on_construct.publish(registry, entt, Component{});
+                return Component{std::forward<Args>(args)...};
             } else {
                 auto &component = storage<Entity, Component>::construct(entt, std::forward<Args>(args)...);
                 on_construct.publish(registry, entt, component);
@@ -120,13 +119,13 @@ class basic_registry {
         }
 
         template<typename... Args>
-        decltype(auto) replace(basic_registry &registry, const Entity entt, [[maybe_unused]] Args &&... args) {
-            Component component{std::forward<Args>(args)...};
-            on_replace.publish(registry, entt, component);
-
+        decltype(auto) replace(basic_registry &registry, const Entity entt, Args &&... args) {
             if constexpr(std::is_empty_v<Component>) {
-                return std::move(component);
+                on_replace.publish(registry, entt, Component{});
+                return Component{std::forward<Args>(args)...};
             } else {
+                Component component{std::forward<Args>(args)...};
+                on_replace.publish(registry, entt, component);
                 return (storage<Entity, Component>::get(entt) = std::move(component));
             }
         }

+ 2 - 0
test/entt/entity/registry.cpp

@@ -1043,6 +1043,8 @@ TEST(Registry, CreateManyEntitiesAtOnce) {
 TEST(Registry, CreateAnEntityWithComponents) {
     entt::registry registry;
     auto &&[entity, ivalue, cvalue, evalue] = registry.create<int, char, empty_type>();
+    // suppress warnings
+    (void)evalue;
 
     ASSERT_FALSE(registry.empty<int>());
     ASSERT_FALSE(registry.empty<char>());