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
00037
00038
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
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
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 }
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 }
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
00180
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