[15] | 1 | /**
|
---|
| 2 | *
|
---|
| 3 | * Distributed under the UTC Heudiascy Pacpus License, Version 1.0.
|
---|
| 4 | * Copyright (c) UTC Heudiasyc 2010 - 2013. All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * See the LICENSE file for more information or a copy at:
|
---|
| 7 | * http://www.hds.utc.fr/~kurdejma/LICENSE_1_0.txt
|
---|
| 8 | *
|
---|
| 9 | */
|
---|
[3] | 10 |
|
---|
| 11 | #ifndef COMPONENTFACTORY_H
|
---|
| 12 | #define COMPONENTFACTORY_H
|
---|
| 13 |
|
---|
[15] | 14 | #include <cassert>
|
---|
| 15 |
|
---|
[3] | 16 | #include "ComponentFactoryBase.h"
|
---|
| 17 |
|
---|
[15] | 18 | #include <QtGlobal>
|
---|
[3] | 19 | #include <QString>
|
---|
| 20 |
|
---|
| 21 | /** Register a component to the factory.
|
---|
| 22 | * @className Name of the class, without the quotes.
|
---|
| 23 | * @factoryName Name of the class in the factory.
|
---|
| 24 | */
|
---|
| 25 | #define REGISTER_COMPONENT(className, factoryName) \
|
---|
| 26 | static pacpus::ComponentFactory<className> sFactory(factoryName)
|
---|
| 27 |
|
---|
[15] | 28 | namespace pacpus
|
---|
[3] | 29 | {
|
---|
[15] | 30 | /** ComponentFactory
|
---|
| 31 | * @brief Use it to interface your components with the application.
|
---|
| 32 | *
|
---|
| 33 | * @example
|
---|
| 34 | * REGISTER_COMPONENT("DummyComponent", DummyComponent);
|
---|
| 35 | */
|
---|
| 36 | template <typename T>
|
---|
| 37 | class ComponentFactory
|
---|
| 38 | : public ComponentFactoryBase
|
---|
| 39 | {
|
---|
| 40 | public:
|
---|
| 41 | /** Ctor of ComponentFactory, initialize the factory of the components of type @em T.
|
---|
| 42 | * @param type Name of the type of the components.
|
---|
| 43 | */
|
---|
| 44 | ComponentFactory(const QString& type);
|
---|
| 45 |
|
---|
| 46 | /** Dtor of ComponentFactory. */
|
---|
| 47 | virtual ~ComponentFactory();
|
---|
[3] | 48 |
|
---|
[15] | 49 | /** Get the name of the type of the components.
|
---|
| 50 | * @return Name of the type of the components.
|
---|
| 51 | */
|
---|
| 52 | const QString& getType() const;
|
---|
[3] | 53 |
|
---|
[15] | 54 | protected:
|
---|
| 55 | virtual ComponentBase* instantiateComponent(const QString& name);
|
---|
| 56 |
|
---|
| 57 | private:
|
---|
| 58 | QString mType;
|
---|
| 59 | };
|
---|
[3] | 60 |
|
---|
| 61 |
|
---|
[15] | 62 | template <typename T>
|
---|
| 63 | ComponentFactory<T>::ComponentFactory(const QString& type)
|
---|
[3] | 64 | : mType(type)
|
---|
[15] | 65 | {
|
---|
| 66 | assert(!type.isEmpty());
|
---|
[3] | 67 | addFactory(this, mType);
|
---|
[15] | 68 | }
|
---|
[3] | 69 |
|
---|
[15] | 70 | template<typename T>
|
---|
| 71 | ComponentFactory<T>::~ComponentFactory()
|
---|
| 72 | {
|
---|
| 73 | }
|
---|
[3] | 74 |
|
---|
[15] | 75 | template <typename T>
|
---|
| 76 | const QString& ComponentFactory<T>::getType() const
|
---|
| 77 | {
|
---|
[3] | 78 | return mType;
|
---|
[15] | 79 | }
|
---|
[3] | 80 |
|
---|
[15] | 81 | template<typename T>
|
---|
| 82 | ComponentBase* ComponentFactory<T>::instantiateComponent(const QString& name)
|
---|
| 83 | {
|
---|
| 84 | return new T(name);
|
---|
| 85 | }
|
---|
[3] | 86 | }
|
---|
| 87 |
|
---|
| 88 | #endif // COMPONENTFACTORY_H
|
---|