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

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

Improvement: when instantiated, check if ComponentBase is base of T.

File size: 2.2 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 <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.