-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory-args1.h
116 lines (96 loc) · 2.68 KB
/
factory-args1.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#ifndef FACTORY_ARGS1_H
#define FACTORY_ARGS1_H
//#define AUTO_PTR 1
#if defined(AUTO_PTR)
#include <memory>
#endif
#include <cstdlib>
#include <string>
#include <map>
#include <iostream>
namespace factory {
void baseDummy();
void derivedDummy();
}
typedef std::string defaultIDKeyType;
template <class ManufacturedType, typename ClassIDKey=defaultIDKeyType>
class GenericFactory {
public:
#if defined(AUTO_PTR)
typedef std::auto_ptr<ManufacturedType> BasePtr;
#else
typedef ManufacturedType* BasePtr;
#endif
private:
typedef BasePtr (*BaseCreateFn)(int, char **);
typedef std::map<ClassIDKey, BaseCreateFn> FnRegistry;
FnRegistry registry;
GenericFactory() : registry() {}
GenericFactory(const GenericFactory&); // Not implemented
GenericFactory &operator=(const GenericFactory&); // Not implemented
public:
static GenericFactory &instance()
{
if ( !bf ) {
bf = new GenericFactory<ManufacturedType, ClassIDKey>;
std::atexit(kill);
}
return *bf;
}
void RegCreateFn(const ClassIDKey & id, BaseCreateFn fn)
{
registry[id] = fn;
}
BasePtr create(const ClassIDKey &className,int nargs, char *args[]) const
{
BasePtr theObject(0);
typename FnRegistry::const_iterator regEntry = registry.find(className);
if (regEntry != registry.end()) {
theObject = regEntry->second(nargs,args);
}
else {
throw std::string("Error: unknown ClassIDKey: "+className);
}
return theObject;
}
void content() const
{
std::cout << "Factory content: " << std::endl;
for (typename FnRegistry::const_iterator regEntry = registry.begin();
regEntry != registry.end(); ++regEntry) {
std::cout << " * " << regEntry->first << std::endl;
}
}
void init() const
{
factory::baseDummy();
factory::derivedDummy();
}
private:
static GenericFactory<ManufacturedType, ClassIDKey> *bf;
/// Call by atexit() to clean up.
static void kill()
{
GenericFactory<ManufacturedType, ClassIDKey> *p(bf);
bf = 0;
p->~GenericFactory<ManufacturedType, ClassIDKey>();
}
};
template <class ManufacturedType, typename ClassIDKey>
GenericFactory<ManufacturedType, ClassIDKey> *GenericFactory<ManufacturedType, ClassIDKey>::bf(0);
template <class AncestorType, class ManufacturedType, typename ClassIDKey=defaultIDKeyType>
class RegisterInFactory {
private:
typedef GenericFactory<AncestorType, ClassIDKey> Factory;
typedef typename Factory::BasePtr BasePtr;
public:
static BasePtr CreateInstance(int nargs, char *args[])
{
return BasePtr(new ManufacturedType(nargs, args));
}
explicit RegisterInFactory(const ClassIDKey &id)
{
Factory::instance().RegCreateFn(id, CreateInstance);
}
};
#endif