Object Factories

Y

YUY0x7

I'm writing because I was having a little problem implementing one of
the designs suggested in Modern C++ Design. The one I'm talking about
is that of the class factory which registers classes using identifiers.
My problem goes like this:

I am developing a game engine and I am using that method to register
my game entity classes with a generic class factory. Each class has a
unique integer ID. The singleton class factory is in a static library
along with the (basic) classes I want to register (of course I want to
allow other classes in other libraries to be registered too, e.g
game-specific classes). I register the classes as it was suggested,
with
a line similar to the following in each of the classes source file:

static const bool registered =
SomeClassFactory::Instance().Register<SomeClass>(some_id);

Alright. Everything seems fine with this, but apparently not. Some
classes get registered and some don't. I've pinpointed the problem to
be that only the classes which I reference some function(s) in their
source file, get registered. The others don't even call Instance() nor
Register(). I thought that every static global variable got
initialized regardless of their usage afterwards. Is this not true?
Can I blame it on the compiler (I'm using vc++ 7.1 which is very good)
? When I go ahead and reference one function in the source file which
doesn't register the class, it now does register it.

I am trying this by linking the static library with my executable (a
game). Does static libraries have different global initialization
rules?

If you need any more information, please don't hestitate to ask.

Thanks a lot,
George Faraj
 
G

Gianni Mariani

YUY0x7 wrote:
....
Alright. Everything seems fine with this, but apparently not. Some
classes get registered and some don't. I've pinpointed the problem to
be that only the classes which I reference some function(s) in their
source file, get registered. The others don't even call Instance() nor
Register(). I thought that every static global variable got
initialized regardless of their usage afterwards. Is this not true?

Yes - however, factory registration through a static variable and linker
choices to link objects semantics do not work well.

See:

http://austria.sourceforge.net/dox/html/group__GenericFactories.html

It describes the problem you see and provides a solution.
Can I blame it on the compiler (I'm using vc++ 7.1 which is very good)
?

It depends on if you want true linker semantics (only link objects that
have been referenced) or you want the linker to link in all objects.
Linker semantics still make sense.

When I go ahead and reference one function in the source file which
doesn't register the class, it now does register it.

That's understandable.
I am trying this by linking the static library with my executable (a
game). Does static libraries have different global initialization
rules?

No, just different linking rules.

The alternative it to create DLL's (or .so's) and "export" (using
compiler extensions).
 
Y

YUY0x7

Thanks. Yes, I get it now, it's understandable since you don't want to
link stuff that isn't used. I'll have a look at the link you gave me
and try to find a solution that fits my needs. Thanks again.
 
D

Donovan Rebbechi

Alright. Everything seems fine with this, but apparently not. Some
classes get registered and some don't. I've pinpointed the problem to
be that only the classes which I reference some function(s) in their
source file, get registered. The others don't even call Instance() nor
Register(). I thought that every static global variable got
initialized regardless of their usage afterwards. Is this not true?

Yes, it's true.

I think I had a problem like this at some stage -- yes, just reproduced it
succesfully. Different compiler/OS, but here's what happened -- I linked to
a static library (like you), and the only members of the static archive that
were linked in to the final executable were those whose symbols were
referenced by the object file that was linking to it. Have you tried listing
the symbols in your executable to see if those objects actually get linked
in ?

I suspect the linker is the one who you should be angry at (-;

I used one of the solutions the other poster alludes to: dynamic loading.
Can I blame it on the compiler (I'm using vc++ 7.1 which is very good)
?

Nah, blame the linker. The compiler is technically doing the right thing, it's
just that the linker doesn't think that those unreferenced translation units
are "part of your program".

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top