Weird (?) problem

mike vertex at gmx.at
Wed Aug 9 12:54:36 PDT 2006


Hi!

I've run into a big problem in my project, it's not really a problem with  
D, more a problem with ... you'll see.

Anyway: I've got a couple of classes in my project which represent type  
information (like MIDI notes, audio output, sample data, etc. - it'll be  
an audio app written in D). Basically it's a reflection system with some  
extras that are specific to this project.

Now, the idea is that every of these type classes has a static field which  
holds a single instance, like that:

' class TypeFoo : TypeBase
' {
'     static TypeBase instance = null;
'     static this() { instance = new TypeFoo(); }
' }

I've got a static opCall to return that instance:

' static TypeBase opCall() { return instance; }

Now everywhere I need to call an overloaded constructor or tell some  
object which type of other objects it can process, I just can call

' TypeFoo()

and get my type class.

Next is, I've got a namespace- or registry-like tree system, where all  
objects are stored into. That's where I store the information of "I can  
process objects of type X" too. It's somehow like:

' getObject("foo").setType(TypeFoo());
' getObject("foo").addCanProcess(TypeBar());

Now the thing is that the type classes need to identify themselves to the  
rest of the system as type classes. Therefore I've got a TypeType class.  
Which also resides in that tree.

Up until now I set up this information on my type classes via a  
"construct"-function, which was called at a point where the class  
instances already exist. But I'm a bit fed up of having to maintain two  
pieces of code (class definition and the construct-function), so I decided  
to make the type classes able to set up themselves. And here the trouble  
starts.

So: In the TypeType static c'tor, I make a new instance of that class.  
Then the normal c'tor is called, trying to setup itself in the tree  
system, telling everybody that it is indead a type. So ... it needs the  
very instance of the TypeType class that is being constructed at that very  
moment ... so: access violation.

' class TypeType : TypeBase
' {
'     TypeBase instance = null;
'	static this() { instance = new TypeType(); }
'     static TypeBase opCall() { return instance; }
'     this()
'     {
'          addObject("typetype");
'          getObject("typetype").setType(TypeType()); // crashes: access  
violation
'     }
' }

It's a bit of a chicken and egg problem. I can't new an instance of that  
class unless there's already one instance there. Is it possible to solve  
that somehow? Something like an "after_this()" or the like? I guess I've  
outsmarted myself there and I have no idea what to do about it besides  
reverting to the old system with that stupid, ugly construct function  
(shudder).

Any ideas would be really appreciated.

-Mike

-- 
Erstellt mit Operas revolutionärem E-Mail-Modul: http://www.opera.com/mail/



More information about the Digitalmars-d-learn mailing list