[89] | 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 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 |
|
---|
[288] | 18 | #include <boost/assert.hpp>
|
---|
| 19 | #include <boost/static_assert.hpp>
|
---|
| 20 | #include <boost/type_traits/is_base_of.hpp>
|
---|
[89] | 21 |
|
---|
| 22 | #include <Pacpus/kernel/ComponentFactoryBase.h>
|
---|
| 23 |
|
---|
| 24 | #include <QtGlobal>
|
---|
| 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 | /// REGISTER_COMPONENT("DummyComponent", DummyComponent);
|
---|
| 34 | /// @see pacpus::ComponentFactory
|
---|
| 35 | #define REGISTER_COMPONENT(className, factoryName) \
|
---|
| 36 | static pacpus::ComponentFactory<className> sFactory(factoryName)
|
---|
| 37 |
|
---|
[288] | 38 | #define PACPUS_REGISTER_COMPONENT(Class) \
|
---|
| 39 | static pacpus::ComponentFactory<Class> sFactory(#Class)
|
---|
[89] | 40 |
|
---|
[288] | 41 | namespace pacpus
|
---|
| 42 | {
|
---|
| 43 |
|
---|
[89] | 44 | /// Use it to interface your components with the application.
|
---|
| 45 | ///
|
---|
| 46 | /// @tparam T Component class
|
---|
| 47 | /// @see #REGISTER_COMPONENT(className, factoryName)
|
---|
| 48 | template <typename T>
|
---|
| 49 | class ComponentFactory
|
---|
| 50 | : public ComponentFactoryBase
|
---|
| 51 | {
|
---|
[288] | 52 | BOOST_STATIC_ASSERT_MSG((boost::is_base_of<ComponentBase, T>::value), "component T must inherit from ComponentBase");
|
---|
| 53 |
|
---|
[89] | 54 | public:
|
---|
| 55 | /** Ctor of ComponentFactory, initialize the factory of the components of type @em T.
|
---|
| 56 | * @param type Name of the type of the components.
|
---|
| 57 | */
|
---|
[288] | 58 | ComponentFactory(QString const& type);
|
---|
[89] | 59 |
|
---|
| 60 | /** Dtor of ComponentFactory. */
|
---|
| 61 | virtual ~ComponentFactory();
|
---|
| 62 |
|
---|
| 63 | /** Get the name of the type of the components.
|
---|
| 64 | * @return Name of the type of the components.
|
---|
| 65 | */
|
---|
[288] | 66 | QString const& getType() const;
|
---|
[89] | 67 |
|
---|
| 68 | protected:
|
---|
[288] | 69 | virtual ComponentSharedPointer instantiateComponent(QString const& name);
|
---|
| 70 |
|
---|
[89] | 71 | private:
|
---|
| 72 | QString mType;
|
---|
| 73 | };
|
---|
| 74 |
|
---|
| 75 | template <typename T>
|
---|
[288] | 76 | ComponentFactory<T>::ComponentFactory(QString const& type)
|
---|
| 77 | : mType(type)
|
---|
[89] | 78 | {
|
---|
[288] | 79 | BOOST_ASSERT(!type.isEmpty());
|
---|
[89] | 80 | addFactory(this, mType);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | template<typename T>
|
---|
| 84 | ComponentFactory<T>::~ComponentFactory()
|
---|
| 85 | {
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | template <typename T>
|
---|
[288] | 89 | QString const& ComponentFactory<T>::getType() const
|
---|
[89] | 90 | {
|
---|
| 91 | return mType;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | template<typename T>
|
---|
[288] | 95 | ComponentSharedPointer ComponentFactory<T>::instantiateComponent(QString const& name)
|
---|
[89] | 96 | {
|
---|
[288] | 97 | return ComponentSharedPointer(new T(name));
|
---|
[89] | 98 | }
|
---|
| 99 |
|
---|
[288] | 100 | } // namespace pacpus
|
---|
[89] | 101 |
|
---|
| 102 | #endif // DEF_PACPUS_DBITEEXCEPTION_H
|
---|