-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.h
56 lines (47 loc) · 1.34 KB
/
factory.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
#ifndef FACTORY_H
#define FACTORY_H
/* Factory method for Player's derived classes.
*
* Usage:
* There is no need to include this file separately in the
* Player's derived header -- it's already done in "player.h".
* To use class in Manager, write in the .cpp file something
* like:
* REGISTRATE_PLAYER(MyPlayer);
* and feel free to use this player in Manager.
*/
#include <map>
class Player;
class PlayerFactory
{
public:
virtual Player * create() = 0;
};
class Factory
{
public:
static void registerType(const std::string & type, PlayerFactory * handler);
protected:
Factory();
typedef std::map<std::string, PlayerFactory *> FactMap;
static FactMap & factories; // just a shortcut
protected:
static FactMap & factoriesContainer(); // contain static object
};
// Purpose to use define-macro instead of template
// registration class is the name of class. To have the
// opportunity to create class by it's name we need to
// parse the name as quoted string during precompilation.
#define PEGISTRATE_PLAYER(name) \
class name##_Factory : public PlayerFactory \
{ \
public: \
Player * create() { \
return static_cast<Player*>(new name()); \
} \
name##_Factory() { \
Factory::registerType(#name, this); \
} \
}; \
static name##_Factory name##Registrator
#endif // FACTORY_H