flipCode - Tech File - Conor Stokes [an error occurred while processing this directive]
Conor Stokes
(aka DirtyPunk)
Click the name for some bio info

E-Mail: cstokes@crytek.com
http://www.claustrophobe.8m.com



   02/13/2000, A Life Time of Learning, Teaching and Eating M&Ms


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").

 
namespace group1 {
 class class1 : public base1 {};
 class class2 : public base1 {};
 class class3 : public base2 {};
 class class4 : public base2 {};
 class class5 : public base2 {};
};

We can then define the group functionality into a macro like so:

 
#define GroupInit(groupname,structname) \
		using namespace groupname; \
		structname->obj1=new class1; \
		structname->obj2=new class2; \
		structname->obj3=new class3; \
		structname->obj4=new class4; \
		structname->obj5=new class5; 

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.

Conor Stokes





  • 12/29/2000 - Techfile From Somewhere Different
  • 10/10/2000 - Some Fun, And A Cameo Appearance
  • 08/10/2000 - Déjà vu - And I've Done It Before
  • 07/08/2000 - Various Loose Ends To Hang
  • 05/15/2000 - The Way To Hit A Ball With A Bat. Or Not
  • 03/28/2000 - The Fine Art Of
  • 02/13/2000 - A Life Time of Learning, Teaching and Eating M&Ms
  • 12/09/1999 - Strangeness And Wondering If You Are Taking Innovation A Tad Too Far
  • 11/12/1999 - How to Break Exam Tension? Update Your Techfile
  • 09/14/1999 - Lots of Ramblings, personal things and comments on why SNFU
  • 08/23/1999 - Trials and Tribulations of Being Cerebrally Defunct
  • 07/29/1999 - Quick Update about Stuff and Things
  • 07/25/1999 - I'm Back Baby
  • 07/01/1999 - Is it so? Or am I just a Psycho Babbling Mental Hobo, who's Brain has No Home?
  • 06/25/1999 - Another Couple of Things
  • 06/17/1999 - I Am A Naughty Little Boy ;( But I Have A Way To Make Up
  • 06/16/1999 - What the hell? A new data structure for visibility? I don't know, I haven't heard of it.
  • 06/05/1999 - A Little Right Brained
  • 05/12/1999 - A Couple O Things
  • 05/08/1999 - Pre Computable Nice Visibility Sets
  • 05/04/1999 - More on Volumetrics
  • 04/30/1999 - Generic Update
  • 04/27/1999 - Spherical Volumetric Rendering (Mapping)
  • 04/25/1999 - Fractal Curves, Emulation Of Nature
  • 04/25/1999 - Claustrophobic Irony Level Loading and Manufacture
  • 04/24/1999 - Visibility Ramblings
  • 04/21/1999 - Why Software Rendering Is Not Dead
  • 04/17/1999 - Optimizing For Specific 3D Hardware

  • This document may not be reproduced in any way without explicit permission from the author and flipCode. All Rights Reserved. Best viewed at a high resolution. The views expressed in this document are the views of the author and NOT neccesarily of anyone else associated with flipCode.

    [an error occurred while processing this directive]