I am looking at the Artemis framework for component-based entity frameworks which is in Java.
A quick bit of background: Component-based entity frameworks are used for developing games by trying to avoid deep hierarchies. This stuff is all pretty new to me but I know both java and c++.
Here is a piece of Java code from the Artemis framework I am struggling to port into c++:
public <T extends Manager> T getManager(Class<T> managerType) {
return managerType.cast(managers.get(managerType));
}
Is this something that c++ can also do? A nice one-liner that casts our object back into the correct sub-class type using templates?
The obvious problem is that we don't have the Class keyword in c++ so I'm struggling with understanding how one would write a "get Manager of class X" function.
In short, is there a way to avoid having to call something similar to this function:
Manager *getManager( string type)
{
return pointer to the base class
}
And later relying on dynamic_cast to get back to a concrete Sub-type, say SpecialManager *manager = dynamic_cast<SpecialManager*>(getManager("special"));
I am a bit worried that I would get myself tangled in this template code for my project because it would require me to implement much of my code using templates.
Since I'm working on a relatively simple project, I am just going to expose my individual managers that are heavily used with pointers that are explicitly set to the correct type outside my managers list.
The data container can still be iterated through normally, but if a part of my program requires a specific manager, I will use a specific function to retreive his pointer directly.
Thanks for the great post. Looks like I need to take a really long look how to use templates to fully understand your code!