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

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

Minor: coding style and license header.

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