[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 76 2013-01-10 17:05:10Z 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 <QtGlobal>
|
---|
| 26 | #include <QString>
|
---|
| 27 |
|
---|
| 28 | /// @def REGISTER_COMPONENT(className, factoryName)
|
---|
| 29 | /// Registers a component to the factory.
|
---|
| 30 | ///
|
---|
| 31 | /// @param className Name of the class, without the quotes.
|
---|
| 32 | /// @param factoryName Name of the class in the factory.
|
---|
| 33 | /// @example
|
---|
| 34 | /// REGISTER_COMPONENT("DummyComponent", DummyComponent);
|
---|
| 35 | /// @see pacpus::ComponentFactory
|
---|
| 36 | #define REGISTER_COMPONENT(className, factoryName) \
|
---|
| 37 | static pacpus::ComponentFactory<className> sFactory(factoryName)
|
---|
| 38 |
|
---|
| 39 | namespace pacpus {
|
---|
| 40 |
|
---|
| 41 | /// Use it to interface your components with the application.
|
---|
| 42 | ///
|
---|
| 43 | /// @tparam T Component class
|
---|
| 44 | /// @see #REGISTER_COMPONENT(className, factoryName)
|
---|
| 45 | template <typename T>
|
---|
| 46 | class ComponentFactory
|
---|
| 47 | : public ComponentFactoryBase
|
---|
| 48 | {
|
---|
| 49 | BOOST_STATIC_ASSERT_MSG((boost::is_base_of<ComponentBase, T>::value), "T must inherit from ComponentBase");
|
---|
| 50 | public:
|
---|
| 51 | /** Ctor of ComponentFactory, initialize the factory of the components of type @em T.
|
---|
| 52 | * @param type Name of the type of the components.
|
---|
| 53 | */
|
---|
| 54 | ComponentFactory(const QString& type);
|
---|
| 55 |
|
---|
| 56 | /** Dtor of ComponentFactory. */
|
---|
| 57 | virtual ~ComponentFactory();
|
---|
| 58 |
|
---|
| 59 | /** Get the name of the type of the components.
|
---|
| 60 | * @return Name of the type of the components.
|
---|
| 61 | */
|
---|
| 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 | } // pacpus
|
---|
| 97 |
|
---|
| 98 | #endif // DEF_PACPUS_DBITEEXCEPTION_H
|
---|