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

Last change on this file since 66 was 66, checked in by Marek Kurdej, 11 years ago

Documentation: file info.

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