Orocos Real-Time Toolkit  2.5.0
Component.hpp
Go to the documentation of this file.
00001 /***************************************************************************
00002   tag: Peter Soetens  Thu Jul 3 15:35:28 CEST 2008  Component.hpp
00003 
00004                         Component.hpp -  description
00005                            -------------------
00006     begin                : Thu July 03 2008
00007     copyright            : (C) 2008 Peter Soetens
00008     email                : peter.soetens@fmtc.be
00009 
00010  ***************************************************************************
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU Lesser General Public            *
00013  *   License as published by the Free Software Foundation; either          *
00014  *   version 2.1 of the License, or (at your option) any later version.    *
00015  *                                                                         *
00016  *   This library is distributed in the hope that it will be useful,       *
00017  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00018  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00019  *   Lesser General Public License for more details.                       *
00020  *                                                                         *
00021  *   You should have received a copy of the GNU Lesser General Public      *
00022  *   License along with this library; if not, write to the Free Software   *
00023  *   Foundation, Inc., 59 Temple Place,                                    *
00024  *   Suite 330, Boston, MA  02111-1307  USA                                *
00025  *                                                                         *
00026  ***************************************************************************/
00027 
00036 // note: we use the same header protection as ocl/Component.hpp because we 
00037 // only want to include one of both. This is to be changed once ocl/Component.hpp
00038 // includes this file.
00039 #ifndef OCL_COMPONENT_HPP
00040 #define OCL_COMPONENT_HPP
00041 
00042 #include <string>
00043 #include <map>
00044 #include <vector>
00045 #include "rtt-fwd.hpp"
00046 #include "rtt-config.h"
00047 
00048 namespace RTT
00049 {
00053     typedef TaskContext* (*ComponentLoaderSignature)(std::string instance_name);
00054     typedef std::map<std::string,ComponentLoaderSignature> FactoryMap;
00055 
00062     class ComponentFactories
00063     {
00068         RTT_HIDE static FactoryMap* Factories;
00069     public:
00070         RTT_HIDE static FactoryMap& Instance() {
00071             if ( Factories == 0)
00072                 Factories = new FactoryMap();
00073             return *Factories;
00074         }
00075     };
00076 
00081     template<class C>
00082     class ComponentFactoryLoader
00083     {
00084     public:
00085         ComponentFactoryLoader(std::string type_name)
00086         {
00087             ComponentFactories::Instance()[type_name] = &ComponentFactoryLoader<C>::createComponent;
00088         }
00089 
00090         static TaskContext* createComponent(std::string instance_name)
00091         {
00092             return new C(instance_name);
00093         }
00094     };
00095 }
00096 
00097 // Helper macros.
00098 #define ORO_CONCAT_LINE2(x,y) x##y
00099 #define ORO_CONCAT_LINE1(x,y) ORO_CONCAT_LINE2(x,y)
00100 #define ORO_CONCAT_LINE(x) ORO_CONCAT_LINE1(x,__LINE__)
00101 
00102 #define ORO_LIST_COMPONENT_TYPE_str(s) ORO_LIST_COMPONENT_TYPE__str(s)
00103 #define ORO_LIST_COMPONENT_TYPE__str(s) #s
00104 
00105 // ORO_CREATE_COMPONENT and ORO_CREATE_COMPONENT_LIBRARY are only used in shared libraries.
00106 #if defined(OCL_DLL_EXPORT) || defined (RTT_COMPONENT)
00107 
00108 #ifdef _MSC_VER
00109 #pragma warning (disable:4190)
00110 #endif
00111 
00128 #define ORO_CREATE_COMPONENT(CNAME) \
00129 extern "C" { \
00130   RTT_EXPORT RTT::TaskContext* createComponent(std::string instance_name); \
00131   RTT::TaskContext* createComponent(std::string instance_name) \
00132   { \
00133     return new CNAME(instance_name); \
00134   } \
00135   RTT_EXPORT std::string getComponentType(); \
00136   std::string getComponentType() \
00137   { \
00138     return ORO_LIST_COMPONENT_TYPE_str(CNAME); \
00139   } \
00140 } /* extern "C" */
00141 
00149 #define ORO_CREATE_COMPONENT_LIBRARY() \
00150 RTT::FactoryMap* RTT::ComponentFactories::Factories = 0;    \
00151 extern "C" { \
00152   RTT_EXPORT RTT::TaskContext* createComponentType(std::string instance_name, std::string type_name) \
00153   { \
00154     if( RTT::ComponentFactories::Instance().count(type_name) ) \
00155       return RTT::ComponentFactories::Instance()[type_name](instance_name); \
00156     return 0; \
00157   } \
00158   RTT_EXPORT std::vector<std::string> getComponentTypeNames() \
00159   { \
00160     std::vector<std::string> ret; \
00161     RTT::FactoryMap::iterator it; \
00162     for(it = RTT::ComponentFactories::Instance().begin(); it != RTT::ComponentFactories::Instance().end(); ++it) { \
00163         ret.push_back(it->first); \
00164     } \
00165     return ret; \
00166   } \
00167   RTT_EXPORT RTT::FactoryMap* getComponentFactoryMap() { return &RTT::ComponentFactories::Instance(); } \
00168 } /* extern "C" */
00169 
00170 #else
00171 
00172 #if !defined(OCL_STATIC) && !defined(RTT_STATIC)
00173 #warning "You're compiling with static library settings. The resulting component library \
00174  will not be loadable at runtime with the deployer.\
00175  Compile with -DRTT_COMPONENT to enable dynamic loadable components, \
00176  or use -DRTT_STATIC to suppress this warning."
00177 #endif
00178 
00179 // Static OCL library:
00180 // Identical to ORO_LIST_COMPONENT_TYPE:
00181 #define ORO_CREATE_COMPONENT(CLASS_NAME) namespace { namespace ORO_CONCAT_LINE(LOADER_) { RTT::ComponentFactoryLoader<CLASS_NAME> m_cloader(ORO_LIST_COMPONENT_TYPE_str(CLASS_NAME)); } }
00182 #define ORO_CREATE_COMPONENT_LIBRARY() __attribute__((weak)) RTT::FactoryMap* RTT::ComponentFactories::Factories = 0;
00183 
00184 #endif
00185 
00203 #define ORO_LIST_COMPONENT_TYPE(CLASS_NAME) namespace { namespace ORO_CONCAT_LINE(LOADER_) { RTT::ComponentFactoryLoader<CLASS_NAME> m_cloader(ORO_LIST_COMPONENT_TYPE_str(CLASS_NAME)); } }
00204 
00208 #define ORO_CREATE_COMPONENT_TYPE( ) ORO_CREATE_COMPONENT_LIBRARY( )
00209 
00210 #endif
00211 
00212 //#undef ORO_CLOADER_CONCAT