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

Last change on this file since 63 was 63, checked in by Marek Kurdej, 12 years ago

Documentation: file info.

File size: 2.4 KB
Line 
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
33namespace pacpus {
34
35/** ComponentFactory
36 * @brief Use it to interface your components with the application.
37 *
38 * @example
39 * REGISTER_COMPONENT("DummyComponent", DummyComponent);
40 */
41template <typename T>
42class ComponentFactory
43 : public ComponentFactoryBase
44{
45 BOOST_STATIC_ASSERT_MSG((boost::is_base_of<ComponentBase, T>::value), "T must inherit from ComponentBase");
46public:
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
60protected:
61 virtual ComponentBase* instantiateComponent(const QString& name);
62
63private:
64 QString mType;
65};
66
67template <typename T>
68ComponentFactory<T>::ComponentFactory(const QString& type)
69 : mType(type)
70{
71 assert(!type.isEmpty());
72 addFactory(this, mType);
73}
74
75template<typename T>
76ComponentFactory<T>::~ComponentFactory()
77{
78}
79
80template <typename T>
81const QString& ComponentFactory<T>::getType() const
82{
83 return mType;
84}
85
86template<typename T>
87ComponentBase* ComponentFactory<T>::instantiateComponent(const QString& name)
88{
89 return new T(name);
90}
91
92} // pacpus
93
94#endif // DEF_PACPUS_DBITEEXCEPTION_H
Note: See TracBrowser for help on using the repository browser.