Added two new functions

Added "removeForMatchingFunction" and "callForMatchingFunction".
The former deletes a specific function and the latter calls a specific
function.
This commit is contained in:
Stephen Seo 2017-07-12 22:02:02 +09:00
parent 7d605bebe7
commit 49ae172c3a
2 changed files with 76 additions and 4 deletions

View 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)

View 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();