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