Advertisement

What is this stuff called in c++

Started by April 26, 2015 10:14 AM
6 comments, last by SteveHatcher 9 years, 8 months ago

I have been using entityx https://github.com/alecthomas/entityx as an ECS framework. I have been following the Examples and looking at some sample projects, and applying what they do to my own engine.

I realize now that I am doing a lot of stuff that I really have no idea whats going on behind the scenes. I also have no clue as to what this stuff is called in c++, so I am asking if you guys can give me a quick rundown on 'keywords' I should be looking up.

Firstly,

class Game : public entityx::Receiver<Game>

Now I would understand if this was class Game : public entityx::Receiver

as Game inheriting from Receiver class, but in this case they use the so called "Curiously recurring template pattern" of which I cant fully understand the Point?

Next,

m_systemManager.update<MovementSystem>(frameTime); <-- What is code like this called in c++?

Somehow this updates everything in my class MovementSystem : public entityx::System<MovementSystem> update function. The update function reads as so:

void update(entityx::EntityManager &entities, entityx::EventManager &events, double dt);

But I only need to pass in my time step (frameTime), so how does it get the other two parameters?

Similarly the first thing I do is add all the systems using

m_systemManager.add<RenderSystem>(&graphics, spriteTextures);

I am just wondering what I can read to understand what is going on behind the scenes.

Edit: One more thing I am not sure of, I see this code:

SplashViewCreator().create(); <-- What is code like this called in c++?

where SplashViewCreator is a class.

I have never seen a class initiated and used without an object of it first being created... e.g. I would expect

SplashViewCreator mySplashViewObject;

mySplashViewObject.create();

Are those two things equivalent?

Thanks for your time.

SystemManager update function probably looks something like this: (C++11)


template<class C>
void SystemManager::update(double frameTime)
{
    for( void * pSomeEntity : entities )
    {
           C * pEntity = dynamic_cast<C>(pSomeEntity);
 
           if( pEntity )
           {
              pEntity->update(entityManager, events, frameTime);
           }
    }
}

entityManager and events being some variables that are part of SystemManager already.

entities being a list of vector or list of a base class that has a virtual update() method

Advertisement

EDIT:

I'm assuming you are aware of the details about templates in C++? If you don't know about templates at all yet, please let me know, I'll rephrase my answers accordingly.


class Game : public entityx::Receiver



Now I would understand if this was class Game : public entityx::Receiver



as Game inheriting from Receiver class, but in this case they use the so called "Curiously recurring template pattern" of which I cant fully understand the Point?

Unless I'm mistaken, EntityX uses the CRTP in this case to generate a unique TypeID for the derived system. This is possible since by inheriting from Receiver<Game>, the compiler generates a seperate template instantiation. Somewhere inside the code, there is a static variable that is incremented every time a certain GetID()-function is called, but only once for every different instantation. Looks like this implementationwise:


class BaseReceiver
{

protected:

	static unsigned int ID_counter;
}

template<typename Derived>
class Receiver : 
	public BaseReceiver
{
	static void GetID(void)
	{
		static unsigned int MyID = ID_counter++; // for every different "Derived", this will be called once, resulting in a unique ID
		return MyID;
	}
}

This ID is later used to look up the system e.g. in this call:

m_systemManager.update<MovementSystem>(frameTime); <-- What is code like this called in c++?

Talking about it, I don't think there is certain name for it, but the functionality is quite straight-forward. You could probably file it under "encapsulation", since you supply minimal information, which is filled out by the manager:


template<typename System>
void SystemManager::Update(double dt)
{
	const auto ID = System::GetID();
	
	BaseSystem* pSystem = GetSystemByID(ID); // accesses a map, or similar
	
	pSystem->Update(*m_pEntities, *m_pEvents, dt); // m_pEntities and m_pEvents are members of the System manager
}

All possible via the magic of CRTP. Also, this is how the System update gets those parameters without you supplying them.

m_systemManager.add<RenderSystem>(&graphics, spriteTextures);

I am just wondering what I can read to understand what is going on behind the scenes.

Well this one uses variadic template, to forward your parameters to the RenderSystem-constructor, so that you don't have to explicitely call new:


template<typename System, typename... Args>
void SystemManager::add(Args&&... args)
{
    System* pSystem = new System(std::forward(args)...);
    
    const auto ID = System::GetID();
    
    StoreSystemByID(ID, *pSystem);
}

Don't try to understand the variadic template syntax unless you have to, because its really quite mindf*cking at first. But you should get the general idea, whatever you pass in as "args" is forwarded to the constructor call of "System", which you specified.


Edit: One more thing I am not sure of, I see this code:



SplashViewCreator().create(); <-- What is code like this called in c++?



where SplashViewCreator is a class.

I have never seen a class initiated and used without an object of it first being created... e.g. I would expect

This does the same as your code. SplashViewCreater() is calling the constructor and returning an object, which you then call the create()-method it. I have never really seen this practially used, and wouldn't advice it (harder to debug since you have to step through constructor and create-call in case something goes wrong), but just to recap, there is an object created, just that it is temporary in this case.

SplashViewCreator().create(); <-- What is code like this called in c++?

where SplashViewCreator is a class.

I have never seen a class initiated and used without an object of it first being created... e.g. I would expect

SplashViewCreator mySplashViewObject;

mySplashViewObject.create();

Are those two things equivalent?

Thanks for your time.

Just so you know what it is called since that is what you asked. It is method chaining, and unlike the previous guys statement it is actually used but depends on the language being used on the frequency of how often you will run across it. To give you an example in the SplashViewCreator() method you would just return (this) and the method needs to be the type of the object i.e. if using statically typed programming languages.


Just so you know what it is called since that is what you asked. It is method chaining, and unlike the previous guys statement it is actually used but depends on the language being used on the frequency of how often you will run across it. To give you an example in the SplashViewCreator() method you would just return (this) and the method needs to be the type of the object i.e. if using statically typed programming languages.

Just to clearify, I didn't mean that method chaining is not used, but more about using this with an empty constructor. Notice that the OP mentioned "where SplashViewCreator is a class", so your comment about int he SplashViewCreator-method is invalid, since SplashViewCreator() is a call to the default constructor of said class. Thats why I didn't even mention method chaining because the OP appearently was merely talking about why this was used on a constructor.

OP wanted to know simple things.

Basically, most of the problems seem from not knowing templates and seeing strange-looking code.

class Game : public entityx::Receiver<Game>

Now I would understand if this was class Game : public entityx::Receiver

as Game inheriting from Receiver class, but in this case they use the so called "Curiously recurring template pattern" of which I cant fully understand the Point?

Its a method of introducing a circular dependency of the template, used as base class, on the type of the class deriving from it, which is supposed to be a clever trick.

Basically, everytime you read "pattern" you can read it as "obscure boilerplate code with a clever marketing name, often (but not always) to work around language deficiencies".

m_systemManager.add<RenderSystem>(&graphics, spriteTextures);

Thats just a templated method call.

SplashViewCreator().create(); <-- What is code like this called in c++?

where SplashViewCreator is a class.

I have never seen a class initiated and used without an object of it first being created... e.g. I would expect

SplashViewCreator mySplashViewObject;

mySplashViewObject.create();

Are those two things equivalent?

Basically, yes. Though it relies on an unnamed temporary, which is then discarded, which is a red flag, telling, you should read the constructor and the method code, as it most likely does things behind your back in a strange way, as usually objects are created to keep them for some use and a constructors job is creating a valid object, so one should not need to rely on an outsourced init/create method.

Advertisement

SplashViewCreator().create(); <-- What is code like this called in c++?

where SplashViewCreator is a class.

I have never seen a class initiated and used without an object of it first being created... e.g. I would expect

SplashViewCreator mySplashViewObject;

mySplashViewObject.create();


Are those two things equivalent?

No.

The equivalent code is this.


{
    SplashViewCreator mySplashViewObject;
    mySplashViewObject.create();
}

Note the scope braces. The SplashViewCreator object is created, its create() member function called, then goes out of scope immediately.

Stephen M. Webb
Professional Free Software Developer

@ProgrammerDX

Yes it is similar to that. Its going to take me quite some time to understand what is going on there though. I need really basic examples that literally just do operations on numbers to make the link to an actual engines implementation.

@Juliean

Thanks a lot for your comprehensive explanation. Definitely clears a lot up for me better than what readings I could find on the net. I understand only what of templates was covered in the C++ book I read (Starting Out with C++ from Control Structures to Objects). I will revise that chapter to clue up on them again since the key things here seem to be templates.

Unless I'm mistaken, EntityX uses the CRTP in this case to generate a unique TypeID for the derived system. This is possible since by inheriting from Receiver<Game>, the compiler generates a seperate template instantiation. Somewhere inside the code, there is a static variable that is incremented every time a certain GetID()-function is called, but only once for every different instantation. Looks like this implementationwise:

Yes it does seem to be doing something like that when I step into the code. Now for once I actually have a clue as to what use the CRTP is.

Reading through the rest of your post while looking at the source code gives me a lot of help so thanks again.

@wicked520

That's the kind of key words I am looking for. Thanks.

@wintertime

It is primarily the strange looking code that I have not seen in my book before, combined with not knowing what this stuff is CALLED that I was struggling with. Your psot gave me a lot of what I needed too.

Thanks everyone I have learnt a lot, and have a lot of reading to do now on these new keywords.

This topic is closed to new replies.

Advertisement