Changeset 288 in pacpusframework for trunk/include/Pacpus/kernel/ComponentFactory.h


Ignore:
Timestamp:
03/26/14 21:27:30 (10 years ago)
Author:
Marek Kurdej
Message:

Using boost::shared_ptr for storing components.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/Pacpus/kernel/ComponentFactory.h

    r89 r288  
    1616#define DEF_PACPUS_COMPONENTFACTORY_H
    1717
    18 #include <cassert>
    19 
    20 //#include <boost/static_assert.hpp>
    21 //#include <boost/type_traits/is_base_of.hpp>
     18#include <boost/assert.hpp>
     19#include <boost/static_assert.hpp>
     20#include <boost/type_traits/is_base_of.hpp>
    2221
    2322#include <Pacpus/kernel/ComponentFactoryBase.h>
     
    3736  static pacpus::ComponentFactory<className> sFactory(factoryName)
    3837
    39 namespace pacpus {
     38#define PACPUS_REGISTER_COMPONENT(Class) \
     39    static pacpus::ComponentFactory<Class> sFactory(#Class)
     40
     41namespace pacpus
     42{
    4043
    4144/// Use it to interface your components with the application.
     
    4750    : public ComponentFactoryBase
    4851{
    49     //static_assert(,"T must inherit from ComponentBase")
    50     //  BOOST_STATIC_ASSERT_MSG((boost::is_base_of<ComponentBase, T>::value), "T must inherit from ComponentBase");
     52    BOOST_STATIC_ASSERT_MSG((boost::is_base_of<ComponentBase, T>::value), "component T must inherit from ComponentBase");
     53
    5154public:
    5255    /** Ctor of ComponentFactory, initialize the factory of the components of type @em T.
    5356     * @param type Name of the type of the components.
    5457     */
    55     ComponentFactory(const QString& type);
     58    ComponentFactory(QString const& type);
    5659   
    5760    /** Dtor of ComponentFactory. */
     
    6164     * @return Name of the type of the components.
    6265     */
    63     const QString& getType() const;
     66    QString const& getType() const;
    6467
    6568protected:
    66     virtual ComponentBase* instantiateComponent(const QString& name);
    67  
     69    virtual ComponentSharedPointer instantiateComponent(QString const& name);
     70
    6871private:
    6972    QString mType;
     
    7174
    7275template <typename T>
    73 ComponentFactory<T>::ComponentFactory(const QString& type)
    74   : mType(type)
     76ComponentFactory<T>::ComponentFactory(QString const& type)
     77    : mType(type)
    7578{
    76     assert(!type.isEmpty());
     79    BOOST_ASSERT(!type.isEmpty());
    7780    addFactory(this, mType);
    7881}
     
    8487
    8588template <typename T>
    86 const QString& ComponentFactory<T>::getType() const
     89QString const& ComponentFactory<T>::getType() const
    8790{
    8891    return mType;
     
    9093
    9194template<typename T>
    92 ComponentBase* ComponentFactory<T>::instantiateComponent(const QString& name)
     95ComponentSharedPointer ComponentFactory<T>::instantiateComponent(QString const& name)
    9396{
    94     return new T(name);
     97    return ComponentSharedPointer(new T(name));
    9598}
    9699
    97 } // pacpus
     100} // namespace pacpus
    98101
    99102#endif // DEF_PACPUS_DBITEEXCEPTION_H
Note: See TracChangeset for help on using the changeset viewer.