00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
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
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 }
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 }
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
00177
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