Browse Source

registry: context insert_or_assign

Michele Caini 3 years ago
parent
commit
b20ab29d82
2 changed files with 27 additions and 1 deletions
  1. 10 0
      src/entt/entity/registry.hpp
  2. 17 1
      test/entt/entity/registry.cpp

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

@@ -171,6 +171,16 @@ public:
         return emplace_hint<Type>(type_id<Type>().hash(), std::forward<Args>(args)...);
     }
 
+    template<typename Type>
+    Type &insert_or_assign(const id_type id, Type &&value) {
+        return any_cast<std::remove_const_t<std::remove_reference_t<Type>> &>(ctx.insert_or_assign(id, std::forward<Type>(value)).first->second);
+    }
+
+    template<typename Type>
+    Type &insert_or_assign(Type &&value) {
+        return insert_or_assign(type_id<Type>().hash(), std::forward<Type>(value));
+    }
+
     template<typename Type>
     bool erase(const id_type id = type_id<Type>().hash()) {
         const auto it = ctx.find(id);

+ 17 - 1
test/entt/entity/registry.cpp

@@ -113,6 +113,16 @@ TEST(Registry, Context) {
 
     ASSERT_EQ(ctx.find<double>(), nullptr);
     ASSERT_EQ(cctx.find<double>(), nullptr);
+
+    ASSERT_EQ(ctx.insert_or_assign<char>('a'), 'a');
+    ASSERT_EQ(ctx.find<const char>(), cctx.find<char>());
+    ASSERT_EQ(ctx.get<char>(), cctx.get<const char>());
+    ASSERT_EQ(ctx.get<const char>(), 'a');
+
+    ASSERT_EQ(ctx.insert_or_assign<const int>(0), 0);
+    ASSERT_EQ(ctx.find<const int>(), cctx.find<int>());
+    ASSERT_EQ(ctx.at<int>(), cctx.at<const int>());
+    ASSERT_EQ(ctx.at<int>(), 0);
 }
 
 TEST(Registry, ContextHint) {
@@ -136,11 +146,17 @@ TEST(Registry, ContextHint) {
     ASSERT_EQ(ctx.get<int>(), 42);
     ASSERT_EQ(cctx.get<const int>("other"_hs), 3);
 
+    ctx.insert_or_assign(3);
+    ctx.insert_or_assign("other"_hs, 42);
+
+    ASSERT_EQ(ctx.get<const int>(), 3);
+    ASSERT_EQ(cctx.get<int>("other"_hs), 42);
+
     ASSERT_FALSE(ctx.erase<char>("other"_hs));
     ASSERT_TRUE(ctx.erase<int>());
 
     ASSERT_TRUE(cctx.contains<int>("other"_hs));
-    ASSERT_EQ(ctx.get<int>("other"_hs), 3);
+    ASSERT_EQ(ctx.get<int>("other"_hs), 42);
 
     ASSERT_TRUE(ctx.erase<int>("other"_hs));