473,394 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Class object factories, references to a **class**

I am trying to create a way to register static members of a **class**, not
an object, for making an object factory. The main thing I want is to be
able to make a call like

class MyClass
{
public:
MyClass *factory();
};
register(MyClass);

and have a method that calls MyClass::factory() to return a MyClass object
on a call like 'MyClass *o = create("MyClass");'.

Is there any C++ construct that would allow this sort of class registration?
I'd be into using macros and even templates (hopefully not).
Here is a longer example...

main.cpp:
------------------------------------------
#include <map>
#include <typeinfo>

typedef std::map<const char *, void (something::*)()> FactoryMap;

FactoryMap *getMap()
{
// I need there to be exactly one instance of registry.
static FactoryMap registry;
return &registry;
}

int register(something Class)
{
pair<const char *, void (something::*)()> p(typeid(Class).name(),
Class::factory);

getMap()->insert(p);
}

void *create(const char *classname)
{
FactoryMap::iterator it = getMap()->find(classname);
if(it != getMap()->end())
return it->second();
else
return NULL;
}

class A
{
public:
static A *factory(){return new A;}
};
int aDummy = register(A); // THIS IS THE SORT OF CALL I'M AFTER
class B : public A
{
public:
static A *factory(){return new B;}
};
int bDummy = register(B); // AGAIN, THIS IS THE SORT OF CALL I'M AFTER

int main()
{
A *a = *b
}

Jul 22 '05 #1
1 2978

"Patrick Stinson" <aj*****@gci.net> wrote in message
news:10*************@corp.supernews.com...
I am trying to create a way to register static members of a **class**, not
an object, for making an object factory. The main thing I want is to be
able to make a call like

class MyClass
{
public:
MyClass *factory();
};
register(MyClass);

and have a method that calls MyClass::factory() to return a MyClass object
on a call like 'MyClass *o = create("MyClass");'.

Is there any C++ construct that would allow this sort of class registration? I'd be into using macros and even templates (hopefully not).
Here is a longer example...

main.cpp:
------------------------------------------
#include <map>
#include <typeinfo>

typedef std::map<const char *, void (something::*)()> FactoryMap;

FactoryMap *getMap()
{
// I need there to be exactly one instance of registry.
static FactoryMap registry;
return &registry;
}

int register(something Class)
{
pair<const char *, void (something::*)()> p(typeid(Class).name(),
Class::factory);

getMap()->insert(p);
}

void *create(const char *classname)
{
FactoryMap::iterator it = getMap()->find(classname);
if(it != getMap()->end())
return it->second();
else
return NULL;
}

class A
{
public:
static A *factory(){return new A;}
};
int aDummy = register(A); // THIS IS THE SORT OF CALL I'M AFTER
class B : public A
{
public:
static A *factory(){return new B;}
};
int bDummy = register(B); // AGAIN, THIS IS THE SORT OF CALL I'M AFTER

int main()
{
A *a = *b
}


Patrick,

here's an outline of a solution - basically I've used templates to make
factory objects out of
your classes so I can call their static methods through a virtual function.
The registration
process creates a mapping of factory to name, so you can do
factory.create("foobar").

dave

#include <stdio.h>
#include <string>
#include <map>
using namespace std;

class MyClass
{
public:
virtual void dofoo()=0;
};

class ClassFactory
{
public:
virtual MyClass* create()=0;
virtual string name()=0;

};
template < class T >
class FactoryWrapper : public ClassFactory
{
public:
virtual MyClass* create(){ return T::create();}
virtual string name(){ return T::name();}
};

class Factories
{
public:
void registerFactory( ClassFactory* factory)
{
_facts[factory->name()]=factory;
}
MyClass* create( const string& name)
{
if ( _facts[name] )
{
return _facts[name]->create();
}

return 0;
}
map<string, ClassFactory*> _facts;

};
class Class1 : public MyClass
{
public:
static Class1* create(){ return new Class1();};
virtual void dofoo(){ printf("Class1::dofoo\n");};
static string name(){ return "Class1";} ;

};
class Class2 : public MyClass
{
public:
static Class2* create(){ return new Class2();};
virtual void dofoo(){ printf("Class2::dofoo\n");};
static string name(){ return "Class2";} ;

};

int main(int argc, char* argv[])
{
FactoryWrapper<Class1> class1Factory;
FactoryWrapper<Class2> class2Factory;
Factories factories;

factories.registerFactory(&class1Factory);
factories.registerFactory(&class2Factory);

MyClass* c1= factories.create("Class1");
MyClass* c2= factories.create("Class2");
c1->dofoo();
c2->dofoo();

return 0;
}

Jul 22 '05 #2

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

Similar topics

3
by: Jeff Louie | last post by:
Well I finally finished a Chapter on the use of Type Based and Interface Based Class Factories in C# at: http://www.geocities.com/jeff_louie/OOP/oop18.htm Happy Holidays, Jeff *** Sent...
1
by: Hans-Christian Stadler | last post by:
Hi, I'm wondering how C++ .NET initializes class members. I have code that looks aproximately like this: _gc class SuperClass; // Factory object creates instances _gc class Factory {...
8
by: deko | last post by:
Which layer should a Factory class go in? DA - Data Access BL - Business Logic UI - User Interface ??
9
by: sashang | last post by:
Hi I'd like to use metaclasses to dynamically generate a class based on a parameter to the objects init function. For example: class MetaThing(type): def __init__(cls, name, bases, dict,...
37
by: JohnGoogle | last post by:
Hi, Newbie question... After a recent article in VSJ I had a go at implementing a Fraction class to aid my understanding of operator overloading. After a previous message someone suggested...
4
by: Chris Brat | last post by:
Hi, I've done some reading up but I cant get my head around how/why class methods can be used as apposed to static methods. Can anyone give an example of where they have used class methods? ...
25
by: David Sanders | last post by:
Hi, As part of a simulation program, I have several different model classes, ModelAA, ModelBB, etc., which are all derived from the class BasicModel by inheritance. model to use, for example...
1
by: Smokey Grindle | last post by:
Any good examples out there of remoting with class factories out there? I want to use interfaces to keep my code server side but also need class factories to make the objects. havent figured out...
6
by: K Viltersten | last post by:
I have a class that reads an XML and according to its contents, it creates a List<Letter>, where Letter is an abstract class that is implemented in Alpha and Beta classes. I use a foreach...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...
0
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...

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.