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

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

[doc] Added: Pacpus API reference main page.
Fixed: example sections in doc.

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