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 #ifndef RTT_COMPONENT_HPP
00037 #define RTT_COMPONENT_HPP
00038 
00039 #include <string>
00040 #include <map>
00041 #include <vector>
00042 #include "rtt-fwd.hpp"
00043 #include "rtt-config.h"
00044 
00045 namespace RTT
00046 {
00050     typedef TaskContext* (*ComponentLoaderSignature)(std::string instance_name);
00051     typedef std::map<std::string,ComponentLoaderSignature> FactoryMap;
00052 
00059     class ComponentFactories
00060     {
00065         RTT_HIDE static FactoryMap* Factories;
00066     public:
00067         RTT_HIDE static FactoryMap& Instance() {
00068             if ( Factories == 0)
00069                 Factories = new FactoryMap();
00070             return *Factories;
00071         }
00072     };
00073 
00078     template<class C>
00079     class ComponentFactoryLoader
00080     {
00081     public:
00082         ComponentFactoryLoader(std::string type_name)
00083         {
00084             ComponentFactories::Instance()[type_name] = &ComponentFactoryLoader<C>::createComponent;
00085         }
00086 
00087         static TaskContext* createComponent(std::string instance_name)
00088         {
00089             return new C(instance_name);
00090         }
00091     };
00092 }
00093 
00094 // Helper macros.
00095 #define ORO_CONCAT_LINE2(x,y) x##y
00096 #define ORO_CONCAT_LINE1(x,y) ORO_CONCAT_LINE2(x,y)
00097 #define ORO_CONCAT_LINE(x) ORO_CONCAT_LINE1(x,__LINE__)
00098 
00099 #define ORO_LIST_COMPONENT_TYPE_str(s) ORO_LIST_COMPONENT_TYPE__str(s)
00100 #define ORO_LIST_COMPONENT_TYPE__str(s) #s
00101 
00102 // ORO_CREATE_COMPONENT and ORO_CREATE_COMPONENT_LIBRARY are only used in shared libraries.
00103 #if defined(OCL_DLL_EXPORT) || defined (RTT_COMPONENT)
00104 
00105 #ifdef _MSC_VER
00106 #pragma warning (disable:4190)
00107 #endif
00108 
00125 #define ORO_CREATE_COMPONENT(CNAME) \
00126 extern "C" { \
00127   RTT_EXPORT RTT::TaskContext* createComponent(std::string instance_name); \
00128   RTT::TaskContext* createComponent(std::string instance_name) \
00129   { \
00130     return new CNAME(instance_name); \
00131   } \
00132   RTT_EXPORT std::string getComponentType(); \
00133   std::string getComponentType() \
00134   { \
00135     return ORO_LIST_COMPONENT_TYPE_str(CNAME); \
00136   } \
00137 } /* extern "C" */
00138 
00146 #define ORO_CREATE_COMPONENT_LIBRARY() \
00147 RTT::FactoryMap* RTT::ComponentFactories::Factories = 0;        \
00148 extern "C" { \
00149   RTT_EXPORT RTT::TaskContext* createComponentType(std::string instance_name, std::string type_name) \
00150   { \
00151     if( RTT::ComponentFactories::Instance().count(type_name) ) \
00152       return RTT::ComponentFactories::Instance()[type_name](instance_name); \
00153     return 0; \
00154   } \
00155   RTT_EXPORT std::vector<std::string> getComponentTypeNames() \
00156   { \
00157     std::vector<std::string> ret; \
00158     RTT::FactoryMap::iterator it; \
00159     for(it = RTT::ComponentFactories::Instance().begin(); it != RTT::ComponentFactories::Instance().end(); ++it) { \
00160         ret.push_back(it->first); \
00161     } \
00162     return ret; \
00163   } \
00164   RTT_EXPORT RTT::FactoryMap* getComponentFactoryMap() { return &RTT::ComponentFactories::Instance(); } \
00165 } /* extern "C" */
00166 
00167 #else
00168 
00169 #if !defined(OCL_STATIC) && !defined(RTT_STATIC) && !defined(RTT_DLL_EXPORT)
00170 #warning "You're compiling with static library settings. The resulting component library \
00171  will not be loadable at runtime with the deployer.\
00172  Compile with -DRTT_COMPONENT to enable dynamic loadable components, \
00173  or use -DRTT_STATIC to suppress this warning."
00174 #endif
00175 
00176 // Static OCL library:
00177 // Identical to ORO_LIST_COMPONENT_TYPE:
00178 #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)); } }
00179 #define ORO_CREATE_COMPONENT_LIBRARY() __attribute__((weak)) RTT::FactoryMap* RTT::ComponentFactories::Factories = 0;
00180 
00181 #endif
00182 
00200 #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)); } }
00201 
00205 #define ORO_CREATE_COMPONENT_TYPE( ) ORO_CREATE_COMPONENT_LIBRARY( )
00206 
00207 #endif
00208 
00209 //#undef ORO_CLOADER_CONCAT


rtt
Author(s): RTT Developers
autogenerated on Thu Jan 2 2014 11:35:19