#include <vector>
#include <tuple>
#include <utility>
+#include <functional>
#include "Meta/Combine.hpp"
#include "Meta/Matching.hpp"
ctype.template getEntityData<Types>(entityID)...
);
}
+
+ template <typename CType, typename Function>
+ void callInstance(std::size_t entityID, CType& ctype, Function&& function) const
+ {
+ ForMatchingSignatureHelper<Types...>::call(entityID, ctype, std::forward<Function>(function));
+ }
};
public:
}
}
+ private:
+ std::vector<std::function<void()> > forMatchingFunctions;
+
+ public:
+ template <typename Signature, typename Function>
+ void addForMatchingFunction(Function&& function)
+ {
+ using SignatureComponents = typename EC::Meta::Matching<Signature, ComponentsList>::type;
+ using Helper = EC::Meta::Morph<SignatureComponents, ForMatchingSignatureHelper<> >;
+
+ Helper helper;
+ BitsetType signatureBitset = BitsetType::template generateBitset<Signature>();
+
+ forMatchingFunctions.emplace_back( [function, signatureBitset, helper, this] () {
+ for(std::size_t i = 0; i < this->currentSize; ++i)
+ {
+ if(!std::get<bool>(this->entities[i]))
+ {
+ continue;
+ }
+ if((signatureBitset & std::get<BitsetType>(this->entities[i])) == signatureBitset)
+ {
+ helper.callInstance(i, *this, function);
+ }
+ }
+ });
+ }
+
+ void callForMatchingFunctions()
+ {
+ for(auto functionIter = forMatchingFunctions.begin(); functionIter != forMatchingFunctions.end(); ++functionIter)
+ {
+ (*functionIter)();
+ }
+ }
+
+ void clearForMatchingFunctions()
+ {
+ forMatchingFunctions.clear();
+ }
+
};
}
}
}
+TEST(EC, FunctionStorage)
+{
+ EC::Manager<ListComponentsAll, ListTagsAll> manager;
+ auto eid = manager.addEntity();
+ manager.addComponent<C0>(eid);
+
+ manager.addForMatchingFunction<EC::Meta::TypeList<C0> >( [] (std::size_t eid, C0& c0) {
+ c0.x = 1;
+ c0.y = 2;
+ });
+
+ manager.callForMatchingFunctions();
+
+ {
+ auto c0 = manager.getEntityData<C0>(eid);
+
+ EXPECT_EQ(c0.x, 1);
+ EXPECT_EQ(c0.y, 2);
+ }
+
+ manager.clearForMatchingFunctions();
+
+ manager.callForMatchingFunctions();
+
+ {
+ auto c0 = manager.getEntityData<C0>(eid);
+
+ EXPECT_EQ(c0.x, 1);
+ EXPECT_EQ(c0.y, 2);
+ }
+}
+