/** * @file include/ActivMiddleware/Misc/Detail/DynamicLibraryLoaderDetail.h * * @brief Header file for dynamic library loader helper implementation details. */ #if (!defined ACTIV_MISC_DETAIL_DYNAMIC_LIBRARY_LOADER_DETAIL_H) #define ACTIV_MISC_DETAIL_DYNAMIC_LIBRARY_LOADER_DETAIL_H #include "ActivMiddleware/CxxYZ/functional.h" #if (defined ACTIV_PLATFORM_POSIX) #include #endif #include ACTIV_BOOST_INCLUDE(tokenizer.hpp) //! Activ namespace. namespace Activ { /** * @brief Function wrapper. */ template class DllFunctionWrapper : public NAME { public: typedef TYPE Type; /** * @brief Constructor. * * Set function pointer to NULL. */ DllFunctionWrapper(); /** * @brief Find the address of the function. * * @param dllHandle handle to the library. */ void FindFunction(const DllHandle dllHandle); protected: Type *m_pFunction; }; // --------------------------------------------------------------------------------------------------------------------------------- template DllFunctionWrapper::DllFunctionWrapper() : m_pFunction(NULL) { } // --------------------------------------------------------------------------------------------------------------------------------- template void DllFunctionWrapper::FindFunction(const DllHandle dllHandle) { // note C-style casts: reinterpret_cast complains with some compilers // this-> syntax as base is a dependent type #if (defined ACTIV_PLATFORM_WINDOWS) if (NULL != dllHandle) m_pFunction = (Type *)::GetProcAddress(dllHandle, this->m_name.c_str()); if (THROW_ON_ERROR) { if (NULL == m_pFunction) ACTIV_THROW_WITH_MESSAGE(OsException, ::GetLastError(), this->m_name); } #elif (defined ACTIV_PLATFORM_POSIX) if (NULL != dllHandle) m_pFunction = (Type *)::dlsym(dllHandle, this->m_name.c_str()); if (THROW_ON_ERROR) { if (NULL == m_pFunction) ACTIV_THROW_WITH_MESSAGE(OsException, errno, this->m_name); } #else #error Write me for this platform #endif } // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- template DynamicLibraryLoader::DynamicLibraryLoader(const String &libraryName) : m_dllHandle(OpenLibrary(libraryName)), m_hasAllAvailable(true) { // For each base of DynamicLibraryLoader, invoke the function object DllFunctionLoader. Boost::mpl::for_each >( CxxYZ::bind(DllFunctionLoader(), CxxYZ::_1, CxxYZ::ref(*this))); } // --------------------------------------------------------------------------------------------------------------------------------- template DynamicLibraryLoader::~DynamicLibraryLoader() { #if (defined ACTIV_PLATFORM_WINDOWS) if (NULL != m_dllHandle) (void) ::FreeLibrary(m_dllHandle); #elif (defined ACTIV_PLATFORM_POSIX) if (NULL != m_dllHandle) (void) ::dlclose(m_dllHandle); #else #error Write me for this platform #endif } // --------------------------------------------------------------------------------------------------------------------------------- template inline bool DynamicLibraryLoader::HasAllAvailable() const { return m_hasAllAvailable; } // --------------------------------------------------------------------------------------------------------------------------------- template DllHandle DynamicLibraryLoader::OpenLibrary(const String &libraryName) { typedef Boost::char_separator CharSeparator; typedef Boost::tokenizer Tokenizer; CharSeparator charSeparator(";"); Tokenizer tokenizer(libraryName, charSeparator); for (Tokenizer::iterator iter = tokenizer.begin(), iterEnd = tokenizer.end(); iter != iterEnd; ++iter) { DllHandle dllHandle; #if (defined ACTIV_PLATFORM_WINDOWS) dllHandle = ::LoadLibrary(iter->c_str()); #elif (defined ACTIV_PLATFORM_POSIX) dllHandle = ::dlopen(iter->c_str(), RTLD_LAZY); #else #error Write me for this platform #endif if (NULL != dllHandle) return dllHandle; } return NULL; } } // namespace Activ #endif // (!defined ACTIV_MISC_DETAIL_DYNAMIC_LIBRARY_LOADER_DETAIL_H)