Well, due to the demands of people on #flipcode, I'm updating my techfile. Under the slew of yo' momma jokes which we spew forth in that humblest of forums (who would have known aggrav8d's momma's address is "This Side Up"), I was forced to do it :) Unfortunately however, I'm not going to go on about stuff I'm working on really, as it is stuff for the commercial project I am currently on. However, I'm still going to ramble on about my thoughts on various subjects that you all love.
First order of business is the sad (and hopefully temporary) depature of the All Rockin', All the Time site www.graphicspapers.com's (or the artist formerly known as www.grafix3d.tzo.com) exquisite paper Archive, due to the possibility of Copyright problems. For the amount of work MidNight has done in helping the community here, it is very much a shame to see his work put to the sword of civil liability. Something needs to be done, and if MidNight can be helped in anyway getting his site back up, I for one will donate my services.
Now, on to the nitty-gritty of the technical nick-nacks and thingamebobs.
What I'm going to be rambling about today, is a much neglected feature of C++. I'm talking about namespaces. Chances are, unless you've done a lot of C++, you don't even know what a namespace is. Well, it is assumed knowledge for this part of the techfile. MSDN has a few things on namespaces, just look up "namespace". Your C++ references should have them too.
Anyway, many people who do know namespaces, know only one function for which they can be used, which is avoiding clashing names in a library, or multi-author code. The other common use of namespaces is to declare large amounts of code "static" at once, by using an unnamed named space (ironic that.), which keep file scope. This can allow you to name anything from classes and structs to other namespaces static, meaning there definition only has file scope.
A less known use is that of grouping for similar use. That is, because your different namespaces can contain identifiers of the same name, you can group to lots of same named but different identifiers. For example, I have two abstract base classes (call them base1 and base2). I have around 5 different groups that contain two classes derived from base1 and three from base2 each. Now, if I define them all in the global scope, I have to use different names, and in choosing between groups, I must each time init each groups 5 classes, which means 5 lots of initialising 5 classes. When you count other setup code out side of constructors, this can begin to get fairly hefty.
However, if I use namespaces, it is possible to put the initialisation code into a macro, and save writing different versions of the same code. For example, we define a group like so (we can define the class out of the namespace, by using the scope resolution operator ::, or the keyword "using").
We can then define the group functionality into a macro like so:
And hence your large case case statement now becomes relatively small. In fact, in many cases we don't even have to rely on polymorphism here, just classes/members/functions/variables with the same name. This method can be used to perform group behaviours, and save rewriting the same code many times. You can write a couple of group methods to save work.
Now, it wouldn't be my techfile unless I suggested something new and radical. The idea is fairly simple, and that is allowing indexable namespacing. That is, each namespace gains a token (the first being zero) at compile time, which indexes an array of namespaces. This would again simplify this sort of code. Although, cautious programming would be needed not to make errors and index the wrong namespace. So, I recommend that another change be made to allow derivitive namespaces, and allow overriding of members (similar to polymorphism, and the virtual classes idea put forward by Tim Sweeney.
By the way, I recently participated in my first multi-Developer to public moderated chat. Talk about a surreal experience.