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
00028
00029 #ifndef OCL_COMPONENT_LOADER_HPP
00030 #define OCL_COMPONENT_LOADER_HPP
00031
00032 #include <string>
00033 #include <map>
00034 #include <vector>
00035 #include <ocl/OCL.hpp>
00036
00037 namespace RTT {
00038 class TaskContext;
00039 }
00040
00041 namespace OCL
00042 {
00046 typedef RTT::TaskContext* (*ComponentLoaderSignature)(std::string instance_name);
00047 typedef std::map<std::string,ComponentLoaderSignature> FactoryMap;
00048
00053 class ComponentFactories
00054 {
00059 OCL_HIDE static FactoryMap* Factories;
00060 public:
00061 OCL_HIDE static FactoryMap& Instance() {
00062 if ( Factories == 0)
00063 Factories = new FactoryMap();
00064 return *Factories;
00065 }
00066 };
00067
00071 template<class C>
00072 class ComponentLoader
00073 {
00074 public:
00075 ComponentLoader(std::string type_name)
00076 {
00077 ComponentFactories::Instance()[type_name] = &ComponentLoader<C>::createComponent;
00078 }
00079
00080 static RTT::TaskContext* createComponent(std::string instance_name)
00081 {
00082 return new C(instance_name);
00083 }
00084 };
00085 }
00086
00087
00088 #define ORO_CONCAT_LINE2(x,y) x##y
00089 #define ORO_CONCAT_LINE1(x,y) ORO_CONCAT_LINE2(x,y)
00090 #define ORO_CONCAT_LINE(x) ORO_CONCAT_LINE1(x,__LINE__)
00091
00092 #define ORO_LIST_COMPONENT_TYPE_str(s) ORO_LIST_COMPONENT_TYPE__str(s)
00093 #define ORO_LIST_COMPONENT_TYPE__str(s) #s
00094
00095
00096 #if defined(OCL_DLL_EXPORT)
00097
00111 #define ORO_CREATE_COMPONENT(CNAME) \
00112 extern "C" { \
00113 OCL_API RTT::TaskContext* createComponent(std::string instance_name) \
00114 { \
00115 return new CNAME(instance_name); \
00116 } \
00117 OCL_API std::string getComponentType() \
00118 { \
00119 return ORO_LIST_COMPONENT_TYPE_str(CNAME); \
00120 } \
00121 }
00122
00129 #define ORO_CREATE_COMPONENT_TYPE() \
00130 OCL::FactoryMap* OCL::ComponentFactories::Factories = 0; \
00131 extern "C" { \
00132 OCL_API RTT::TaskContext* createComponentType(std::string instance_name, std::string type_name) \
00133 { \
00134 if( OCL::ComponentFactories::Instance().count(type_name) ) \
00135 return OCL::ComponentFactories::Instance()[type_name](instance_name); \
00136 return 0; \
00137 } \
00138 OCL_API std::vector<std::string> getComponentTypeNames() \
00139 { \
00140 std::vector<std::string> ret; \
00141 OCL::FactoryMap::iterator it; \
00142 for(it = OCL::ComponentFactories::Instance().begin(); it != OCL::ComponentFactories::Instance().end(); ++it) { \
00143 ret.push_back(it->first); \
00144 } \
00145 return ret; \
00146 } \
00147 OCL_API OCL::FactoryMap* getComponentFactoryMap() { return &OCL::ComponentFactories::Instance(); } \
00148 }
00149
00150 #else
00151
00152 #ifndef OCL_STATIC
00153 #warning "You're compiling with static library settings. The resulting component library \
00154 will not be loadable at runtime with the deployer.\
00155 Compile with -DOCL_DLL_EXPORT to enable dynamic loadable components, \
00156 or use -DOCL_STATIC to suppress this warning."
00157 #endif
00158
00159
00160
00161 #define ORO_CREATE_COMPONENT(CLASS_NAME) namespace { namespace ORO_CONCAT_LINE(LOADER_) { OCL::ComponentLoader<CLASS_NAME> m_cloader(ORO_LIST_COMPONENT_TYPE_str(CLASS_NAME)); } }
00162 #define ORO_CREATE_COMPONENT_TYPE() __attribute__((weak)) OCL::FactoryMap* OCL::ComponentFactories::Factories = 0;
00163
00164 #endif
00165
00185 #define ORO_LIST_COMPONENT_TYPE(CLASS_NAME) namespace { namespace ORO_CONCAT_LINE(LOADER_) { OCL::ComponentLoader<CLASS_NAME> m_cloader(ORO_LIST_COMPONENT_TYPE_str(CLASS_NAME)); } }
00186
00187
00188 #endif
00189
00190