/** * @file include/ActivMiddleware/Misc/DynamicLibraryLoader.h * * @brief Header file for dynamic library loader helper classes. */ #if (!defined ACTIV_MISC_DYNAMIC_LIBRARY_LOADER_H) #define ACTIV_MISC_DYNAMIC_LIBRARY_LOADER_H #include "ActivMiddleware/ActivBase/OsException.h" #include "ActivMiddleware/CxxYZ/functional.h" #include ACTIV_BOOST_INCLUDE(mpl/vector.hpp) #include ACTIV_BOOST_INCLUDE(mpl/inherit_linearly.hpp) #include ACTIV_BOOST_INCLUDE(mpl/inherit.hpp) #include ACTIV_BOOST_INCLUDE(mpl/for_each.hpp) //! Activ namespace. namespace Activ { /** * @brief Handle to dynamic library type. */ #if (defined ACTIV_PLATFORM_WINDOWS) typedef HMODULE DllHandle; #elif (defined ACTIV_PLATFORM_POSIX) typedef void *DllHandle; #else #error Write me for this platform #endif // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Declare a dynamic library function to find. * * This macro creates a new class with the same name as provided to this macro. Therefore, this macro should probably * be used at a different scope to the one containing the function declaration, to avoid clashes. This new class provides * a m_name member (the name of the function), a Type typedef (the type of the function) and a m_pFunction * pointer (to hold the pointer to the function's entry point). * * Note we're not using ACTIV_STRINGIZE on name: we don't want it expanded if it is a pre-processor macro. * * @param name the unquoted name of the library function, e.g. getaddrinfo * @param type the type of the library function, e.g. * int (WINAPI)(const char FAR *, const char FAR *, const struct addrinfo FAR *, struct addrinfo FAR *FAR *) * @param throwOnError boolean whether to throw if the function cannot be found. Useful to distinguish between * functions which must be found, and those that are optional. */ #define ACTIV_DECLARE_DLL_FUNCTION(name, type, throwOnError) \ class name##Name { public: name##Name() : m_name(#name) { } const String m_name; }; \ typedef DllFunctionWrapper name // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Declare a dynamic library function to find. * * This macro creates a new class with the same name as provided to this macro. Therefore, this macro should probably * be used at a different scope to the one containing the function declaration, to avoid clashes. This new class provides * a m_name member (the name of the function), a Type typedef (the type of the function) and a m_pFunction * pointer (to hold the pointer to the function's entry point). * * Note we're not using ACTIV_STRINGIZE on name: we don't want it expanded if it is a pre-processor macro. * * This macro only works where decltype() is available; otherwise use ACTIV_DECLARE_DLL_FUNCTION. It also only works for * global scope functions. * * @param name the unquoted name of the library function, e.g. getaddrinfo * @param throwOnError boolean whether to throw if the function cannot be found. Useful to distinguish between * functions which must be found, and those that are optional. */ #define ACTIV_DECLTYPE_DLL_FUNCTION(name, throwOnError) ACTIV_DECLARE_DLL_FUNCTION(name, decltype(::name), throwOnError) // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Main dynamic library loader class. * * The class is templatized by a Boost::mpl::vector of DllFunctionWrapper classes. For example: * * typedef int (WINAPI getaddrinfo_type)(const char FAR *, const char FAR *, const struct addrinfo FAR *, * struct addrinfo FAR *FAR *); * ACTIV_DECLARE_DLL_FUNCTION(getaddrinfo, getaddrinfo_type, true); * typedef void (WINAPI freeaddrinfo_type)(struct addrinfo FAR *); * ACTIV_DECLARE_DLL_FUNCTION(freeaddrinfo, freeaddrinfo_type, true); * * typedef DynamicLibraryLoader > WinsockLoader; * * Instantiating a WinsockLoader by passing "ws2_32.dll" to the constructor will attempt to open the Winsock library * and find the function pointers for "getaddrinfo" and "freeaddrinfo". * * Different library names can be provided by passing in a semi-colon delimited list, e.g. * "libssl.so.6;libssl.0.9.8;libssl.so". The names are opened in order until a successful open. * * These function pointers can then be accessed using Get(): * winsockLoader.Get()(...); */ template class DynamicLibraryLoader : private Boost::mpl::inherit_linearly >::type { public: /** * @brief Loads the library. * * @param libraryName a semi-colon delimited list of names of this library to attempt to open. */ DynamicLibraryLoader(const String &libraryName); /** * @brief Destructor. * * Unloads the library. */ ~DynamicLibraryLoader(); /** * @brief Get function pointer. * * Template parameter F is a DllFunctionWrapper class from ACTIV_DECLARE_DLL_FUNCTION. Usage is, for example: * winsockLoader.Get()(...); * * @return Function pointer, or NULL if function wasn't found. */ #if (!defined ACTIV_COMPILER_SUNPRO) template typename F::Type *Get() const { return F::m_pFunction; } #else // sun compiler (as of version 5.8) throws error "notfunc" if using the above Get() in one step, e.g. // myLoader.Get()(param, param2); // this works around that, at the cost of using CxxYZ::function template CxxYZ::function Get() const { return CxxYZ::function(typename F::m_pFunction); } #endif /** * @brief Were all the functions found? * * @return boolean. */ bool HasAllAvailable() const; private: /** * @brief Helper class to find function pointer for each base of a DynamicLibraryLoader. */ class DllFunctionLoader { public: /** * @brief Function operator. * * Calls FindFunction() on the base F of the provided DynamicLibraryLoader. * * @param unused * @param dynamicLibraryLoader the DynamicLibraryLoader derived from type F. */ template void operator()(F *unused, DynamicLibraryLoader &dynamicLibraryLoader) const { dynamicLibraryLoader.F::FindFunction(dynamicLibraryLoader.m_dllHandle); if (NULL == dynamicLibraryLoader.F::m_pFunction) dynamicLibraryLoader.m_hasAllAvailable = false; } }; /** * @brief Open the shared library. * * @param libraryName a semi-colon delimited list of names of this library to attempt to open. * * @return Handle to the library, or NULL. */ static DllHandle OpenLibrary(const String &libraryName); const DllHandle m_dllHandle; bool m_hasAllAvailable; }; } // namespace Activ #include "ActivMiddleware/Misc/Detail/DynamicLibraryLoaderDetail.h" #endif // (!defined ACTIV_MISC_DYNAMIC_LIBRARY_LOADER_H)