diff --git a/Framework/Kernel/inc/MantidKernel/DllOpen.h b/Framework/Kernel/inc/MantidKernel/DllOpen.h index 98ce70b6148ea1370ff939a4203285e6fa51f917..888c8ce09d3056320d06e15ea4049c0ef51aa804 100644 --- a/Framework/Kernel/inc/MantidKernel/DllOpen.h +++ b/Framework/Kernel/inc/MantidKernel/DllOpen.h @@ -38,43 +38,43 @@ namespace Kernel { Code Documentation is available at: <http://doxygen.mantidproject.org> */ class MANTID_KERNEL_DLL DllOpen { +public: + // Unconstructible + DllOpen() = delete; + // Not copyable + DllOpen(const DllOpen &) = delete; + ~DllOpen() = delete; + public: /// Static method for opening the shared library - static void *OpenDll(const std::string &); + static void *openDll(const std::string &); /// Static method for opening the shared library - static void *OpenDll(const std::string &, const std::string &); + static void *openDll(const std::string &, const std::string &); /// Static method for retrieving a function pointer - static void *GetFunction(void *, const std::string &); + static void *getFunction(void *, const std::string &); /// Static method for closing the shared library - static void CloseDll(void *); + static void closeDll(void *); /// Static method for converting a filename to a libName (without lib___.so or /// ___.dll) - static const std::string ConvertToLibName(const std::string &); + static const std::string convertToLibName(std::string); /// Adds a directiry to the dll search path. static void addSearchDirectory(const std::string &); private: - /// Constructor private as not needed - DllOpen() = default; - /// Copy operator private as not needed - DllOpen(const DllOpen &) = default; - /// Destructor private as not needed - ~DllOpen() = default; - // private functions specific to implementation /// Implementation specifc static method for opening a shared library - static void *OpenDllImpl(const std::string &); + static void *openDllImpl(const std::string &); /// Implementation specifc static method for retrieving a function pointer - static void *GetFunctionImpl(void *, const std::string &); + static void *getFunctionImpl(void *, const std::string &); /// Implementation specifc static method for closing a shared library - static void CloseDllImpl(void *); + static void closeDllImpl(void *); /// Implementation specifc static method for adding a directiry to the dll /// search path. diff --git a/Framework/Kernel/inc/MantidKernel/LibraryWrapper.h b/Framework/Kernel/inc/MantidKernel/LibraryWrapper.h index 67ee36154af0afdbe826f51734bff126bb6b452b..102c01e30342b8c53a1eec075a65e0a277343343 100644 --- a/Framework/Kernel/inc/MantidKernel/LibraryWrapper.h +++ b/Framework/Kernel/inc/MantidKernel/LibraryWrapper.h @@ -38,19 +38,23 @@ namespace Kernel { */ class MANTID_KERNEL_DLL LibraryWrapper { public: - LibraryWrapper(); - virtual ~LibraryWrapper(); - - // Returns true if DLL is opened or already open - bool OpenLibrary(const std::string &); - - bool OpenLibrary(const std::string &, const std::string &); + // Move-only class. The internal module pointer + // is not safe to copy around. + LibraryWrapper() = default; + LibraryWrapper(const LibraryWrapper &) = delete; + LibraryWrapper& operator=(const LibraryWrapper &) = delete; + LibraryWrapper(LibraryWrapper &&) = default; + LibraryWrapper& operator=(LibraryWrapper &&) = default; + ~LibraryWrapper(); + + bool openLibrary(const std::string &); + bool openLibrary(const std::string &, const std::string &); private: /** An untyped pointer to the loaded library. * This is created and deleted by this class. **/ - void *module; + void *m_module = nullptr; }; } // namespace Kernel diff --git a/Framework/Kernel/src/DllOpen.cpp b/Framework/Kernel/src/DllOpen.cpp index e30995e0a369f15bc1d63a26cafde4917f48d0b7..1bd92c4783833b3609e346f9afab95b215e99491 100644 --- a/Framework/Kernel/src/DllOpen.cpp +++ b/Framework/Kernel/src/DllOpen.cpp @@ -32,9 +32,9 @@ Logger g_log("DllOpen"); * @param libName :: Name of the library. * @return Pointer to library (of type void). **/ -void *DllOpen::OpenDll(const std::string &libName) { +void *DllOpen::openDll(const std::string &libName) { std::string str = LIB_PREFIX + libName + LIB_POSTFIX; - return OpenDllImpl(str); + return openDllImpl(str); } /* Opens the shared library after appending the required formatting, @@ -44,11 +44,11 @@ void *DllOpen::OpenDll(const std::string &libName) { * @param filePath :: The location on the library. * @return Pointer to library (of type void). **/ -void *DllOpen::OpenDll(const std::string &libName, +void *DllOpen::openDll(const std::string &libName, const std::string &filePath) { std::string str = filePath + PATH_SEPERATOR + LIB_PREFIX + libName + LIB_POSTFIX; - return OpenDllImpl(str); + return openDllImpl(str); } /* Retrieves a function from the opened library. @@ -57,15 +57,15 @@ void *DllOpen::OpenDll(const std::string &libName, * @param funcName :: The name of the function to retrieve. * @return Pointer to the function (of type void). **/ -void *DllOpen::GetFunction(void *libName, const std::string &funcName) { - return GetFunctionImpl(libName, funcName); +void *DllOpen::getFunction(void *libName, const std::string &funcName) { + return getFunctionImpl(libName, funcName); } /* Closes an open library. * Calls the correct implementation based on the current O/S. * @param libName :: Name of the library. **/ -void DllOpen::CloseDll(void *libName) { CloseDllImpl(libName); } +void DllOpen::closeDll(void *libName) { closeDllImpl(libName); } /** Converts a file name (without directory) to a undecorated library name. * e.g. MyLibrary.dll or libMyLibary.so would become MyLibrary. @@ -73,28 +73,26 @@ void DllOpen::CloseDll(void *libName) { CloseDllImpl(libName); } * @return The converted libName, or empty string if the conversion was not *possible. **/ -const std::string DllOpen::ConvertToLibName(const std::string &fileName) { - // take a copy of the input string - std::string retVal = fileName; - - if ((retVal.compare(0, LIB_PREFIX.size(), LIB_PREFIX) == 0) && - (retVal.find(PATH_SEPERATOR) == std::string::npos)) { +const std::string DllOpen::convertToLibName(std::string fileName) { + if ((fileName.compare(0, LIB_PREFIX.size(), LIB_PREFIX) == 0) && + (fileName.find(PATH_SEPERATOR) == std::string::npos)) { // found - retVal = - retVal.substr(LIB_PREFIX.size(), retVal.size() - LIB_PREFIX.size()); + fileName = + fileName.substr(LIB_PREFIX.size(), fileName.size() - LIB_PREFIX.size()); } else { // prefix not found return ""; } - std::string::size_type pos = retVal.rfind(LIB_POSTFIX); - if (pos != std::string::npos && pos == (retVal.size() - LIB_POSTFIX.size())) { + std::string::size_type pos = fileName.rfind(LIB_POSTFIX); + if (pos != std::string::npos && + pos == (fileName.size() - LIB_POSTFIX.size())) { // found - retVal = retVal.substr(0, retVal.size() - LIB_POSTFIX.size()); + fileName = fileName.substr(0, fileName.size() - LIB_POSTFIX.size()); } else { // postfix not found return ""; } - return retVal; + return fileName; } /* Adds a directory to the dll cearch path @@ -155,7 +153,7 @@ void *DllOpen::GetFunctionImpl(void *libName, const std::string &funcName) { **/ void DllOpen::CloseDllImpl(void *libName) { FreeLibrary((HINSTANCE)libName); } -/* Adds a directory to the dll cearch path +/* Adds a directory to the dll search path **/ void DllOpen::addSearchDirectoryImpl(const std::string &dir) { SetDllDirectory(dir.c_str()); @@ -177,7 +175,7 @@ const std::string DllOpen::PATH_SEPERATOR = "/"; * @param filePath :: Filepath of the library. * @return Pointer to library (of type void). **/ -void *DllOpen::OpenDllImpl(const std::string &filePath) { +void *DllOpen::openDllImpl(const std::string &filePath) { void *handle = dlopen(filePath.c_str(), RTLD_NOW | RTLD_GLOBAL); if (!handle) { g_log.error("Could not open library " + filePath + ": " + dlerror()); @@ -191,21 +189,22 @@ void *DllOpen::OpenDllImpl(const std::string &filePath) { * @param funcName :: The name of the function to retrieve. * @return Pointer to the function (of type void). **/ -void *DllOpen::GetFunctionImpl(void *libName, const std::string &funcName) { +void *DllOpen::getFunctionImpl(void *libName, const std::string &funcName) { return dlsym(libName, funcName.c_str()); } /* Closes an open .so file. * @param libName :: Name of the library. **/ -void DllOpen::CloseDllImpl(void *libName) { +void DllOpen::closeDllImpl(void *libName) { UNUSED_ARG(libName); // Commented out for now due to a potential bug in glibc // dlclose(libName); } -/* Adds a directory to the dll cearch path - **/ +/* Adds a directory to the dll search path + * @param dir Unused argument + */ void DllOpen::addSearchDirectoryImpl(const std::string &dir) { UNUSED_ARG(dir); } diff --git a/Framework/Kernel/src/LibraryWrapper.cpp b/Framework/Kernel/src/LibraryWrapper.cpp index 9172ddf789109b3b01ca2bdeb3108de748bf3428..cc10a72de35720c21d15ab7004f893117380ab40 100644 --- a/Framework/Kernel/src/LibraryWrapper.cpp +++ b/Framework/Kernel/src/LibraryWrapper.cpp @@ -4,15 +4,12 @@ namespace Mantid { namespace Kernel { -/// Constructor -LibraryWrapper::LibraryWrapper() : module(nullptr) {} - /// Destructor LibraryWrapper::~LibraryWrapper() { // Close lib - if (module) { - DllOpen::CloseDll(module); - module = nullptr; + if (m_module) { + DllOpen::closeDll(m_module); + m_module = nullptr; } } @@ -21,12 +18,12 @@ LibraryWrapper::~LibraryWrapper() { * lib/so/dll). * @return True if DLL is opened or already open. */ -bool LibraryWrapper::OpenLibrary(const std::string &libName) { - if (!module) { +bool LibraryWrapper::openLibrary(const std::string &libName) { + if (!m_module) { // Load dynamically loaded library - module = DllOpen::OpenDll(libName); + m_module = DllOpen::openDll(libName); - if (!module) { + if (!m_module) { return false; } } @@ -40,12 +37,12 @@ bool LibraryWrapper::OpenLibrary(const std::string &libName) { * @param filePath :: The filepath to the directory where the library is. * @return True if DLL is opened or already open */ -bool LibraryWrapper::OpenLibrary(const std::string &libName, +bool LibraryWrapper::openLibrary(const std::string &libName, const std::string &filePath) { - if (!module) { + if (!m_module) { // Load dynamically loaded library - module = DllOpen::OpenDll(libName, filePath); - if (!module) { + m_module = DllOpen::openDll(libName, filePath); + if (!m_module) { return false; } }