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