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) || defined(RTT_COMPONENT)
00097
00111 #define ORO_CREATE_COMPONENT(CNAME) \
00112 extern "C" { \
00113 OCL_EXPORT RTT::TaskContext* createComponent(std::string instance_name); \
00114 RTT::TaskContext* createComponent(std::string instance_name) \
00115 { \
00116 return new CNAME(instance_name); \
00117 } \
00118 OCL_EXPORT std::string getComponentType(); \
00119 std::string getComponentType() \
00120 { \
00121 return ORO_LIST_COMPONENT_TYPE_str(CNAME); \
00122 } \
00123 }
00124
00131 #define ORO_CREATE_COMPONENT_TYPE() \
00132 OCL::FactoryMap* OCL::ComponentFactories::Factories = 0; \
00133 extern "C" { \
00134 OCL_EXPORT RTT::TaskContext* createComponentType(std::string instance_name, std::string type_name) \
00135 { \
00136 if( OCL::ComponentFactories::Instance().count(type_name) ) \
00137 return OCL::ComponentFactories::Instance()[type_name](instance_name); \
00138 return 0; \
00139 } \
00140 OCL_EXPORT std::vector<std::string> getComponentTypeNames() \
00141 { \
00142 std::vector<std::string> ret; \
00143 OCL::FactoryMap::iterator it; \
00144 for(it = OCL::ComponentFactories::Instance().begin(); it != OCL::ComponentFactories::Instance().end(); ++it) { \
00145 ret.push_back(it->first); \
00146 } \
00147 return ret; \
00148 } \
00149 OCL_EXPORT OCL::FactoryMap* getComponentFactoryMap() { return &OCL::ComponentFactories::Instance(); } \
00150 }
00151
00152 #else
00153
00154 #if !defined(OCL_STATIC) && !defined(RTT_STATIC)
00155 #warning "You're compiling with static library settings. The resulting component library \
00156 will not be loadable at runtime with the deployer.\
00157 Compile with -DRTT_COMPONENT to enable dynamic loadable components, \
00158 or use -DRTT_STATIC to suppress this warning."
00159 #endif
00160
00161
00162
00163 #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)); } }
00164 #define ORO_CREATE_COMPONENT_TYPE() __attribute__((weak)) OCL::FactoryMap* OCL::ComponentFactories::Factories = 0;
00165
00166 #endif
00167
00187 #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)); } }
00188
00189
00190 #endif
00191
00192