From 7c49ab4f044dfe81c8552ce044c8d05c02e525e9 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Tue, 20 Sep 2016 20:07:28 +0900 Subject: [PATCH] Added capability to store functions EC::Manager can now store functions similar to functions given to EC::Manager::forMatchingSignature. --- src/EC/Manager.hpp | 48 +++++++++++++++++++++++++++++++++++++++++++++ src/test/ECTest.cpp | 32 ++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/src/EC/Manager.hpp b/src/EC/Manager.hpp index aa44a79..2338c8d 100644 --- a/src/EC/Manager.hpp +++ b/src/EC/Manager.hpp @@ -13,6 +13,7 @@ #include #include #include +#include #include "Meta/Combine.hpp" #include "Meta/Matching.hpp" @@ -226,6 +227,12 @@ namespace EC ctype.template getEntityData(entityID)... ); } + + template + void callInstance(std::size_t entityID, CType& ctype, Function&& function) const + { + ForMatchingSignatureHelper::call(entityID, ctype, std::forward(function)); + } }; public: @@ -250,6 +257,47 @@ namespace EC } } + private: + std::vector > forMatchingFunctions; + + public: + template + void addForMatchingFunction(Function&& function) + { + using SignatureComponents = typename EC::Meta::Matching::type; + using Helper = EC::Meta::Morph >; + + Helper helper; + BitsetType signatureBitset = BitsetType::template generateBitset(); + + forMatchingFunctions.emplace_back( [function, signatureBitset, helper, this] () { + for(std::size_t i = 0; i < this->currentSize; ++i) + { + if(!std::get(this->entities[i])) + { + continue; + } + if((signatureBitset & std::get(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(); + } + }; } diff --git a/src/test/ECTest.cpp b/src/test/ECTest.cpp index ee72cd2..e93d929 100644 --- a/src/test/ECTest.cpp +++ b/src/test/ECTest.cpp @@ -242,3 +242,35 @@ TEST(EC, DeletedEntities) } } +TEST(EC, FunctionStorage) +{ + EC::Manager manager; + auto eid = manager.addEntity(); + manager.addComponent(eid); + + manager.addForMatchingFunction >( [] (std::size_t eid, C0& c0) { + c0.x = 1; + c0.y = 2; + }); + + manager.callForMatchingFunctions(); + + { + auto c0 = manager.getEntityData(eid); + + EXPECT_EQ(c0.x, 1); + EXPECT_EQ(c0.y, 2); + } + + manager.clearForMatchingFunctions(); + + manager.callForMatchingFunctions(); + + { + auto c0 = manager.getEntityData(eid); + + EXPECT_EQ(c0.x, 1); + EXPECT_EQ(c0.y, 2); + } +} +