Object Factories

Discussion in 'C++' started by YUY0x7, May 29, 2005.

  1. YUY0x7

    YUY0x7 Guest

    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
     
    YUY0x7, May 29, 2005
    #1
    1. Advertisements

  2. 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).
     
    Gianni Mariani, May 30, 2005
    #2
    1. Advertisements

  3. YUY0x7

    YUY0x7 Guest

    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.
     
    YUY0x7, May 30, 2005
    #3
  4. On 2005-05-29, 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, 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,
    --
    Donovan Rebbechi
    http://pegasus.rutgers.edu/~elflord/
     
    Donovan Rebbechi, May 30, 2005
    #4
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark F. Haigh
    Replies:
    1
    Views:
    364
    David Hilsee
    Aug 15, 2004
  2. Patrick Stinson
    Replies:
    1
    Views:
    407
    Dave Townsend
    Sep 6, 2004
  3. Jeff  Greenberg
    Replies:
    2
    Views:
    453
    Jeff Greenberg
    Oct 19, 2004
  4. Dave

    Object factories

    Dave, Jan 20, 2005, in forum: C++
    Replies:
    3
    Views:
    395
  5. Replies:
    1
    Views:
    509
    Oliver Wong
    Oct 2, 2006
Loading...

Share This Page