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

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

Documentation: file info.

  • Property svn:keywords set to Id
File size: 2.4 KB
RevLine 
[62]1// This file is part of the PACPUS framework distributed under the
2// CECILL-C License, Version 1.0.
3//
[66]4/// @file
[62]5/// @author Gerald Dherbomez <firstname.surname@utc.fr>
[63]6/// @date February, 2006
[62]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///
[63]11/// Purpose: Template class ComponentFactory.
12/// Use it to interface your components with the application.
[3]13
[31]14#ifndef DEF_PACPUS_COMPONENTFACTORY_H
15#define DEF_PACPUS_COMPONENTFACTORY_H
[3]16
[15]17#include <cassert>
18
[46]19#include <boost/static_assert.hpp>
20#include <boost/type_traits/is_base_of.hpp>
21
[31]22#include <Pacpus/kernel/ComponentFactoryBase.h>
[3]23
[15]24#include <QtGlobal>
[3]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
[45]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
[3]45{
[46]46 BOOST_STATIC_ASSERT_MSG((boost::is_base_of<ComponentBase, T>::value), "T must inherit from ComponentBase");
[45]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();
[3]55
[45]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;
[3]60
[45]61protected:
62 virtual ComponentBase* instantiateComponent(const QString& name);
63
64private:
65 QString mType;
66};
[3]67
[45]68template <typename T>
69ComponentFactory<T>::ComponentFactory(const QString& type)
70 : mType(type)
71{
[15]72 assert(!type.isEmpty());
[3]73 addFactory(this, mType);
[45]74}
[3]75
[45]76template<typename T>
77ComponentFactory<T>::~ComponentFactory()
78{
79}
[3]80
[45]81template <typename T>
82const QString& ComponentFactory<T>::getType() const
83{
[3]84 return mType;
[45]85}
[3]86
[45]87template<typename T>
88ComponentBase* ComponentFactory<T>::instantiateComponent(const QString& name)
89{
[15]90 return new T(name);
[3]91}
92
[45]93} // pacpus
94
[31]95#endif // DEF_PACPUS_DBITEEXCEPTION_H
Note: See TracBrowser for help on using the repository browser.