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 OCL_COMPONENT_HPP
00037 #define OCL_COMPONENT_HPP
00038
00039 #include <string>
00040 #include <map>
00041 #include <vector>
00042 #include <ocl/OCL.hpp>
00043
00044 namespace RTT {
00045 class TaskContext;
00046 }
00047
00048 namespace OCL
00049 {
00053 typedef RTT::TaskContext* (*ComponentLoaderSignature)(std::string instance_name);
00054 typedef std::map<std::string,ComponentLoaderSignature> FactoryMap;
00055
00062 class ComponentFactories
00063 {
00068 OCL_HIDE static FactoryMap* Factories;
00069 public:
00070 OCL_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 RTT::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
00109 #ifdef _MSC_VER
00110 #pragma warning (disable:4190)
00111 #endif
00112
00129 #define ORO_CREATE_COMPONENT(CNAME) \
00130 extern "C" { \
00131 OCL_EXPORT RTT::TaskContext* createComponent(std::string instance_name); \
00132 OCL_EXPORT RTT::TaskContext* createComponent(std::string instance_name) \
00133 { \
00134 return new CNAME(instance_name); \
00135 } \
00136 OCL_EXPORT std::string getComponentType(); \
00137 OCL_EXPORT std::string getComponentType() \
00138 { \
00139 return ORO_LIST_COMPONENT_TYPE_str(CNAME); \
00140 } \
00141 }
00142
00150 #define ORO_CREATE_COMPONENT_LIBRARY() \
00151 OCL::FactoryMap* OCL::ComponentFactories::Factories = 0; \
00152 extern "C" { \
00153 OCL_EXPORT RTT::TaskContext* createComponentType(std::string instance_name, std::string type_name) \
00154 { \
00155 if( OCL::ComponentFactories::Instance().count(type_name) ) \
00156 return OCL::ComponentFactories::Instance()[type_name](instance_name); \
00157 return 0; \
00158 } \
00159 OCL_EXPORT std::vector<std::string> getComponentTypeNames() \
00160 { \
00161 std::vector<std::string> ret; \
00162 OCL::FactoryMap::iterator it; \
00163 for(it = OCL::ComponentFactories::Instance().begin(); it != OCL::ComponentFactories::Instance().end(); ++it) { \
00164 ret.push_back(it->first); \
00165 } \
00166 return ret; \
00167 } \
00168 OCL_EXPORT OCL::FactoryMap* getComponentFactoryMap() { return &OCL::ComponentFactories::Instance(); } \
00169 }
00170
00171 #else
00172
00173 #if !defined(OCL_STATIC) && !defined(RTT_STATIC)
00174 #warning "You're compiling with static library settings. The resulting component library \
00175 will not be loadable at runtime with the deployer.\
00176 Compile with -DRTT_COMPONENT to enable dynamic loadable components, \
00177 or use -DRTT_STATIC to suppress this warning."
00178 #endif
00179
00180
00181
00182 #define ORO_CREATE_COMPONENT(CLASS_NAME) namespace { namespace ORO_CONCAT_LINE(LOADER_) { OCL::ComponentFactoryLoader<CLASS_NAME> m_cloader(ORO_LIST_COMPONENT_TYPE_str(CLASS_NAME)); } }
00183 #define ORO_CREATE_COMPONENT_LIBRARY() __attribute__((weak)) OCL::FactoryMap* OCL::ComponentFactories::Factories = 0;
00184
00185 #endif
00186
00203 #define ORO_LIST_COMPONENT_TYPE(CLASS_NAME) namespace { namespace ORO_CONCAT_LINE(LOADER_) { OCL::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