1 #ifndef SimTK_SimTKCOMMON_PLUGIN_H_
2 #define SimTK_SimTKCOMMON_PLUGIN_H_
98 explicit Plugin(
const std::string& defaultPathname=
"");
114 bool load(
const std::string& name=
"");
127 return m_loadedPathname;
140 m_searchPath.clear();
141 for (
unsigned i=0; i < pathIn.
size(); ++i)
142 addSearchDirectory(pathIn[i]);
153 m_searchPath.push_back(absDir);
163 m_searchPath.insert(m_searchPath.begin(), absDir);
169 static void* loadPluginByFileName(
const std::string& name, std::string& errMsg);
174 static void* loadDebugOrReleasePlugin(
const std::string& base,
const std::string& extension,
175 std::string& loadedFileName, std::string& errMsg);
178 static void* getSymbolAddress(
void* handle,
const std::string& name, std::string& errMsg);
181 static void unloadPlugin(
void* handle);
224 static bool deconstructLibraryName(
const std::string& name,
225 bool& isAbsolutePath,
226 std::string& directory,
227 std::string& libPrefix,
228 std::string& baseName,
229 std::string& debugSuffix,
230 std::string& extension);
235 static std::string getDynamicLibPrefix();
240 static std::string getDynamicLibExtension();
246 static std::string getDynamicLibDebugSuffix();
259 #define SimTK_PLUGIN_XXX_MAKE_HOLDER(FuncName) \
260 struct FuncName##__Holder__ { \
261 FuncName##__Holder__() : fp(0) {} \
262 bool loadSym(void* h, std::string& msg) const { \
263 if(!fp) fp =(FuncName##__Type__) \
264 Plugin::getSymbolAddress(h, #FuncName, msg); \
267 mutable FuncName##__Type__ fp; \
269 #define SimTK_PLUGIN_XXX_MAKE_BODY(FuncName) \
270 if (!FuncName##__Ref__.loadSym(m_handle,m_lastMessage)) \
271 throw std::runtime_error \
272 ("Plugin function " #FuncName " not found: " + m_lastMessage); \
273 return FuncName##__Ref__.fp
274 #define SimTK_PLUGIN_XXX_MAKE_SYMTEST(Symbol) \
275 bool has_##Symbol() const { \
276 return Symbol##__Ref__.loadSym(m_handle,m_lastMessage); \
279 #define SimTK_PLUGIN_DEFINE_SYMBOL(Type, SymName) \
280 typedef Type SymName##__Type__; \
281 SimTK_PLUGIN_XXX_MAKE_HOLDER(SymName); \
282 const Type& SymName() const { \
283 if (!SymName##__Ref__.loadSym(m_handle,m_lastMessage)) \
284 throw std::runtime_error \
285 ("Plugin symbol " #SymName " not found: " + m_lastMessage); \
286 return *(SymName##__Ref__.fp); \
289 #define SimTK_PLUGIN_DEFINE_FUNCTION(RetType, FuncName) \
290 typedef RetType (*FuncName##__Type__)(); \
291 SimTK_PLUGIN_XXX_MAKE_HOLDER(FuncName); \
292 RetType FuncName() const { \
293 SimTK_PLUGIN_XXX_MAKE_BODY(FuncName)(); \
295 SimTK_PLUGIN_XXX_MAKE_SYMTEST(FuncName)
297 #define SimTK_PLUGIN_DEFINE_FUNCTION1(RetType, FuncName, Arg1) \
298 typedef RetType (*FuncName##__Type__)(Arg1); \
299 SimTK_PLUGIN_XXX_MAKE_HOLDER(FuncName); \
300 RetType FuncName(Arg1 a1) const { \
301 SimTK_PLUGIN_XXX_MAKE_BODY(FuncName)(a1); \
303 SimTK_PLUGIN_XXX_MAKE_SYMTEST(FuncName)
305 #define SimTK_PLUGIN_DEFINE_FUNCTION2(RetType, FuncName, Arg1, Arg2) \
306 typedef RetType (*FuncName##__Type__)(Arg1,Arg2); \
307 SimTK_PLUGIN_XXX_MAKE_HOLDER(FuncName); \
308 RetType FuncName(Arg1 a1, Arg2 a2) const { \
309 SimTK_PLUGIN_XXX_MAKE_BODY(FuncName)(a1,a2); \
311 SimTK_PLUGIN_XXX_MAKE_SYMTEST(FuncName)
316 #endif // SimTK_SimTKCOMMON_PLUGIN_H_