From 49ae172c3a9f3a2f4eb10aef23b59502ac95fccc Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 12 Jul 2017 22:02:02 +0900 Subject: [PATCH] Added two new functions Added "removeForMatchingFunction" and "callForMatchingFunction". The former deletes a specific function and the latter calls a specific function. --- src/EC/Manager.hpp | 43 ++++++++++++++++++++++++++++++++++++++++--- src/test/ECTest.cpp | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 76 insertions(+), 4 deletions(-) diff --git a/src/EC/Manager.hpp b/src/EC/Manager.hpp index e6d4625..1e07a80 100644 --- a/src/EC/Manager.hpp +++ b/src/EC/Manager.hpp @@ -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>( + [] (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 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(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 void clearSomeMatchingFunctionsWithSet(Set set) diff --git a/src/test/ECTest.cpp b/src/test/ECTest.cpp index 02b83fb..f7ca0d3 100644 --- a/src/test/ECTest.cpp +++ b/src/test/ECTest.cpp @@ -266,7 +266,7 @@ TEST(EC, FunctionStorage) //derp 0 }); - manager.addForMatchingFunction>( + auto lastIndex = manager.addForMatchingFunction>( [] (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(eid); + + EXPECT_EQ(2, c0.x); + EXPECT_EQ(3, c0.y); + + c0.x = 1; + c0.y = 2; + + auto c1 = manager.getEntityData(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(eid); + + EXPECT_EQ(1, c0.x); + EXPECT_EQ(2, c0.y); + + auto c1 = manager.getEntityData(eid); + + EXPECT_EQ(11, c1.vx); + EXPECT_EQ(46, c1.vy); + } + manager.clearForMatchingFunctions(); manager.callForMatchingFunctions();