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