Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What's the best way to create c++ objects from string names on the fly?

0 views
Skip to first unread message

Release

unread,
Oct 28, 2006, 3:52:42 PM10/28/06
to

Gianni Mariani

unread,
Oct 28, 2006, 5:14:10 PM10/28/06
to

Use Austria C++ generic factories ...

You need to:

#include "at_factory.h"

.... snippet of code in client ....

class Interface
{
... your interface goes here ...
};

... snippet of code in your client ...
// note that this factory registry takes constructors
// with 1 parameter of type const char * and is keyed

Interface * ptr =
at::FactoryRegister<
Interface,
at::DKy, at::Creator1P<Interface, at::DKy, const char * >
>::Get().Create( "ImplementorKEY" )( "contructor param" );

... snippet of code elsewhere, in some DLL/.so, whatever ...

class Implementor
: public Interface
{
public:

Implementor( const char * );

... do everything else you need for this type of "Interface" ...
};

// right after defining "Implementor" use this monster macro ...

AT_MakeFactory1P(
"ImplementorKEY", Implementor, Interface, DKy, const char *
);

The first parameter is the key for this implementation, the second
is the implementing class, the third is the interface being implemented
the fourth is the "key type" to use, the fourth is the type of the first
constructor parameter.

Similarly, there are AT_MakeFactory0P, AT_MakeFactory2P,
AT_MakeFactory3P etc that alter the number of parameters that the
factoory registry is registering.

As I hinted at before, it works with dynamically linked libraries as
well, as soon as a DLL or a .so is loaded, it registers with the
"registry" all the factories. Linking with libraries gets a little
tricky because there are no references from the client to the factory at
link time, only compile time. So you may need to force the linking of a
particular file if you want the factory to be loaded.

There is some more functionality you can pull in, for example, if the
constructor throws, the factory still returns a null pointer, there is a
traits class that you can change that behaviour with.

It also supports placement new. The factory supports a method that
returns the size of the objects being created and a method that takes a
void * where you would like the object created and a "destory" method to
run the destructor on classes created with the factory.

It is thread safe except for I believe, registration of new factories.
This can be easily added. Oh - btw, you must make sure that the object
file created by at_factory_funcs.cpp is only ever linked into the main
executable and never into the DLL's. This is because the factory
registry must remain a singleton.

Oh, and the docs suck, I know, I need to fix that...

0 new messages