2009-09-28 19:43:28 +04:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
2002-08-21 19:58:01 +04:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
2002-08-21 19:58:01 +04:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
|
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the License for more information.
|
|
|
|
============================================================================*/
|
2002-08-21 19:58:01 +04:00
|
|
|
#include "cmDynamicLoader.h"
|
|
|
|
|
2006-03-16 19:01:05 +03:00
|
|
|
class cmDynamicLoaderCache
|
2002-09-24 00:24:59 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
~cmDynamicLoaderCache();
|
2016-08-17 02:49:57 +03:00
|
|
|
void CacheFile(const char* path,
|
|
|
|
const cmsys::DynamicLoader::LibraryHandle& /*p*/);
|
|
|
|
bool GetCacheFile(const char* path,
|
|
|
|
cmsys::DynamicLoader::LibraryHandle& /*p*/);
|
2002-09-24 00:24:59 +04:00
|
|
|
bool FlushCache(const char* path);
|
|
|
|
void FlushCache();
|
|
|
|
static cmDynamicLoaderCache* GetInstance();
|
|
|
|
|
|
|
|
private:
|
2014-02-10 09:21:34 +04:00
|
|
|
std::map<std::string, cmsys::DynamicLoader::LibraryHandle> CacheMap;
|
2002-09-24 00:24:59 +04:00
|
|
|
static cmDynamicLoaderCache* Instance;
|
|
|
|
};
|
|
|
|
|
2016-06-27 23:44:16 +03:00
|
|
|
cmDynamicLoaderCache* cmDynamicLoaderCache::Instance = CM_NULLPTR;
|
2002-09-24 00:24:59 +04:00
|
|
|
|
|
|
|
cmDynamicLoaderCache::~cmDynamicLoaderCache()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
void cmDynamicLoaderCache::CacheFile(
|
|
|
|
const char* path, const cmsys::DynamicLoader::LibraryHandle& p)
|
2002-09-24 00:24:59 +04:00
|
|
|
{
|
2006-03-16 19:01:05 +03:00
|
|
|
cmsys::DynamicLoader::LibraryHandle h;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->GetCacheFile(path, h)) {
|
2002-09-24 00:24:59 +04:00
|
|
|
this->FlushCache(path);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2006-03-15 19:02:08 +03:00
|
|
|
this->CacheMap[path] = p;
|
2002-09-24 00:24:59 +04:00
|
|
|
}
|
|
|
|
|
2006-03-16 19:01:05 +03:00
|
|
|
bool cmDynamicLoaderCache::GetCacheFile(const char* path,
|
2016-05-16 17:34:04 +03:00
|
|
|
cmsys::DynamicLoader::LibraryHandle& p)
|
2002-09-24 00:24:59 +04:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
std::map<std::string, cmsys::DynamicLoader::LibraryHandle>::iterator it =
|
|
|
|
this->CacheMap.find(path);
|
|
|
|
if (it != this->CacheMap.end()) {
|
2002-09-24 00:24:59 +04:00
|
|
|
p = it->second;
|
|
|
|
return true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2002-09-24 00:24:59 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmDynamicLoaderCache::FlushCache(const char* path)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
std::map<std::string, cmsys::DynamicLoader::LibraryHandle>::iterator it =
|
|
|
|
this->CacheMap.find(path);
|
2002-10-01 00:25:02 +04:00
|
|
|
bool ret = false;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (it != this->CacheMap.end()) {
|
2006-03-16 19:01:05 +03:00
|
|
|
cmsys::DynamicLoader::CloseLibrary(it->second);
|
2006-03-15 19:02:08 +03:00
|
|
|
this->CacheMap.erase(it);
|
2002-10-01 00:25:02 +04:00
|
|
|
ret = true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2002-10-01 00:25:02 +04:00
|
|
|
return ret;
|
2002-09-24 00:24:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmDynamicLoaderCache::FlushCache()
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
for (std::map<std::string, cmsys::DynamicLoader::LibraryHandle>::iterator
|
|
|
|
it = this->CacheMap.begin();
|
|
|
|
it != this->CacheMap.end(); it++) {
|
2006-03-16 19:01:05 +03:00
|
|
|
cmsys::DynamicLoader::CloseLibrary(it->second);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2002-10-10 01:47:24 +04:00
|
|
|
delete cmDynamicLoaderCache::Instance;
|
2016-06-27 23:44:16 +03:00
|
|
|
cmDynamicLoaderCache::Instance = CM_NULLPTR;
|
2002-09-24 00:24:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
cmDynamicLoaderCache* cmDynamicLoaderCache::GetInstance()
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!cmDynamicLoaderCache::Instance) {
|
2002-09-24 00:24:59 +04:00
|
|
|
cmDynamicLoaderCache::Instance = new cmDynamicLoaderCache;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2002-09-24 00:24:59 +04:00
|
|
|
return cmDynamicLoaderCache::Instance;
|
|
|
|
}
|
|
|
|
|
2006-03-16 19:01:05 +03:00
|
|
|
cmsys::DynamicLoader::LibraryHandle cmDynamicLoader::OpenLibrary(
|
2016-05-16 17:34:04 +03:00
|
|
|
const char* libname)
|
2002-08-21 19:58:01 +04:00
|
|
|
{
|
2006-03-16 19:01:05 +03:00
|
|
|
cmsys::DynamicLoader::LibraryHandle lh;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (cmDynamicLoaderCache::GetInstance()->GetCacheFile(libname, lh)) {
|
2002-09-24 00:24:59 +04:00
|
|
|
return lh;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2006-03-16 19:01:05 +03:00
|
|
|
lh = cmsys::DynamicLoader::OpenLibrary(libname);
|
2002-09-24 00:24:59 +04:00
|
|
|
cmDynamicLoaderCache::GetInstance()->CacheFile(libname, lh);
|
|
|
|
return lh;
|
2002-08-21 19:58:01 +04:00
|
|
|
}
|
|
|
|
|
2002-09-24 18:24:56 +04:00
|
|
|
void cmDynamicLoader::FlushCache()
|
|
|
|
{
|
|
|
|
cmDynamicLoaderCache::GetInstance()->FlushCache();
|
|
|
|
}
|