From d12af4d5272b982393395836202d4e838d151cf6 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Thu, 17 May 2018 17:05:49 +0900 Subject: [PATCH] Add ability to handle unknown types EC/Manager and EC/Bitset can now handle types that are not known (Components or Tags not in the given Components or Tags when created). Manager::getEntityData now returns a pointer instead of a reference. Functions passed to Manager::forMatchingFunction (and other similar fns) should now accept Component fields as pointer types, not reference types. --- src/EC/Bitset.hpp | 26 ++- src/EC/Manager.hpp | 59 ++++-- src/EC/Meta/IndexOf.hpp | 8 +- src/test/ECTest.cpp | 417 ++++++++++++++++++++-------------------- src/test/MetaTest.cpp | 6 +- 5 files changed, 289 insertions(+), 227 deletions(-) diff --git a/src/EC/Bitset.hpp b/src/EC/Bitset.hpp index e3dc07b..b814e07 100644 --- a/src/EC/Bitset.hpp +++ b/src/EC/Bitset.hpp @@ -16,34 +16,50 @@ namespace EC { + // Note bitset size is sizes of components and tags + 1 + // This is to use the last extra bit as the result of a query + // with a Component or Tag not known to the Bitset. + // Those queries will return a false bit every time. template struct Bitset : - public std::bitset + public std::bitset { using Combined = EC::Meta::Combine; + Bitset() + { + (*this)[Combined::size] = false; + } + + // TODO find a better way to handle non-member type in const template constexpr auto getComponentBit() const { - return (*this)[EC::Meta::IndexOf::value]; + auto index = EC::Meta::IndexOf::value; + return (*this)[index]; } template constexpr auto getComponentBit() { - return (*this)[EC::Meta::IndexOf::value]; + auto index = EC::Meta::IndexOf::value; + (*this)[Combined::size] = false; + return (*this)[index]; } template constexpr auto getTagBit() const { - return (*this)[EC::Meta::IndexOf::value]; + auto index = EC::Meta::IndexOf::value; + return (*this)[index]; } template constexpr auto getTagBit() { - return (*this)[EC::Meta::IndexOf::value]; + auto index = EC::Meta::IndexOf::value; + (*this)[Combined::size] = false; + return (*this)[index]; } template diff --git a/src/EC/Manager.hpp b/src/EC/Manager.hpp index 0e958fd..b3dd90f 100644 --- a/src/EC/Manager.hpp +++ b/src/EC/Manager.hpp @@ -31,6 +31,7 @@ #include "Meta/Matching.hpp" #include "Meta/ForEachWithIndex.hpp" #include "Meta/ForEachDoubleTuple.hpp" +#include "Meta/IndexOf.hpp" #include "Bitset.hpp" namespace EC @@ -61,7 +62,7 @@ namespace EC template struct Storage { - using type = std::tuple... >; + using type = std::tuple..., std::vector >; }; using ComponentsStorage = typename EC::Meta::Morph >::type; @@ -230,10 +231,22 @@ namespace EC the Entity actually owns that Component. */ template - Component& getEntityData(const std::size_t& index) + Component* getEntityData(const std::size_t& index) { - return std::get >(componentsStorage).at( - index); + constexpr auto componentIndex = EC::Meta::IndexOf< + Component, Components>::value; + if(componentIndex < Components::size) + { + // Cast required due to compiler thinking that an invalid + // Component is needed even though the enclosing if statement + // prevents this from ever happening. + return (Component*) &std::get( + componentsStorage).at(index); + } + else + { + return nullptr; + } } /*! @@ -249,7 +262,7 @@ namespace EC the Entity actually owns that Component. */ template - Component& getEntityComponent(const std::size_t& index) + Component* getEntityComponent(const std::size_t& index) { return getEntityData(index); } @@ -265,10 +278,22 @@ namespace EC to determine if the Entity actually owns that Component. */ template - const Component& getEntityData(const std::size_t& index) const + const Component* getEntityData(const std::size_t& index) const { - return std::get >(componentsStorage).at( - index); + constexpr auto componentIndex = EC::Meta::IndexOf< + Component, Components>::value; + if(componentIndex < Components::size) + { + // Cast required due to compiler thinking that an invalid + // Component is needed even though the enclosing if statement + // prevents this from ever happening. + return (Component*) &std::get( + componentsStorage).at(index); + } + else + { + return nullptr; + } } /*! @@ -284,7 +309,7 @@ namespace EC to determine if the Entity actually owns that Component. */ template - const Component& getEntityComponent(const std::size_t& index) const + const Component* getEntityComponent(const std::size_t& index) const { return getEntityData(index); } @@ -349,7 +374,8 @@ namespace EC template void addComponent(const std::size_t& entityID, Args&&... args) { - if(!isAlive(entityID)) + if(!isAlive(entityID) + || !EC::Meta::Contains::value) { return; } @@ -360,9 +386,15 @@ namespace EC entities[entityID] ).template getComponentBit() = true; - std::get >( + constexpr auto index = + EC::Meta::IndexOf::value; + + // Cast required due to compiler thinking that vector at + // index = Components::size is being used, even if the previous + // if statement will prevent this from ever happening. + (*((std::vector*)(&std::get( componentsStorage - )[entityID] = std::move(component); + ))))[entityID] = std::move(component); } /*! @@ -400,7 +432,8 @@ namespace EC template void addTag(const std::size_t& entityID) { - if(!isAlive(entityID)) + if(!isAlive(entityID) + || !EC::Meta::Contains::value) { return; } diff --git a/src/EC/Meta/IndexOf.hpp b/src/EC/Meta/IndexOf.hpp index db155d8..851d9fb 100644 --- a/src/EC/Meta/IndexOf.hpp +++ b/src/EC/Meta/IndexOf.hpp @@ -4,6 +4,12 @@ // His code is available here: https://github.com/SuperV1234/cppcon2015 +/* + Returns the index of a type in a type list. + If the type does not exist in the type list, + returns the size of the type list. +*/ + #ifndef EC_META_INDEX_OF_HPP #define EC_META_INDEX_OF_HPP @@ -14,7 +20,7 @@ namespace EC namespace Meta { template - struct IndexOf + struct IndexOf : std::integral_constant { }; diff --git a/src/test/ECTest.cpp b/src/test/ECTest.cpp index fc5e59a..3167a52 100644 --- a/src/test/ECTest.cpp +++ b/src/test/ECTest.cpp @@ -96,18 +96,18 @@ TEST(EC, Manager) manager.addTag(e1); { - auto& vel = manager.getEntityData(e0); - vel.vx = 1; - vel.vy = 1; + auto* vel = manager.getEntityData(e0); + vel->vx = 1; + vel->vy = 1; } - auto posUpdate = [] (std::size_t id, C0& pos, C1& vel) { - pos.x += vel.vx; - pos.y += vel.vy; + auto posUpdate = [] (std::size_t id, C0* pos, C1* vel) { + pos->x += vel->vx; + pos->y += vel->vy; }; - auto updateTag = [] (std::size_t id, C0& pos, C1& vel) { - pos.x = pos.y = vel.vx = vel.vy = 0; + auto updateTag = [] (std::size_t id, C0* pos, C1* vel) { + pos->x = pos->y = vel->vx = vel->vy = 0; }; manager.forMatchingSignature >(posUpdate); @@ -116,9 +116,9 @@ TEST(EC, Manager) manager.forMatchingSignature >(updateTag); { - auto& pos = manager.getEntityData(e0); - EXPECT_EQ(pos.x, 7); - EXPECT_EQ(pos.y, 7); + auto* pos = manager.getEntityData(e0); + EXPECT_EQ(pos->x, 7); + EXPECT_EQ(pos->y, 7); } { @@ -166,9 +166,10 @@ TEST(EC, MoveComponentWithUniquePtr) int x = 0; int y = 0; - manager.forMatchingSignature >([&x, &y] (std::size_t eID, C0Ptr& ptr) { - x = ptr->x; - y = ptr->y; + manager.forMatchingSignature >([&x, &y] + (std::size_t eID, C0Ptr* ptr) { + x = (*ptr)->x; + y = (*ptr)->y; }); EXPECT_EQ(5, x); EXPECT_EQ(10, y); @@ -185,8 +186,8 @@ TEST(EC, MoveComponentWithUniquePtr) int result = 0; - auto getResultFunction = [&result] (std::size_t eID, TestPtr& ptr) { - result = ptr->getInt(); + auto getResultFunction = [&result] (std::size_t eID, TestPtr* ptr) { + result = (*ptr)->getInt(); }; manager.forMatchingSignature >(getResultFunction); @@ -244,32 +245,34 @@ TEST(EC, FunctionStorage) manager.addComponent(eid); manager.addComponent(eid); - auto f0index = manager.addForMatchingFunction>( [] (std::size_t eid, C0& c0) { - ++c0.x; - ++c0.y; + auto f0index = manager.addForMatchingFunction>( + [] (std::size_t eid, C0* c0) { + ++c0->x; + ++c0->y; }); - auto f1index = manager.addForMatchingFunction>( [] (std::size_t eid, C0& c0, C1& c1) { - c1.vx = c0.x + 10; - c1.vy = c1.vy + c1.vx + c0.y + 10; + auto f1index = manager.addForMatchingFunction>( + [] (std::size_t eid, C0* c0, C1* c1) { + c1->vx = c0->x + 10; + c1->vy = c1->vy + c1->vx + c0->y + 10; }); auto f2index = manager.addForMatchingFunction>( - [] (std::size_t eid, C0& c0) { - c0.x = c0.y = 9999; + [] (std::size_t eid, C0* c0) { + c0->x = c0->y = 9999; }); auto f3index = manager.addForMatchingFunction>( - [] (std::size_t eid, C1& c1) { - c1.vx = c1.vy = 10000; + [] (std::size_t eid, C1* c1) { + c1->vx = c1->vy = 10000; }); EXPECT_EQ(2, manager.removeSomeMatchingFunctions({f2index, f3index})); auto f4index = manager.addForMatchingFunction>( - [] (std::size_t eid, C0& c0) { - c0.x = 999; - c0.y = 888; + [] (std::size_t eid, C0* c0) { + c0->x = 999; + c0->y = 888; }); { @@ -278,9 +281,9 @@ TEST(EC, FunctionStorage) } auto f5index = manager.addForMatchingFunction>( - [] (std::size_t eid, C0& c0) { - c0.x = 777; - c0.y = 666; + [] (std::size_t eid, C0* c0) { + c0->x = 777; + c0->y = 666; }); auto lastIndex = f5index; @@ -293,33 +296,33 @@ TEST(EC, FunctionStorage) manager.callForMatchingFunctions(); { - auto c0 = manager.getEntityData(eid); + auto* c0 = manager.getEntityData(eid); - EXPECT_EQ(1, c0.x); - EXPECT_EQ(2, c0.y); + EXPECT_EQ(1, c0->x); + EXPECT_EQ(2, c0->y); - auto c1 = manager.getEntityData(eid); + auto* c1 = manager.getEntityData(eid); - EXPECT_EQ(11, c1.vx); - EXPECT_EQ(23, c1.vy); + EXPECT_EQ(11, c1->vx); + EXPECT_EQ(23, c1->vy); } EXPECT_TRUE(manager.callForMatchingFunction(f0index)); EXPECT_FALSE(manager.callForMatchingFunction(lastIndex + 1)); { - auto& c0 = manager.getEntityData(eid); + auto* c0 = manager.getEntityData(eid); - EXPECT_EQ(2, c0.x); - EXPECT_EQ(3, c0.y); + EXPECT_EQ(2, c0->x); + EXPECT_EQ(3, c0->y); - c0.x = 1; - c0.y = 2; + c0->x = 1; + c0->y = 2; - auto c1 = manager.getEntityData(eid); + auto* c1 = manager.getEntityData(eid); - EXPECT_EQ(11, c1.vx); - EXPECT_EQ(23, c1.vy); + EXPECT_EQ(11, c1->vx); + EXPECT_EQ(23, c1->vy); } EXPECT_EQ(1, manager.keepSomeMatchingFunctions({f1index})); @@ -340,15 +343,15 @@ TEST(EC, FunctionStorage) manager.callForMatchingFunctions(); { - auto c0 = manager.getEntityData(eid); + auto* c0 = manager.getEntityData(eid); - EXPECT_EQ(1, c0.x); - EXPECT_EQ(2, c0.y); + EXPECT_EQ(1, c0->x); + EXPECT_EQ(2, c0->y); - auto c1 = manager.getEntityData(eid); + auto* c1 = manager.getEntityData(eid); - EXPECT_EQ(11, c1.vx); - EXPECT_EQ(46, c1.vy); + EXPECT_EQ(11, c1->vx); + EXPECT_EQ(46, c1->vy); } EXPECT_TRUE(manager.removeForMatchingFunction(f1index)); @@ -357,30 +360,30 @@ TEST(EC, FunctionStorage) manager.callForMatchingFunctions(); { - auto c0 = manager.getEntityData(eid); + auto* c0 = manager.getEntityData(eid); - EXPECT_EQ(1, c0.x); - EXPECT_EQ(2, c0.y); + EXPECT_EQ(1, c0->x); + EXPECT_EQ(2, c0->y); - auto c1 = manager.getEntityData(eid); + auto* c1 = manager.getEntityData(eid); - EXPECT_EQ(11, c1.vx); - EXPECT_EQ(46, c1.vy); + EXPECT_EQ(11, c1->vx); + EXPECT_EQ(46, c1->vy); } manager.clearForMatchingFunctions(); manager.callForMatchingFunctions(); { - auto c0 = manager.getEntityData(eid); + auto* c0 = manager.getEntityData(eid); - EXPECT_EQ(1, c0.x); - EXPECT_EQ(2, c0.y); + EXPECT_EQ(1, c0->x); + EXPECT_EQ(2, c0->y); - auto c1 = manager.getEntityData(eid); + auto* c1 = manager.getEntityData(eid); - EXPECT_EQ(11, c1.vx); - EXPECT_EQ(46, c1.vy); + EXPECT_EQ(11, c1->vx); + EXPECT_EQ(46, c1->vy); } } @@ -410,22 +413,22 @@ TEST(EC, MultiThreaded) { manager.addEntity(); manager.addComponent(i, 0, 0); - EXPECT_EQ(0, manager.getEntityData(i).x); - EXPECT_EQ(0, manager.getEntityData(i).y); + EXPECT_EQ(0, manager.getEntityData(i)->x); + EXPECT_EQ(0, manager.getEntityData(i)->y); } manager.forMatchingSignature >( - [] (const std::size_t& eid, C0& c) { - c.x = 1; - c.y = 2; + [] (const std::size_t& eid, C0* c) { + c->x = 1; + c->y = 2; }, 2 ); for(unsigned int i = 0; i < 17; ++i) { - EXPECT_EQ(1, manager.getEntityData(i).x); - EXPECT_EQ(2, manager.getEntityData(i).y); + EXPECT_EQ(1, manager.getEntityData(i)->x); + EXPECT_EQ(2, manager.getEntityData(i)->y); } for(unsigned int i = 3; i < 17; ++i) @@ -435,22 +438,22 @@ TEST(EC, MultiThreaded) for(unsigned int i = 0; i < 3; ++i) { - EXPECT_EQ(1, manager.getEntityData(i).x); - EXPECT_EQ(2, manager.getEntityData(i).y); + EXPECT_EQ(1, manager.getEntityData(i)->x); + EXPECT_EQ(2, manager.getEntityData(i)->y); } manager.forMatchingSignature >( - [] (const std::size_t& eid, C0& c) { - c.x = 3; - c.y = 4; + [] (const std::size_t& eid, C0* c) { + c->x = 3; + c->y = 4; }, 8 ); for(unsigned int i = 0; i < 3; ++i) { - EXPECT_EQ(3, manager.getEntityData(i).x); - EXPECT_EQ(4, manager.getEntityData(i).y); + EXPECT_EQ(3, manager.getEntityData(i)->x); + EXPECT_EQ(4, manager.getEntityData(i)->y); } manager.reset(); @@ -459,14 +462,14 @@ TEST(EC, MultiThreaded) { manager.addEntity(); manager.addComponent(i, 0, 0); - EXPECT_EQ(0, manager.getEntityData(i).x); - EXPECT_EQ(0, manager.getEntityData(i).y); + EXPECT_EQ(0, manager.getEntityData(i)->x); + EXPECT_EQ(0, manager.getEntityData(i)->y); } auto f0 = manager.addForMatchingFunction >( - [] (const std::size_t& eid, C0& c) { - c.x = 1; - c.y = 2; + [] (const std::size_t& eid, C0* c) { + c->x = 1; + c->y = 2; } ); @@ -474,14 +477,14 @@ TEST(EC, MultiThreaded) for(unsigned int i = 0; i < 17; ++i) { - EXPECT_EQ(1, manager.getEntityData(i).x); - EXPECT_EQ(2, manager.getEntityData(i).y); + EXPECT_EQ(1, manager.getEntityData(i)->x); + EXPECT_EQ(2, manager.getEntityData(i)->y); } auto f1 = manager.addForMatchingFunction >( - [] (const std::size_t& eid, C0& c) { - c.x = 3; - c.y = 4; + [] (const std::size_t& eid, C0* c) { + c->x = 3; + c->y = 4; } ); @@ -489,8 +492,8 @@ TEST(EC, MultiThreaded) for(unsigned int i = 0; i < 17; ++i) { - EXPECT_EQ(3, manager.getEntityData(i).x); - EXPECT_EQ(4, manager.getEntityData(i).y); + EXPECT_EQ(3, manager.getEntityData(i)->x); + EXPECT_EQ(4, manager.getEntityData(i)->y); } for(unsigned int i = 4; i < 17; ++i) @@ -502,16 +505,16 @@ TEST(EC, MultiThreaded) for(unsigned int i = 0; i < 4; ++i) { - EXPECT_EQ(1, manager.getEntityData(i).x); - EXPECT_EQ(2, manager.getEntityData(i).y); + EXPECT_EQ(1, manager.getEntityData(i)->x); + EXPECT_EQ(2, manager.getEntityData(i)->y); } manager.callForMatchingFunction(f1, 8); for(unsigned int i = 0; i < 4; ++i) { - EXPECT_EQ(3, manager.getEntityData(i).x); - EXPECT_EQ(4, manager.getEntityData(i).y); + EXPECT_EQ(3, manager.getEntityData(i)->x); + EXPECT_EQ(4, manager.getEntityData(i)->y); } } @@ -540,9 +543,9 @@ TEST(EC, ForMatchingSignatures) manager.addComponent(id); manager.addTag(id); - auto& c1 = manager.getEntityData(id); - c1.vx = 0; - c1.vy = 0; + auto* c1 = manager.getEntityData(id); + c1->vx = 0; + c1->vy = 0; } else { @@ -556,21 +559,21 @@ TEST(EC, ForMatchingSignatures) TypeList, TypeList > >( std::make_tuple( - [] (std::size_t eid, C0& c) { - EXPECT_EQ(c.x, 0); - EXPECT_EQ(c.y, 0); - c.x = 1; - c.y = 1; + [] (std::size_t eid, C0* c) { + EXPECT_EQ(c->x, 0); + EXPECT_EQ(c->y, 0); + c->x = 1; + c->y = 1; }, - [] (std::size_t eid, C0& c0, C1& c1) { - EXPECT_EQ(c0.x, 1); - EXPECT_EQ(c0.y, 1); - EXPECT_EQ(c1.vx, 0); - EXPECT_EQ(c1.vy, 0); - c1.vx = c0.x; - c1.vy = c0.y; - c0.x = 2; - c0.y = 2; + [] (std::size_t eid, C0* c0, C1* c1) { + EXPECT_EQ(c0->x, 1); + EXPECT_EQ(c0->y, 1); + EXPECT_EQ(c1->vx, 0); + EXPECT_EQ(c1->vy, 0); + c1->vx = c0->x; + c1->vy = c0->y; + c0->x = 2; + c0->y = 2; }) ); @@ -578,15 +581,15 @@ TEST(EC, ForMatchingSignatures) { if(id != first && id != last) { - EXPECT_EQ(2, manager.getEntityData(id).x); - EXPECT_EQ(2, manager.getEntityData(id).y); - EXPECT_EQ(1, manager.getEntityData(id).vx); - EXPECT_EQ(1, manager.getEntityData(id).vy); + EXPECT_EQ(2, manager.getEntityData(id)->x); + EXPECT_EQ(2, manager.getEntityData(id)->y); + EXPECT_EQ(1, manager.getEntityData(id)->vx); + EXPECT_EQ(1, manager.getEntityData(id)->vy); } else { - EXPECT_EQ(1, manager.getEntityData(id).x); - EXPECT_EQ(1, manager.getEntityData(id).y); + EXPECT_EQ(1, manager.getEntityData(id)->x); + EXPECT_EQ(1, manager.getEntityData(id)->y); } } @@ -609,58 +612,58 @@ TEST(EC, ForMatchingSignatures) > ( std::make_tuple( - [&first, &last, &cx, &cy, &cxM, &cyM] (std::size_t eid, C0& c) { + [&first, &last, &cx, &cy, &cxM, &cyM] (std::size_t eid, C0* c) { if(eid != first && eid != last) { { std::lock_guard guard(cxM); - cx.insert(std::make_pair(eid, c.x)); + cx.insert(std::make_pair(eid, c->x)); } { std::lock_guard guard(cyM); - cy.insert(std::make_pair(eid, c.y)); + cy.insert(std::make_pair(eid, c->y)); } - c.x = 5; - c.y = 7; + c->x = 5; + c->y = 7; } else { { std::lock_guard guard(cxM); - cx.insert(std::make_pair(eid, c.x)); + cx.insert(std::make_pair(eid, c->x)); } { std::lock_guard guard(cyM); - cy.insert(std::make_pair(eid, c.y)); + cy.insert(std::make_pair(eid, c->y)); } - c.x = 11; - c.y = 13; + c->x = 11; + c->y = 13; } }, [&c0x, &c0y, &c1vx, &c1vy, &c0xM, &c0yM, &c1vxM, &c1vyM] - (std::size_t eid, C0& c0, C1& c1) { + (std::size_t eid, C0* c0, C1* c1) { { std::lock_guard guard(c0xM); - c0x.insert(std::make_pair(eid, c0.x)); + c0x.insert(std::make_pair(eid, c0->x)); } { std::lock_guard guard(c0yM); - c0y.insert(std::make_pair(eid, c0.y)); + c0y.insert(std::make_pair(eid, c0->y)); } { std::lock_guard guard(c1vxM); - c1vx.insert(std::make_pair(eid, c1.vx)); + c1vx.insert(std::make_pair(eid, c1->vx)); } { std::lock_guard guard(c1vyM); - c1vy.insert(std::make_pair(eid, c1.vy)); + c1vy.insert(std::make_pair(eid, c1->vy)); } - c1.vx += c0.x; - c1.vy += c0.y; + c1->vx += c0->x; + c1->vy += c0->y; - c0.x = 1; - c0.y = 2; + c0->x = 1; + c0->y = 2; }), 3 ); @@ -709,15 +712,15 @@ TEST(EC, ForMatchingSignatures) { if(eid != first && eid != last) { - EXPECT_EQ(1, manager.getEntityData(eid).x); - EXPECT_EQ(2, manager.getEntityData(eid).y); - EXPECT_EQ(6, manager.getEntityData(eid).vx); - EXPECT_EQ(8, manager.getEntityData(eid).vy); + EXPECT_EQ(1, manager.getEntityData(eid)->x); + EXPECT_EQ(2, manager.getEntityData(eid)->y); + EXPECT_EQ(6, manager.getEntityData(eid)->vx); + EXPECT_EQ(8, manager.getEntityData(eid)->vy); } else { - EXPECT_EQ(11, manager.getEntityData(eid).x); - EXPECT_EQ(13, manager.getEntityData(eid).y); + EXPECT_EQ(11, manager.getEntityData(eid)->x); + EXPECT_EQ(13, manager.getEntityData(eid)->y); } } @@ -726,27 +729,27 @@ TEST(EC, ForMatchingSignatures) TypeList, TypeList > >( std::make_tuple( - [] (std::size_t eid, C0& c0, C1& c1) { - c0.x = 9999; - c0.y = 9999; - c1.vx = 9999; - c1.vy = 9999; + [] (std::size_t eid, C0* c0, C1* c1) { + c0->x = 9999; + c0->y = 9999; + c1->vx = 9999; + c1->vy = 9999; }, - [] (std::size_t eid, C0& c0, C1& c1) { - c0.x = 10000; - c0.y = 10000; - c1.vx = 10000; - c1.vy = 10000; + [] (std::size_t eid, C0* c0, C1* c1) { + c0->x = 10000; + c0->y = 10000; + c1->vx = 10000; + c1->vy = 10000; } )); for(auto id : e) { if(id != first && id != last) { - EXPECT_EQ(10000, manager.getEntityData(id).x); - EXPECT_EQ(10000, manager.getEntityData(id).y); - EXPECT_EQ(10000, manager.getEntityData(id).vx); - EXPECT_EQ(10000, manager.getEntityData(id).vy); + EXPECT_EQ(10000, manager.getEntityData(id)->x); + EXPECT_EQ(10000, manager.getEntityData(id)->y); + EXPECT_EQ(10000, manager.getEntityData(id)->vx); + EXPECT_EQ(10000, manager.getEntityData(id)->vy); } }; } @@ -782,17 +785,17 @@ TEST(EC, forMatchingPtrs) } } - const auto func0 = [] (std::size_t eid, C0& c0, C1& c1) + const auto func0 = [] (std::size_t eid, C0* c0, C1* c1) { - c0.x = 1; - c0.y = 2; - c1.vx = 3; - c1.vy = 4; + c0->x = 1; + c0->y = 2; + c1->vx = 3; + c1->vy = 4; }; - const auto func1 = [] (std::size_t eid, C0& c0) + const auto func1 = [] (std::size_t eid, C0* c0) { - c0.x = 11; - c0.y = 12; + c0->x = 11; + c0->y = 12; }; using namespace EC::Meta; @@ -808,24 +811,24 @@ TEST(EC, forMatchingPtrs) { if(eid != first && eid != last) { - C0& c0 = manager.getEntityData(eid); - EXPECT_EQ(1, c0.x); - EXPECT_EQ(2, c0.y); - c0.x = 0; - c0.y = 0; - C1& c1 = manager.getEntityData(eid); - EXPECT_EQ(3, c1.vx); - EXPECT_EQ(4, c1.vy); - c1.vx = 0; - c1.vy = 0; + C0* c0 = manager.getEntityData(eid); + EXPECT_EQ(1, c0->x); + EXPECT_EQ(2, c0->y); + c0->x = 0; + c0->y = 0; + C1* c1 = manager.getEntityData(eid); + EXPECT_EQ(3, c1->vx); + EXPECT_EQ(4, c1->vy); + c1->vx = 0; + c1->vy = 0; } else { - C0& c = manager.getEntityData(eid); - EXPECT_EQ(11, c.x); - EXPECT_EQ(12, c.y); - c.x = 0; - c.y = 0; + C0* c = manager.getEntityData(eid); + EXPECT_EQ(11, c->x); + EXPECT_EQ(12, c->y); + c->x = 0; + c->y = 0; } } @@ -839,39 +842,39 @@ TEST(EC, forMatchingPtrs) { if(eid != first && eid != last) { - C0& c0 = manager.getEntityData(eid); - EXPECT_EQ(1, c0.x); - EXPECT_EQ(2, c0.y); - c0.x = 0; - c0.y = 0; - C1& c1 = manager.getEntityData(eid); - EXPECT_EQ(3, c1.vx); - EXPECT_EQ(4, c1.vy); - c1.vx = 0; - c1.vy = 0; + C0* c0 = manager.getEntityData(eid); + EXPECT_EQ(1, c0->x); + EXPECT_EQ(2, c0->y); + c0->x = 0; + c0->y = 0; + C1* c1 = manager.getEntityData(eid); + EXPECT_EQ(3, c1->vx); + EXPECT_EQ(4, c1->vy); + c1->vx = 0; + c1->vy = 0; } else { - C0& c = manager.getEntityData(eid); - EXPECT_EQ(11, c.x); - EXPECT_EQ(12, c.y); - c.x = 0; - c.y = 0; + C0* c = manager.getEntityData(eid); + EXPECT_EQ(11, c->x); + EXPECT_EQ(12, c->y); + c->x = 0; + c->y = 0; } } // test duplicate signatures - const auto setTo9999 = [] (std::size_t eid, C0& c0, C1& c1) { - c0.x = 9999; - c0.y = 9999; - c1.vx = 9999; - c1.vy = 9999; + const auto setTo9999 = [] (std::size_t eid, C0* c0, C1* c1) { + c0->x = 9999; + c0->y = 9999; + c1->vx = 9999; + c1->vy = 9999; }; - const auto setTo10000 = [] (std::size_t eid, C0& c0, C1& c1) { - c0.x = 10000; - c0.y = 10000; - c1.vx = 10000; - c1.vy = 10000; + const auto setTo10000 = [] (std::size_t eid, C0* c0, C1* c1) { + c0->x = 10000; + c0->y = 10000; + c1->vx = 10000; + c1->vy = 10000; }; manager.forMatchingSignaturesPtr, @@ -884,10 +887,10 @@ TEST(EC, forMatchingPtrs) { if(id != first && id != last) { - EXPECT_EQ(10000, manager.getEntityData(id).x); - EXPECT_EQ(10000, manager.getEntityData(id).y); - EXPECT_EQ(10000, manager.getEntityData(id).vx); - EXPECT_EQ(10000, manager.getEntityData(id).vy); + EXPECT_EQ(10000, manager.getEntityData(id)->x); + EXPECT_EQ(10000, manager.getEntityData(id)->y); + EXPECT_EQ(10000, manager.getEntityData(id)->vx); + EXPECT_EQ(10000, manager.getEntityData(id)->vy); } }; } diff --git a/src/test/MetaTest.cpp b/src/test/MetaTest.cpp index 6dcf850..02f966c 100644 --- a/src/test/MetaTest.cpp +++ b/src/test/MetaTest.cpp @@ -77,17 +77,21 @@ TEST(Meta, IndexOf) EXPECT_EQ(index, 2); index = EC::Meta::IndexOf::value; EXPECT_EQ(index, 3); + index = EC::Meta::IndexOf::value; + EXPECT_EQ(index, 4); index = EC::Meta::IndexOf::value; EXPECT_EQ(index, 0); index = EC::Meta::IndexOf::value; EXPECT_EQ(index, 1); + index = EC::Meta::IndexOf::value; + EXPECT_EQ(index, 2); } TEST(Meta, Bitset) { EC::Bitset bitset; - EXPECT_EQ(bitset.size(), ListComponentsAll::size + ListTagsAll::size); + EXPECT_EQ(bitset.size(), ListComponentsAll::size + ListTagsAll::size + 1); bitset[EC::Meta::IndexOf::value] = true; EXPECT_TRUE(bitset.getComponentBit());