As I developed the Entity-Component System I came across an interesting problem. I keep all my components on a list, there’s no “Entity” object, the only thing that components share is an ID, the ID of their entity. The list is a binary search tree for fast access to groups of components, whenever I need an entity, is just a matter of calling equal_range(entity) and I have a std::pair<set::iterator,set::iterator> of all the components on owned by that entity. The problem with that, is that it’s not readily obvious which components an entity owns, and systems need this information. Since I was just prototyping at first, my solution was simple:
void System::update(cset& components, float delta)
{
for(cpair entity : components)
{
Position* position = nullptr;
Sprite* sprite = nullptr;
for(auto it = entity.first; it != entity.second; ++it)
{
if(typeid(**it) == typeid(Position)) position = *it;
else if(typeid(**it) == typeid(Sprite)) sprite = *it;
}
if(!position || !sprite) return;
//[...] do something with position and sprite here
}
}
Which was fine and worked, I had a small number of entities so performance wasn’t a requirement, eventually though, I had 4 systems with that same code, and every system would need that code, so it made sense that the System class should have that functionality. In the end I setled for the approach below, which I think will solve my problems.
template <class ... Required>
class Signature : public System
{
private:
typedef std::vector<std::pair<size_t, Component**>> vector_type;
template <class First, class ... Rest>
void _set_vector_of_types(vector_type& vector, First** f, Rest** ... r)
{
vector.push_back(std::make_pair(typeid(First).hash_code(),(Component**)f));
return _set_vector_of_types(vector, r...);
}
void _set_vector_of_types(vector_type& vector)
{
return;
}
public:
template <class ... Optional>
bool get_signature(cpair pair, Required** ... r, Optional** ... o)
{
vector_type required;
vector_type optional;
int req = sizeof...(Required);
_set_vector_of_types(required, r...);
_set_vector_of_types(optional, o...);
for(auto it = pair.first; it != pair.second; ++it)
{
for(auto t : required)
{
if(typeid(**it).hash_code() == t.first)
{
*t.second = *it;
--req;
}
}
for(auto t : optional)
{
if(typeid(**it).hash_code() == t.first)
{
*t.second = *it;
}
}
}
return !req;
}
};
And the interface for that is really cool, first you inherit that class choosing classes as a signature:
class RenderSystem : public Signature<Model, Position>
And then you can just call get_signature like this:
Model* model = nullptr;
Position* position = nullptr;
Material* material = nullptr;
if(get_signature(pair, &model, &position, &material))
{
// [...] do stuff
}
As you can see, I can even request optional components, like material, which the system will supply if the entity doesn’t have one. I’m really happy with how this turned out to, variadic templates are really cool.

Entity camera = scene.create_entity();
scene.add_component<Spatial>(camera, vec3(0,0,-5), vec3(1), vec3(0,180,0));
scene.add_component<Camera>(camera, 65.f, 800.f/600.f, 0.01f, 100.f);
Entity b = scene.create_entity();
scene.add_component<Spatial>(b, vec3(1,0,0), vec3(1.5,1,1));
scene.add_component<Model>(b, VertexData::SimpleSquare());
scene.add_component<Material>(b, new Shader("assets/shaders/andre.shader"))
->set_image("texture", new ImageData("assets/images/grass.png"));
Entity c = scene.create_entity();
scene.add_component<Spatial>(c, vec3(-1,0,0));
scene.add_component<Model>(c, VertexData::SimpleSquare()); scene.add_component<Material>(c, new Shader("assets/shaders/andre.shader"))
->set_image("texture", new ImageData("assets/images/fire.png"));
Entity a = scene.create_entity();
scene.add_component<Spatial>(a, vec3(0,0,-1), vec3(1,1,1), vec3(0,0,45));
scene.add_component<Model>(a, VertexData::SimpleTriangle()); Material* mat_a =
scene.add_component<Material>(a, new Shader("assets/shaders/image_mix.shader"));
mat_a->set_image("texture1", new ImageData("assets/images/water.png"));
mat_a->set_image("texture2", new ImageData("assets/images/grass.png"));
mat_a->set_value("mix_factor",0.2);
For quite some time now, I decided to really learn C++. I, obviously, had no idea what I was getting myself into. It’s not that chose to learn things in an specific order, I just stumbled upon several “new” things about the language and couldn’t stop learning. One of the first things was const correctness, which is basically a way to tell the compiler about your intentions with your methods, it’s also nice for self documenting code, when a method is const you can be sure calling it won’t change the object itself. This was easy, I can write all my code to be const correct now.
Next, I started learning about references. It’s amazing how important references are in the language but you don’t learn about them at school. And learning about references, I also had to learn about copy constructors, if you use a const reference for a parameter, you don’t have to copy your object, saving some processing from the copy.) Since my code was already const correct I didn’t have to go back and make everything const just so I could use my objects. I rarely invoke a copy constructor now, unless my class is going to be kept in a STL container (as a key, since keys can’t be references, or as an object when it is not a pointer container) there’s no need for anything other than a const reference.
After that I learned about virtual methods. Another really important thing that I didn’t know about. I mean, I had several children classes on a Base class pointer and I had no idea about virtual dctor’s (in retrospect, I mostly had no idea about memory management.) Because of this I went to learn how to better manage my memory (all I knew about that was that I should call delete for every new.) I learned about scope, about how “normal” variables delete themselves at the end of the scope, but new’ed memory lives on, I learned about references deleting themselves and I learned how to clean up properly. Now most of my memory trouble is about who should delete the pointer data, which brings us to my next point.
Smart pointers. Smart pointers are the way to use pointers in a more automatic way. Like auto_ptr that calls delete on the data when it goes out of scope, and sets its pointer to null when you copy it, so it wont call delete when another auto_ptr might have called already. There’s shared_ptr which keeps a reference count and increase/decrease when a copy is made/goes out of scope. There are other too, for other uses.
I’m pretty sure I don’t know half of the language, and C++11 is here to make me suffer (new type of for, for_each, std::function, std::bind, std::thread, etc, etc, etc). Wish me luck!