[76] | 1 | // %pacpus:license{
|
---|
[73] | 2 | // This file is part of the PACPUS framework distributed under the
|
---|
| 3 | // CECILL-C License, Version 1.0.
|
---|
[76] | 4 | // %pacpus:license}
|
---|
[73] | 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
|
---|
[115] | 33 | /// ~~~
|
---|
[73] | 34 | /// REGISTER_COMPONENT("DummyComponent", DummyComponent);
|
---|
[115] | 35 | /// ~~~
|
---|
[73] | 36 | /// @see pacpus::ComponentFactory
|
---|
| 37 | #define REGISTER_COMPONENT(className, factoryName) \
|
---|
[116] | 38 | static pacpus::ComponentFactory<className> sFactory(factoryName)
|
---|
[73] | 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");
|
---|
[116] | 51 |
|
---|
[73] | 52 | public:
|
---|
[116] | 53 | /// Ctor of ComponentFactory, initialize the factory of the components of type @em T.
|
---|
| 54 | /// @param type Name of the type of the components.
|
---|
[117] | 55 | ComponentFactory(const QString & type);
|
---|
[116] | 56 |
|
---|
| 57 | /// Dtor of ComponentFactory.
|
---|
[73] | 58 | virtual ~ComponentFactory();
|
---|
| 59 |
|
---|
[116] | 60 | /// Gets the name of the type of the components.
|
---|
| 61 | /// @returns Name of the type of the components.
|
---|
[117] | 62 | const QString & getType() const;
|
---|
[73] | 63 |
|
---|
| 64 | protected:
|
---|
[117] | 65 | virtual ComponentBase * instantiateComponent(const QString& name);
|
---|
[116] | 66 |
|
---|
[73] | 67 | private:
|
---|
| 68 | QString mType;
|
---|
| 69 | };
|
---|
| 70 |
|
---|
| 71 | template <typename T>
|
---|
| 72 | ComponentFactory<T>::ComponentFactory(const QString& type)
|
---|
[116] | 73 | : mType(type)
|
---|
[73] | 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>
|
---|
[117] | 91 | ComponentBase * ComponentFactory<T>::instantiateComponent(const QString & name)
|
---|
[73] | 92 | {
|
---|
| 93 | return new T(name);
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[116] | 96 | } // namespace pacpus
|
---|
[73] | 97 |
|
---|
[117] | 98 | #endif // DEF_PACPUS_COMPONENTFACTORY_H
|
---|