31 #include "../../Defines.h" 33 #include "../translator/Translator.h" 36 #error "Could not determine platform! Please, contact TerraLib team (terralib-team@terralib.org) for helping support this platform!" 45 #include <boost/format.hpp> 47 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS 49 #elif (TE_PLATFORM == TE_PLATFORMCODE_LINUX) || (TE_PLATFORM == TE_PLATFORMCODE_APPLE) 53 #error "Platform not supported! Please, contact TerraLib team (terralib-team@terralib.org) for helping support this platform!" 61 #if defined(TE_PLATFORM) && (TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS) 62 static bool added_search_path_;
65 Impl(
const std::string& slib_file_name);
68 #if (TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS) 69 bool te::core::Library::Impl::added_search_path_(
false);
73 : slib_file_name(slib_file_name),
80 #if (TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS) 84 LPVOID lp_msg_buf = 0;
86 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
89 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
95 return std::string(
TE_TR(
"Shared Library: could not determine the Operational System report error!"));
97 std::string msg((
char*)(lp_msg_buf));
99 LocalFree(lp_msg_buf);
103 #elif (TE_PLATFORM == TE_PLATFORMCODE_LINUX) || (TE_PLATFORM == TE_PLATFORMCODE_APPLE) 105 const char* err_msg = dlerror();
107 return (err_msg ==
nullptr) ? std::string(
TE_TR(
"Shared Library: could not determine the Operational System report error!")) : std::string(err_msg);
111 #error "Platform not supported! Please, contact TerraLib team (terralib-team@terralib.org) for helping support this platform!" 119 if(slib_file_name.empty() || std::all_of(slib_file_name.begin(), slib_file_name.end(), isspace))
121 boost::format err_msg(
TE_TR(
"Library name cannot be empty."));
157 #if (TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS) 161 #elif (TE_PLATFORM == TE_PLATFORMCODE_LINUX) || (TE_PLATFORM == TE_PLATFORMCODE_APPLE) 167 #error "Platform not supported! Please, contact TerraLib team (terralib-team@terralib.org) for helping support this platform!" 173 boost::format err_msg(
TE_TR(
"Could not load library: %1%, due to following error: %2%."));
185 #if (TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS) 191 boost::format err_msg(
TE_TR(
"Could not unload library: %1%, due to following error: %2%."));
196 #elif (TE_PLATFORM == TE_PLATFORMCODE_LINUX) || (TE_PLATFORM == TE_PLATFORMCODE_APPLE) 200 boost::format err_msg(
TE_TR(
"Could not unload library: %1%, due to following error: %2%."));
207 #error "Platform not supported! Please, contact TerraLib team (terralib-team@terralib.org) for helping support this platform!" 231 #if (TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS) 233 void* f = GetProcAddress((HINSTANCE)
m_pimpl->
module, symbol);
235 #elif (TE_PLATFORM == TE_PLATFORMCODE_LINUX) || (TE_PLATFORM == TE_PLATFORMCODE_APPLE) 241 #error "Platform not supported! Please, contact TerraLib team (terralib-team@terralib.org) for helping support this platform!" 247 boost::format err_msg(
TE_TR(
"Could not find symbol: %1%, in the library %2%, due to the following error: %3%."));
258 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS 260 std::string nativeName = name +
".dll";
262 std::string nativeName = name +
"d.dll";
265 #elif TE_PLATFORM == TE_PLATFORMCODE_LINUX 266 std::string nativeName =
"lib" + name +
".so";
268 #elif TE_PLATFORM == TE_PLATFORMCODE_APPLE 269 std::string nativeName =
"lib" + name +
".dylib";
272 #error "Platform not supported! Please, contact the TerraLib team (terralib-team@dpi.inpe.br) for helping support this platform!" 278 #if (TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS) 282 if(dir_name.length() > (MAX_PATH - 2))
284 boost::format err_msg(
TE_TR(
"The DLL lookup path is too long: %1%."));
290 BOOL retval = SetDllDirectory(dir_name.c_str());
294 boost::format err_msg(
TE_TR(
"The informed dir \"%1%\" couldn't be added to the application dll lookup path due to the following error: \"%2%\"."));
299 te::core::Library::Impl::added_search_path_ =
true;
306 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS 309 BOOL retval = SetDllDirectory(
"");
313 boost::format err_msg(
TE_TR(
"Couldn't come back with default Windows DLL lookup path due to the following error: %1%."));
318 te::core::Library::Impl::added_search_path_ =
false;
325 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS 327 const DWORD buff_size = 32768;
329 char buff[buff_size];
331 DWORD length = GetDllDirectory(buff_size, buff);
333 if(length == 0 && te::core::Library::Impl::added_search_path_)
335 boost::format err_msg(
TE_TR(
"Couldn't get Windows DLL lookup path due to the following error: %1%!"));
340 if(length <= buff_size)
342 return std::string(buff, length);
349 #elif (TE_PLATFORM == TE_PLATFORMCODE_LINUX) || (TE_PLATFORM == TE_PLATFORMCODE_APPLE) 351 #if (TE_PLATFORM == TE_PLATFORMCODE_LINUX) 352 const char* ldLibraryPath = getenv(
"LD_LIBRARY_PATH");
354 const char* ldLibraryPath = getenv(
"DYLD_LIBRARY_PATH");
357 return (ldLibraryPath ==
nullptr) ? std::string(
"") : std::string(ldLibraryPath);
361 #error "Platform not supported! Please, contact TerraLib team (terralib-team@terralib.org) for helping support this platform!" An exception indicating the operational system search library path was not found. ...
An exception indicating an error when reseting the operational system search library path...
A class for handling shared libraries.
An exception indicating an error when loading a shared library.
An exception indicating an error when releasing a shared library.
std::string slib_file_name
The shared library file name.
Specific exception types for Library Manager.
static void addSearchDir(const std::string &dir_name)
Add the informed dir to the path used by the operational system to lookup for shared libraries...
void unload()
Force the unload of the shared library from memory.
#define TE_TR(message)
It marks a string in order to get translated.
boost::error_info< struct tag_error_description, std::string > ErrorDescription
The base type for error report messages.
static std::string getSearchPath()
Returns the system lookup path.
~Library()
The destructor automatically unloads from memory the shared library if it was not unloaded explicitly...
static void resetSearchPath()
Comes back the application lookup path to the original state, before any add_search_dir has been call...
void * getAddress(const char *symbol) const
Return the address where the given symbol is loaded into memory.
Library(const std::string &slib_file_name, const bool &delay_load=false)
Load a new shared library.
TECOMMONEXPORT const std::string GetLastError()
It obtains the last thread's error message in a Windows System.
Impl(const std::string &slib_file_name)
const std::string & getFileName() const
Return the shared library file name as informed in the constructor.
void * module
The handle for a DLLs, SO or a DyLib.
An exception indicating an error when adding a given path to the operational system search library pa...
An exception indicating an error when searching for a given symbol in a shared library.
static std::string te_get_os_error()
bool isLoaded() const
Return true if the shared library is loaded otherwise return false.
void load()
Load the shared library to memory.
static std::string getNativeName(const std::string &name)
Given a shared library name without file extensions, prefixes and nor suffixes it will construct a li...
An execption indicating an error when the library name is empty.