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().
}
}
+ /*!
+ \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.
template <typename List>
void clearSomeMatchingFunctions(List list)
{
- bool willErase = true;
+ bool willErase;
for(auto functionIter = forMatchingFunctions.begin();
functionIter != forMatchingFunctions.end();
++functionIter)
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)
//derp 0
});
- manager.addForMatchingFunction<EC::Meta::TypeList<>>(
+ auto lastIndex = manager.addForMatchingFunction<EC::Meta::TypeList<>>(
[] (std::size_t eid) {
//derp 1
});
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});
{
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();