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

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

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

File size: 2.2 KB
RevLine 
[15]1/**
2 *
[45]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 *
[15]11 */
[3]12
[31]13#ifndef DEF_PACPUS_COMPONENTFACTORY_H
14#define DEF_PACPUS_COMPONENTFACTORY_H
[3]15
[15]16#include <cassert>
17
[46]18#include <boost/static_assert.hpp>
19#include <boost/type_traits/is_base_of.hpp>
20
[31]21#include <Pacpus/kernel/ComponentFactoryBase.h>
[3]22
[15]23#include <QtGlobal>
[3]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
[45]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
[3]44{
[46]45 BOOST_STATIC_ASSERT_MSG((boost::is_base_of<ComponentBase, T>::value), "T must inherit from ComponentBase");
[45]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();
[3]54
[45]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;
[3]59
[45]60protected:
61 virtual ComponentBase* instantiateComponent(const QString& name);
62
63private:
64 QString mType;
65};
[3]66
[45]67template <typename T>
68ComponentFactory<T>::ComponentFactory(const QString& type)
69 : mType(type)
70{
[15]71 assert(!type.isEmpty());
[3]72 addFactory(this, mType);
[45]73}
[3]74
[45]75template<typename T>
76ComponentFactory<T>::~ComponentFactory()
77{
78}
[3]79
[45]80template <typename T>
81const QString& ComponentFactory<T>::getType() const
82{
[3]83 return mType;
[45]84}
[3]85
[45]86template<typename T>
87ComponentBase* ComponentFactory<T>::instantiateComponent(const QString& name)
88{
[15]89 return new T(name);
[3]90}
91
[45]92} // pacpus
93
[31]94#endif // DEF_PACPUS_DBITEEXCEPTION_H
Note: See TracBrowser for help on using the repository browser.