473,484 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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



Oct 28 '06 #1
1 1504


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...
Oct 28 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
18798
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
7
3537
by: Michele Simionato | last post by:
So far, I have not installed Prothon, nor I have experience with Io, Self or other prototype-based languages. Still, from the discussion on the mailing list, I have got the strong impression that...
8
1815
by: Bruce | last post by:
OK, this won't compile saying it can't access private members declared in class F. I don't get it and even if I make the entire class public, it still says that. I realize it has something to do...
12
3269
by: Steven T. Hatton | last post by:
This is something I've been looking at because it is central to a currently broken part of the KDevelop new application wizard. I'm not complaining about it being broken, It's a CVS images. ...
6
2542
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a...
51
4447
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is...
21
13780
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered...
4
3807
Ispep
by: Ispep | last post by:
Hi, unfortunately having a bit of difficulty with a question from an Open University course I'm currently doing. If you could help me out in any way I'd be grafeul (though obviously it goes without...
0
7080
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6950
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7103
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7140
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5403
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4527
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3038
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1355
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
588
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.