]> git.seodisparate.com - EntityComponentMetaSystem/commitdiff
Added two new functions
authorStephen Seo <seo.disparate@gmail.com>
Wed, 12 Jul 2017 13:02:02 +0000 (22:02 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Wed, 12 Jul 2017 13:02:02 +0000 (22:02 +0900)
Added "removeForMatchingFunction" and "callForMatchingFunction".
The former deletes a specific function and the latter calls a specific
function.

src/EC/Manager.hpp
src/test/ECTest.cpp

index e6d46250f714ead738bc48d7533aa2e113dfc4b6..1e07a80cdf6e6d262921ad62ccce0ff0681fb0cd 100644 (file)
@@ -439,8 +439,9 @@ namespace EC
 
             As an alternative to calling functions directly with
             forMatchingSignature(), functions can be stored in the manager to
-            be called later with callForMatchingFunctions() and removed with
-            clearForMatchingFunctions().
+            be called later with callForMatchingFunctions() and
+            callForMatchingFunction, and removed with clearForMatchingFunctions()
+            and removeForMatchingFunction().
 
             The syntax for the Function is the same as with forMatchingSignature().
 
@@ -513,6 +514,32 @@ namespace EC
             }
         }
 
+        /*!
+            \brief Call a specific stored function.
+
+            Example:
+            \code{.cpp}
+                unsigned long long id = manager.addForMatchingFunction<TypeList<C0, C1, T0>>(
+                        [] (std::size_t ID, C0& c0, C1& c1) {
+                    // Lambda function contents here
+                });
+
+                manager.callForMatchingFunction(id); // call the previously added function
+            \endcode
+
+            \return False if a function with the given id does not exist.
+        */
+        bool callForMatchingFunction(unsigned long long id)
+        {
+            auto iter = forMatchingFunctions.find(id);
+            if(iter == forMatchingFunctions.end())
+            {
+                return false;
+            }
+            iter->second();
+            return true;
+        }
+
         /*!
             \brief Remove all stored functions.
 
@@ -547,7 +574,7 @@ namespace EC
         template <typename List>
         void clearSomeMatchingFunctions(List list)
         {
-            bool willErase = true;
+            bool willErase;
             for(auto functionIter = forMatchingFunctions.begin();
                 functionIter != forMatchingFunctions.end();
                 ++functionIter)
@@ -584,6 +611,16 @@ namespace EC
             clearSomeMatchingFunctions<decltype(list)>(list);
         }
 
+        /*!
+            \brief Removes a function that has the given id.
+
+            \return True if a function was erased.
+        */
+        bool removeForMatchingFunction(unsigned long long id)
+        {
+            return forMatchingFunctions.erase(id) == 1;
+        }
+
     private:
         template <typename Set>
         void clearSomeMatchingFunctionsWithSet(Set set)
index 02b83fb2b01598b0535acaded368b47b2c798946..f7ca0d313661dcdd9a6e35c41fffce21ec09d1a7 100644 (file)
@@ -266,7 +266,7 @@ TEST(EC, FunctionStorage)
             //derp 0
     });
 
-    manager.addForMatchingFunction<EC::Meta::TypeList<>>(
+    auto lastIndex = manager.addForMatchingFunction<EC::Meta::TypeList<>>(
         [] (std::size_t eid) {
             //derp 1
     });
@@ -285,6 +285,24 @@ TEST(EC, FunctionStorage)
         EXPECT_EQ(23, c1.vy);
     }
 
+    EXPECT_TRUE(manager.callForMatchingFunction(f0index));
+    EXPECT_FALSE(manager.callForMatchingFunction(lastIndex + 1));
+
+    {
+        auto& c0 = manager.getEntityData<C0>(eid);
+
+        EXPECT_EQ(2, c0.x);
+        EXPECT_EQ(3, c0.y);
+
+        c0.x = 1;
+        c0.y = 2;
+
+        auto c1 = manager.getEntityData<C1>(eid);
+
+        EXPECT_EQ(11, c1.vx);
+        EXPECT_EQ(23, c1.vy);
+    }
+
     manager.clearSomeMatchingFunctions({f1index});
 
     {
@@ -314,6 +332,23 @@ TEST(EC, FunctionStorage)
         EXPECT_EQ(46, c1.vy);
     }
 
+    EXPECT_TRUE(manager.removeForMatchingFunction(f1index));
+    EXPECT_FALSE(manager.removeForMatchingFunction(f1index));
+
+    manager.callForMatchingFunctions();
+
+    {
+        auto c0 = manager.getEntityData<C0>(eid);
+
+        EXPECT_EQ(1, c0.x);
+        EXPECT_EQ(2, c0.y);
+
+        auto c1 = manager.getEntityData<C1>(eid);
+
+        EXPECT_EQ(11, c1.vx);
+        EXPECT_EQ(46, c1.vy);
+    }
+
     manager.clearForMatchingFunctions();
     manager.callForMatchingFunctions();