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