157 lines
4.4 KiB
C++
157 lines
4.4 KiB
C++
/**
|
|
* @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 <dlfcn.h>
|
|
#endif
|
|
|
|
#include ACTIV_BOOST_INCLUDE(tokenizer.hpp)
|
|
|
|
//! Activ namespace.
|
|
namespace Activ
|
|
{
|
|
|
|
/**
|
|
* @brief Function wrapper.
|
|
*/
|
|
template<typename NAME, typename TYPE, bool THROW_ON_ERROR>
|
|
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<typename NAME, typename TYPE, bool THROW_ON_ERROR>
|
|
DllFunctionWrapper<NAME, TYPE, THROW_ON_ERROR>::DllFunctionWrapper() :
|
|
m_pFunction(NULL)
|
|
{
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<typename NAME, typename TYPE, bool THROW_ON_ERROR>
|
|
void DllFunctionWrapper<NAME, TYPE, THROW_ON_ERROR>::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<typename T>
|
|
DynamicLibraryLoader<T>::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<T, Boost::add_pointer<Boost::mpl::_> >(
|
|
CxxYZ::bind<void>(DllFunctionLoader(), CxxYZ::_1, CxxYZ::ref(*this)));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<typename T>
|
|
DynamicLibraryLoader<T>::~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<typename T>
|
|
inline bool DynamicLibraryLoader<T>::HasAllAvailable() const
|
|
{
|
|
return m_hasAllAvailable;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<typename T>
|
|
DllHandle DynamicLibraryLoader<T>::OpenLibrary(const String &libraryName)
|
|
{
|
|
typedef Boost::char_separator<char> CharSeparator;
|
|
typedef Boost::tokenizer<CharSeparator, String::const_iterator, String> 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)
|