473,549 Members | 2,699 Online
Bytes | Software Development & Data Engineering Community
+ 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 1512


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::FactoryRegi ster<
Interface,
at::DKy, at::Creator1P<I nterface, 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 "Implemento r" use this monster macro ...

AT_MakeFactory1 P(
"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_MakeFactory0 P, AT_MakeFactory2 P,
AT_MakeFactory3 P 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_func s.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
18831
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 any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it...
7
3547
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 you do not actually need to fork Python in order to implement prototypes. It seems to me that Python metaclasses + descriptors are more than powerful...
8
1822
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 with the constructors but not what. Second, I'd like to take the line: // Create an vector of 1000 F objects init'd to 1,1 vector<F>...
12
3277
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. Such things happen. The whole subsystem is going through radical changes. I don't really want to say what I think of the code just yet. That would...
6
2558
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 "SEGV" when run (presumably - attempt to delete deleted memory. Please take a look and see if you can notice any mistakes I'm making. Basically,...
51
4472
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 there something wrong in there? -------------------------------------------------------------------- Types A type is a definition for a sequence of...
21
13793
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 by Sets. - ICollection cannot decide containment - IList promises indexability by the natural numbers, which is not achievable (since i hash...
4
3815
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 saying I'm not asking you to solve the question - that won't help come exam time :(). Anyway I have a CSV delimited file in the following format; ...
0
7518
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7446
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7715
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7956
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7469
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7808
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1935
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 we have to send another system
1
1057
muto222
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.