26 #include <usUtils_p.h>
31 #if defined(US_PLATFORM_POSIX)
33 #elif defined(US_PLATFORM_WINDOWS)
34 #ifndef WIN32_LEAN_AND_MEAN
35 #define WIN32_LEAN_AND_MEAN
40 #error Unsupported platform
46 #ifdef US_PLATFORM_POSIX
56 SharedLibraryPrivate()
58 #ifdef US_PLATFORM_WINDOWS
60 #elif defined(US_PLATFORM_APPLE)
73 std::string m_FilePath;
79 SharedLibrary::SharedLibrary()
80 : d(new SharedLibraryPrivate)
90 : d(new SharedLibraryPrivate)
97 : d(new SharedLibraryPrivate)
99 d->m_FilePath = absoluteFilePath;
115 if (d->m_Handle)
throw std::logic_error(std::string(
"Library already loaded: ") +
GetFilePath());
117 #ifdef US_PLATFORM_POSIX
118 d->m_Handle = dlopen(libPath.c_str(), flags);
121 const char* err = dlerror();
122 throw std::runtime_error(err ? std::string(err) : (std::string(
"Error loading ") + libPath));
125 d->m_Handle = LoadLibrary(libPath.c_str());
128 std::string errMsg =
"Loading ";
129 errMsg.append(libPath).append(
"failed with error: ").append(
GetLastErrorStr());
131 throw std::runtime_error(errMsg);
138 #ifdef US_PLATFORM_POSIX
139 #ifdef US_GCC_RTTI_WORKAROUND_NEEDED
140 Load(RTLD_LAZY | RTLD_GLOBAL);
142 Load(RTLD_LAZY | RTLD_LOCAL);
153 #ifdef US_PLATFORM_POSIX
154 if (dlclose(d->m_Handle))
156 const char* err = dlerror();
157 throw std::runtime_error(err ? std::string(err) : (std::string(
"Error unloading ") +
GetLibraryPath()));
160 if (!FreeLibrary((HMODULE)d->m_Handle))
162 std::string errMsg =
"Unloading ";
165 throw std::runtime_error(errMsg);
174 if (
IsLoaded() || !d->m_FilePath.empty())
return;
186 if (!d->m_FilePath.empty())
return d->m_FilePath;
195 d->m_FilePath = absoluteFilePath;
197 std::string name = d->m_FilePath;
199 if (pos != std::string::npos)
201 d->m_Path = d->m_FilePath.substr(0, pos);
202 name = d->m_FilePath.substr(pos+1);
209 if (name.size() >= d->m_Prefix.size() &&
210 name.compare(0, d->m_Prefix.size(), d->m_Prefix) == 0)
212 name = name.substr(d->m_Prefix.size());
214 if (name.size() >= d->m_Suffix.size() &&
215 name.compare(name.size()-d->m_Suffix.size(), d->m_Suffix.size(), d->m_Suffix) == 0)
217 name = name.substr(0, name.size()-d->m_Suffix.size());
229 if (
IsLoaded() || !d->m_FilePath.empty())
return;
241 if (
IsLoaded() || !d->m_FilePath.empty())
return;
243 d->m_Suffix = suffix;
253 if (
IsLoaded() || !d->m_FilePath.empty())
return;
255 d->m_Prefix = prefix;
270 return d->m_Handle != NULL;
std::string GetLibraryPath() const
void SetLibraryPath(const std::string &path)
void SetName(const std::string &name)
SharedLibrary & operator=(const SharedLibrary &other)
void SetPrefix(const std::string &prefix)
void SetFilePath(const std::string &absoluteFilePath)
std::string GetSuffix() const
std::string GetPrefix() const
std::string GetName() const
void SetSuffix(const std::string &suffix)
static const char PATH_SEPARATOR
std::string GetLastErrorStr()
std::string GetFilePath() const