Tuesday, November 27, 2007

Fwd: C++ Globals Registry Problem

I sent this to Programmer's Avenue, however, no one is really talking
much over there... Strange, since that used to be a very active
group.

Perhaps if I ask the same question here I can get a response other than silence.


---------- Forwarded message ----------
From: Chris Miller <lordsauronthegreat@gmail.com>
Date: Nov 25, 2007 9:11 PM
Subject: C++ Globals Registry Problem
To: Programmer's Avenue <progav@googlegroups.com>


I think I may have a good one here.

I've been building my game in C++, since Java is just too difficult to
make work (threading issues with Java AWT rendering, I wasn't about to
rewrite AWT, long story).

So I get to the problem of my global objects, since I can make them in
the right order, but tearing them down is different. I'd like to use
this nifty trick I found at
http://www.cs.utexas.edu/~downing/publications/Globals-JOOP-1996.pdf,
called a registry, which keeps track of the globals in order that they
were created, and tears them down in reverse order. C++ is hard
enough, I'm trying to be extra-sure I have code I can work with in the
long run, since I love this project, and I want it to be the best it
can be. Anyways, I guess I'm just saying I'm trying to maintain an
even higher standard to excellence for this one.

So I understand most of the registry, and I also get that it has a
limit of 16 items to begin with, and cannot grow (it uses the assert()
function to ensure this). I want to re-write this to use an STL
datatype (a vector, though I think it'd be best done in a linked list;
I have yet to pick the data structure, I just know I need something
more than an array).

However, I don't understand all of the device. I'll annotate it to
point out the bits I don't understand, so if you could help, I'd
really appreciate it.

// ----------
// Registry.h
// ----------
class Registry {
public:
Registry() :
iIndex(0) {

Where did the : iIndex(0) come from? I thought that meant extending a
constructor from a base class. I'm unfamiliar with that, what does it
do?

// initialize the function-pointer array
for (int i = 0; i < iSize; i++)
aFunctions[i] = 0;

Where did iSize come from? I only see i, eSize (from the struct) and
iIndex in scope. Is this an error?

}
~Registry() {
// invoke the vDestroy methods in reverse
for (int i = iIndex - 1; i &= 0; i--)
(*aFunctions[i])();
}
// declare a class static pointer for the Registry
static Registry* pTheRegistry;
private:
// define a typedef for the vDestroy methods
typedef void (*FP)();
public:
// define a method to store the function pointers
static void vStore(FP pFunction) {
assert(iIndex < eSize);
aFunctions[iIndex++] = pFunction;
}
private:
// define the size of the array
enum {eSize = 16};

Only one question: why put it inside an enumeration?

// define an index into the array
int iIndex;
// define the function-pointer array
FP aFunctions[eSize];};

I'm looking to replace that with another data structure that can grow
dynamically, however, I want to understand the original code before
mangling it.

// ----------
// Registry.c
// ----------
#include "Registry.h"
// define the class static pointer for the Registry
Registry* Registry::pTheRegistry;

And that I understand. I've been using Google pretty diligently, but
there are a few things I'm hung up on and would appreciate some more
experienced people showing me a few ropes.

Thanks in advance!

--
Registered Linux Addict #431495
http://profile.xfire.com/mrstalinman
John 3:16!

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "C++ Programming" group.
To post to this group, send email to Cpp-Programming@googlegroups.com
To unsubscribe from this group, send email to Cpp-Programming-unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/Cpp-Programming?hl=en
-~----------~----~----~----~------~----~------~--~---

0 Comments:

Post a Comment

<< Home