Hi guys, I'm having some trouble understanding what this code is doing, can you give me a hand ?_?
Code by Vittorio Romeo, you can find the whole thing here
So the part I'm having trouble with is this one:
// Another useful method will allow the user to execute arbitrary
// code on all entities of a certain type.
template <typename T, typename TFunc>
void forEach(const TFunc& mFunc)
{
// Retrieve all entities of type `T`.
auto& vector(getAll<T>());
// For each pointer in the entity vector, simply cast the
// pointer to its "real" type then call the function with the
// casted pointer, dereferenced.
for(auto ptr : vector) mFunc(*reinterpret_cast<T*>(ptr));
}
and
// The game logic is now much more generic: we ask the
// manager to give us all instances of a certain game
// object type, then we run the collision functions.
// This is very flexible as we can have any number
// of balls and bricks, and adding new types of game
// objects is extremely easy.
manager.forEach<Ball>([this](auto& mBall)
{
manager.forEach<Brick>([this, &mBall](auto& mBrick)
{
solveBrickBallCollision(mBrick, mBall);
});
manager.forEach<Paddle>([this, &mBall](auto& mPaddle)
{
solvePaddleBallCollision(mPaddle, mBall);
});
});
I get that forEach<T>(Tfunc) retrieves a vector<BaseClass*>& ,casts every element in it from the BaseClass pointer to a T* and apply the TFunc passed to them, but I kind of get lost, I mean why the lambda passed to it is capturing [this], and even what is [this] in that context...?!
Is it the Game class that owns the manager data member?
Or 'this' is becoming something else inside the forEach function (meaning that it acts inside of it since is a template argument, capturing the "this" inside of forEach, which I believe should be the manager itself?).
And then, I don't see 'this' used anywhere, so...just very confused. Can you help me understand?