source: pacpusframework/trunk/include/Pacpus/kernel/ComponentFactory.h@ 36

Last change on this file since 36 was 31, checked in by sgosseli, 12 years ago

Huge commit: use the new includes style in all the files, add the license header in all the headers, and in some cpp.

File size: 2.1 KB
Line 
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 */
10
11#ifndef DEF_PACPUS_COMPONENTFACTORY_H
12#define DEF_PACPUS_COMPONENTFACTORY_H
13
14#include <cassert>
15
16#include <Pacpus/kernel/ComponentFactoryBase.h>
17
18#include <QtGlobal>
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
28namespace pacpus
29{
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();
48
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;
53
54 protected:
55 virtual ComponentBase* instantiateComponent(const QString& name);
56
57 private:
58 QString mType;
59 };
60
61 template <typename T>
62 ComponentFactory<T>::ComponentFactory(const QString& type)
63 : mType(type)
64 {
65 assert(!type.isEmpty());
66 addFactory(this, mType);
67 }
68
69 template<typename T>
70 ComponentFactory<T>::~ComponentFactory()
71 {
72 }
73
74 template <typename T>
75 const QString& ComponentFactory<T>::getType() const
76 {
77 return mType;
78 }
79
80 template<typename T>
81 ComponentBase* ComponentFactory<T>::instantiateComponent(const QString& name)
82 {
83 return new T(name);
84 }
85}
86
87#endif // DEF_PACPUS_DBITEEXCEPTION_H
Note: See TracBrowser for help on using the repository browser.