1 | /**
|
---|
2 | *
|
---|
3 | * Distributed under the UTC Heudiascy Pacpus License, Version 1.0.
|
---|
4 | * Copyright (c) UTC Heudiasyc 2010 - 2013. All rights reserved.
|
---|
5 | *
|
---|
6 | * See the LICENSE file for more information or a copy at:
|
---|
7 | * http://www.hds.utc.fr/~kurdejma/LICENSE_1_0.txt
|
---|
8 | *
|
---|
9 | */
|
---|
10 |
|
---|
11 | #ifndef COMPONENTFACTORY_H
|
---|
12 | #define COMPONENTFACTORY_H
|
---|
13 |
|
---|
14 | #include <cassert>
|
---|
15 |
|
---|
16 | #include "ComponentFactoryBase.h"
|
---|
17 |
|
---|
18 | #include <QtGlobal>
|
---|
19 | #include <QString>
|
---|
20 |
|
---|
21 | /** Register a component to the factory.
|
---|
22 | * @className Name of the class, without the quotes.
|
---|
23 | * @factoryName Name of the class in the factory.
|
---|
24 | */
|
---|
25 | #define REGISTER_COMPONENT(className, factoryName) \
|
---|
26 | static pacpus::ComponentFactory<className> sFactory(factoryName)
|
---|
27 |
|
---|
28 | namespace pacpus
|
---|
29 | {
|
---|
30 | /** ComponentFactory
|
---|
31 | * @brief Use it to interface your components with the application.
|
---|
32 | *
|
---|
33 | * @example
|
---|
34 | * REGISTER_COMPONENT("DummyComponent", DummyComponent);
|
---|
35 | */
|
---|
36 | template <typename T>
|
---|
37 | class ComponentFactory
|
---|
38 | : public ComponentFactoryBase
|
---|
39 | {
|
---|
40 | public:
|
---|
41 | /** Ctor of ComponentFactory, initialize the factory of the components of type @em T.
|
---|
42 | * @param type Name of the type of the components.
|
---|
43 | */
|
---|
44 | ComponentFactory(const QString& type);
|
---|
45 |
|
---|
46 | /** Dtor of ComponentFactory. */
|
---|
47 | virtual ~ComponentFactory();
|
---|
48 |
|
---|
49 | /** Get the name of the type of the components.
|
---|
50 | * @return Name of the type of the components.
|
---|
51 | */
|
---|
52 | const QString& getType() const;
|
---|
53 |
|
---|
54 | protected:
|
---|
55 | virtual ComponentBase* instantiateComponent(const QString& name);
|
---|
56 |
|
---|
57 | private:
|
---|
58 | QString mType;
|
---|
59 | };
|
---|
60 |
|
---|
61 |
|
---|
62 | template <typename T>
|
---|
63 | ComponentFactory<T>::ComponentFactory(const QString& type)
|
---|
64 | : mType(type)
|
---|
65 | {
|
---|
66 | assert(!type.isEmpty());
|
---|
67 | addFactory(this, mType);
|
---|
68 | }
|
---|
69 |
|
---|
70 | template<typename T>
|
---|
71 | ComponentFactory<T>::~ComponentFactory()
|
---|
72 | {
|
---|
73 | }
|
---|
74 |
|
---|
75 | template <typename T>
|
---|
76 | const QString& ComponentFactory<T>::getType() const
|
---|
77 | {
|
---|
78 | return mType;
|
---|
79 | }
|
---|
80 |
|
---|
81 | template<typename T>
|
---|
82 | ComponentBase* ComponentFactory<T>::instantiateComponent(const QString& name)
|
---|
83 | {
|
---|
84 | return new T(name);
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | #endif // COMPONENTFACTORY_H
|
---|