Browse Source

registry: assign_or_replace works also with aggregates now (close #429)

Michele Caini 6 years ago
parent
commit
17d96427ea
2 changed files with 13 additions and 1 deletions
  1. 1 1
      src/entt/entity/registry.hpp
  2. 12 0
      test/entt/entity/registry.cpp

+ 1 - 1
src/entt/entity/registry.hpp

@@ -717,7 +717,7 @@ public:
         auto &cpool = assure<Component>();
 
         return cpool.has(entity)
-                ? (cpool.replace(*this, entity, [args = std::forward_as_tuple(std::forward<Args>(args)...)](auto &&component) { component = std::make_from_tuple<Component>(std::move(args)); }), cpool.get(entity))
+                ? (cpool.replace(*this, entity, [&args...](auto &&component) { component = Component{std::forward<Args>(args)...}; }), cpool.get(entity))
                 : cpool.assign(*this, entity, std::forward<Args>(args)...);
     }
 

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

@@ -16,6 +16,10 @@ struct non_default_constructible {
     int value;
 };
 
+struct aggregate {
+    int value{};
+};
+
 struct listener {
     template<typename Component>
     static void sort(entt::registry &registry) {
@@ -256,6 +260,14 @@ TEST(Registry, Functionalities) {
     ASSERT_EQ(registry.capacity<char>(), entt::registry::size_type{});
 }
 
+TEST(Registry, AssignOrReplaceAggregates) {
+    entt::registry registry;
+    const auto entity = registry.create();
+    auto &instance = registry.assign_or_replace<aggregate>(entity, 42);
+
+    ASSERT_EQ(instance.value, 42);
+}
+
 TEST(Registry, Identifiers) {
     entt::registry registry;
     const auto pre = registry.create();