author | rubidium |
Fri, 20 Jul 2007 15:48:17 +0000 | |
branch | noai |
changeset 9686 | d3c195c226f9 |
parent 9631 | 8a2d1c2ceb88 |
child 8063 | 0e907a0b5add |
child 9703 | d2a6acdbd665 |
permissions | -rw-r--r-- |
9628 | 1 |
/* $Id$ */ |
2 |
||
3 |
#ifndef BLITTER_FACTORY_HPP |
|
4 |
#define BLITTER_FACTORY_HPP |
|
5 |
||
6 |
#include "base.hpp" |
|
7 |
#include <string> |
|
8 |
#include <map> |
|
9 |
||
10 |
/** |
|
11 |
* The base factory, keeping track of all blitters. |
|
12 |
*/ |
|
13 |
class BlitterFactoryBase { |
|
14 |
private: |
|
15 |
char *name; |
|
16 |
typedef std::map<std::string, BlitterFactoryBase *> Blitters; |
|
17 |
||
18 |
static Blitters &GetBlitters() |
|
19 |
{ |
|
20 |
static Blitters &s_blitters = *new Blitters(); |
|
21 |
return s_blitters; |
|
22 |
} |
|
23 |
||
24 |
static Blitter **GetActiveBlitter() |
|
25 |
{ |
|
26 |
static Blitter *s_blitter = NULL; |
|
27 |
return &s_blitter; |
|
28 |
} |
|
29 |
||
30 |
protected: |
|
31 |
/** |
|
32 |
* Register a blitter internally, based on his name. |
|
33 |
* @param name the name of the blitter. |
|
34 |
* @note an assert() will be trigger if 2 blitters with the same name try to register. |
|
35 |
*/ |
|
36 |
void RegisterBlitter(const char *name) |
|
37 |
{ |
|
38 |
/* Don't register nameless Blitters */ |
|
39 |
if (name == NULL) return; |
|
40 |
||
41 |
this->name = strdup(name); |
|
9686
d3c195c226f9
(svn r10636) [NoAI] -Sync with trunk r10532:10635.
rubidium
parents:
9631
diff
changeset
|
42 |
#if !defined(NDEBUG) |
d3c195c226f9
(svn r10636) [NoAI] -Sync with trunk r10532:10635.
rubidium
parents:
9631
diff
changeset
|
43 |
/* NDEBUG disables asserts and gives a warning: unused variable 'P' */ |
d3c195c226f9
(svn r10636) [NoAI] -Sync with trunk r10532:10635.
rubidium
parents:
9631
diff
changeset
|
44 |
std::pair<Blitters::iterator, bool> P = |
d3c195c226f9
(svn r10636) [NoAI] -Sync with trunk r10532:10635.
rubidium
parents:
9631
diff
changeset
|
45 |
#endif /* !NDEBUG */ |
d3c195c226f9
(svn r10636) [NoAI] -Sync with trunk r10532:10635.
rubidium
parents:
9631
diff
changeset
|
46 |
GetBlitters().insert(Blitters::value_type(name, this)); |
9628 | 47 |
assert(P.second); |
48 |
} |
|
49 |
||
50 |
public: |
|
51 |
BlitterFactoryBase() : |
|
52 |
name(NULL) |
|
53 |
{} |
|
54 |
||
55 |
virtual ~BlitterFactoryBase() { if (this->name != NULL) GetBlitters().erase(this->name); free(this->name); } |
|
56 |
||
57 |
/** |
|
58 |
* Find the requested blitter and return his class. |
|
59 |
* @param name the blitter to select. |
|
60 |
* @post Sets the blitter so GetCurrentBlitter() returns it too. |
|
61 |
*/ |
|
62 |
static Blitter *SelectBlitter(const char *name) |
|
63 |
{ |
|
64 |
if (GetBlitters().size() == 0) return NULL; |
|
65 |
||
66 |
Blitters::iterator it = GetBlitters().begin(); |
|
67 |
for (; it != GetBlitters().end(); it++) { |
|
68 |
BlitterFactoryBase *b = (*it).second; |
|
69 |
if (strcasecmp(name, b->name) == 0) { |
|
70 |
Blitter *newb = b->CreateInstance(); |
|
9631
8a2d1c2ceb88
(svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents:
9628
diff
changeset
|
71 |
delete *GetActiveBlitter(); |
9628 | 72 |
*GetActiveBlitter() = newb; |
73 |
return newb; |
|
74 |
} |
|
75 |
} |
|
76 |
return NULL; |
|
77 |
} |
|
78 |
||
79 |
/** |
|
80 |
* Get the current active blitter (always set by calling SelectBlitter). |
|
81 |
*/ |
|
82 |
static Blitter *GetCurrentBlitter() |
|
83 |
{ |
|
84 |
return *GetActiveBlitter(); |
|
85 |
} |
|
86 |
||
87 |
||
88 |
static char *GetBlittersInfo(char *p, const char *last) |
|
89 |
{ |
|
90 |
p += snprintf(p, last - p, "List of blitters:\n"); |
|
91 |
Blitters::iterator it = GetBlitters().begin(); |
|
92 |
for (; it != GetBlitters().end(); it++) { |
|
93 |
BlitterFactoryBase *b = (*it).second; |
|
94 |
p += snprintf(p, last - p, "%18s: %s\n", b->name, b->GetDescription()); |
|
95 |
} |
|
96 |
p += snprintf(p, last - p, "\n"); |
|
97 |
||
98 |
return p; |
|
99 |
} |
|
100 |
||
101 |
/** |
|
102 |
* Get a nice description of the blitter-class. |
|
103 |
*/ |
|
104 |
virtual const char *GetDescription() = 0; |
|
105 |
||
106 |
/** |
|
107 |
* Create an instance of this Blitter-class. |
|
108 |
*/ |
|
109 |
virtual Blitter *CreateInstance() = 0; |
|
110 |
}; |
|
111 |
||
112 |
/** |
|
113 |
* A template factory, so ->GetName() works correctly. This because else some compiler will complain. |
|
114 |
*/ |
|
115 |
template <class T> |
|
116 |
class BlitterFactory: public BlitterFactoryBase { |
|
117 |
public: |
|
118 |
BlitterFactory() { this->RegisterBlitter(((T *)this)->GetName()); } |
|
119 |
||
120 |
/** |
|
121 |
* Get the long, human readable, name for the Blitter-class. |
|
122 |
*/ |
|
123 |
const char *GetName(); |
|
124 |
}; |
|
125 |
||
126 |
#endif /* BLITTER_FACTORY_HPP */ |