[62] | 1 | // This file is part of the PACPUS framework distributed under the
|
---|
| 2 | // CECILL-C License, Version 1.0.
|
---|
| 3 | //
|
---|
[66] | 4 | /// @file
|
---|
[62] | 5 | /// @author Gerald Dherbomez <firstname.surname@utc.fr>
|
---|
[63] | 6 | /// @date February, 2006
|
---|
[62] | 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 | ///
|
---|
[63] | 11 | /// Purpose: Template class ComponentFactory.
|
---|
| 12 | /// Use it to interface your components with the application.
|
---|
[3] | 13 |
|
---|
[31] | 14 | #ifndef DEF_PACPUS_COMPONENTFACTORY_H
|
---|
| 15 | #define DEF_PACPUS_COMPONENTFACTORY_H
|
---|
[3] | 16 |
|
---|
[15] | 17 | #include <cassert>
|
---|
| 18 |
|
---|
[46] | 19 | #include <boost/static_assert.hpp>
|
---|
| 20 | #include <boost/type_traits/is_base_of.hpp>
|
---|
| 21 |
|
---|
[31] | 22 | #include <Pacpus/kernel/ComponentFactoryBase.h>
|
---|
[3] | 23 |
|
---|
[15] | 24 | #include <QtGlobal>
|
---|
[3] | 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 |
|
---|
[45] | 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
|
---|
[3] | 45 | {
|
---|
[46] | 46 | BOOST_STATIC_ASSERT_MSG((boost::is_base_of<ComponentBase, T>::value), "T must inherit from ComponentBase");
|
---|
[45] | 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();
|
---|
[3] | 55 |
|
---|
[45] | 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;
|
---|
[3] | 60 |
|
---|
[45] | 61 | protected:
|
---|
| 62 | virtual ComponentBase* instantiateComponent(const QString& name);
|
---|
| 63 |
|
---|
| 64 | private:
|
---|
| 65 | QString mType;
|
---|
| 66 | };
|
---|
[3] | 67 |
|
---|
[45] | 68 | template <typename T>
|
---|
| 69 | ComponentFactory<T>::ComponentFactory(const QString& type)
|
---|
| 70 | : mType(type)
|
---|
| 71 | {
|
---|
[15] | 72 | assert(!type.isEmpty());
|
---|
[3] | 73 | addFactory(this, mType);
|
---|
[45] | 74 | }
|
---|
[3] | 75 |
|
---|
[45] | 76 | template<typename T>
|
---|
| 77 | ComponentFactory<T>::~ComponentFactory()
|
---|
| 78 | {
|
---|
| 79 | }
|
---|
[3] | 80 |
|
---|
[45] | 81 | template <typename T>
|
---|
| 82 | const QString& ComponentFactory<T>::getType() const
|
---|
| 83 | {
|
---|
[3] | 84 | return mType;
|
---|
[45] | 85 | }
|
---|
[3] | 86 |
|
---|
[45] | 87 | template<typename T>
|
---|
| 88 | ComponentBase* ComponentFactory<T>::instantiateComponent(const QString& name)
|
---|
| 89 | {
|
---|
[15] | 90 | return new T(name);
|
---|
[3] | 91 | }
|
---|
| 92 |
|
---|
[45] | 93 | } // pacpus
|
---|
| 94 |
|
---|
[31] | 95 | #endif // DEF_PACPUS_DBITEEXCEPTION_H
|
---|