1 | // ********************************************************************
|
---|
2 | // created: 2006/02/14 - 15:39
|
---|
3 | // filename: ComponentFactory.h
|
---|
4 | //
|
---|
5 | // author: Gerald Dherbomez
|
---|
6 | //
|
---|
7 | // purpose: Template class ComponentFactory.
|
---|
8 | // Use it to interface your components with the application
|
---|
9 | // *********************************************************************
|
---|
10 |
|
---|
11 | #ifndef COMPONENTFACTORY_H
|
---|
12 | #define COMPONENTFACTORY_H
|
---|
13 |
|
---|
14 | #include "ComponentFactoryBase.h"
|
---|
15 |
|
---|
16 | #include <QString>
|
---|
17 |
|
---|
18 | /** Register a component to the factory.
|
---|
19 | * @className Name of the class, without the quotes.
|
---|
20 | * @factoryName Name of the class in the factory.
|
---|
21 | */
|
---|
22 | #define REGISTER_COMPONENT(className, factoryName) \
|
---|
23 | static pacpus::ComponentFactory<className> sFactory(factoryName)
|
---|
24 |
|
---|
25 | namespace pacpus {
|
---|
26 |
|
---|
27 | template <typename T>
|
---|
28 | class ComponentFactory
|
---|
29 | : public ComponentFactoryBase
|
---|
30 | {
|
---|
31 | public:
|
---|
32 | ComponentFactory(QString type);
|
---|
33 | virtual ~ComponentFactory();
|
---|
34 |
|
---|
35 | const QString & getType() const;
|
---|
36 |
|
---|
37 | protected:
|
---|
38 | virtual ComponentBase * instantiateComponent(const QString & name);
|
---|
39 |
|
---|
40 | private:
|
---|
41 | QString mType;
|
---|
42 | };
|
---|
43 |
|
---|
44 | ////////////////////////////////////////////////////////////////////////////////
|
---|
45 | template <typename T>
|
---|
46 | ComponentFactory<T>::ComponentFactory(QString type)
|
---|
47 | : mType(type)
|
---|
48 | {
|
---|
49 | addFactory(this, mType);
|
---|
50 | }
|
---|
51 |
|
---|
52 | template<typename T>
|
---|
53 | ComponentFactory<T>::~ComponentFactory()
|
---|
54 | {
|
---|
55 | }
|
---|
56 |
|
---|
57 | template <typename T>
|
---|
58 | const QString & ComponentFactory<T>::getType() const
|
---|
59 | {
|
---|
60 | return mType;
|
---|
61 | }
|
---|
62 |
|
---|
63 | template<typename T>
|
---|
64 | ComponentBase * ComponentFactory<T>::instantiateComponent(const QString & name)
|
---|
65 | {
|
---|
66 | T * component = new T(name);
|
---|
67 | return component;
|
---|
68 | }
|
---|
69 |
|
---|
70 | } // namespace pacpus
|
---|
71 |
|
---|
72 | #endif // COMPONENTFACTORY_H
|
---|