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();
|
2006-03-16 19:01:05 +03:00
|
|
|
void CacheFile(const char* path,
|
|
|
|
const cmsys::DynamicLoader::LibraryHandle&);
|
|
|
|
bool GetCacheFile(const char* path, cmsys::DynamicLoader::LibraryHandle&);
|
2002-09-24 00:24:59 +04:00
|
|
|
bool FlushCache(const char* path);
|
|
|
|
void FlushCache();
|
|
|
|
static cmDynamicLoaderCache* GetInstance();
|
|
|
|
|
|
|
|
private:
|
2006-03-16 19:01:05 +03:00
|
|
|
std::map<cmStdString, cmsys::DynamicLoader::LibraryHandle> CacheMap;
|
2002-09-24 00:24:59 +04:00
|
|
|
static cmDynamicLoaderCache* Instance;
|
|
|
|
};
|
|
|
|
|
|
|
|
cmDynamicLoaderCache* cmDynamicLoaderCache::Instance = 0;
|
|
|
|
|
|
|
|
cmDynamicLoaderCache::~cmDynamicLoaderCache()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2006-03-16 19:01:05 +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;
|
2002-09-24 00:24:59 +04:00
|
|
|
if ( this->GetCacheFile(path, h) )
|
|
|
|
{
|
|
|
|
this->FlushCache(path);
|
|
|
|
}
|
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,
|
|
|
|
cmsys::DynamicLoader::LibraryHandle& p)
|
2002-09-24 00:24:59 +04:00
|
|
|
{
|
2006-03-16 19:01:05 +03:00
|
|
|
std::map<cmStdString, cmsys::DynamicLoader::LibraryHandle>::iterator it
|
|
|
|
= this->CacheMap.find(path);
|
2006-03-15 19:02:08 +03:00
|
|
|
if ( it != this->CacheMap.end() )
|
2002-09-24 00:24:59 +04:00
|
|
|
{
|
|
|
|
p = it->second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmDynamicLoaderCache::FlushCache(const char* path)
|
|
|
|
{
|
2006-03-16 19:01:05 +03:00
|
|
|
std::map<cmStdString, cmsys::DynamicLoader::LibraryHandle>::iterator it
|
|
|
|
= this->CacheMap.find(path);
|
2002-10-01 00:25:02 +04:00
|
|
|
bool ret = false;
|
2006-03-15 19:02:08 +03:00
|
|
|
if ( it != this->CacheMap.end() )
|
2002-09-24 00:24:59 +04:00
|
|
|
{
|
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;
|
2002-09-24 00:24:59 +04:00
|
|
|
}
|
2002-10-01 00:25:02 +04:00
|
|
|
return ret;
|
2002-09-24 00:24:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmDynamicLoaderCache::FlushCache()
|
|
|
|
{
|
2006-03-16 19:01:05 +03:00
|
|
|
for ( std::map<cmStdString,
|
|
|
|
cmsys::DynamicLoader::LibraryHandle>::iterator it
|
|
|
|
= this->CacheMap.begin();
|
2006-03-15 19:02:08 +03:00
|
|
|
it != this->CacheMap.end(); it++ )
|
2002-09-24 00:24:59 +04:00
|
|
|
{
|
2006-03-16 19:01:05 +03:00
|
|
|
cmsys::DynamicLoader::CloseLibrary(it->second);
|
2002-09-24 00:24:59 +04:00
|
|
|
}
|
2002-10-10 01:47:24 +04:00
|
|
|
delete cmDynamicLoaderCache::Instance;
|
|
|
|
cmDynamicLoaderCache::Instance = 0;
|
2002-09-24 00:24:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
cmDynamicLoaderCache* cmDynamicLoaderCache::GetInstance()
|
|
|
|
{
|
|
|
|
if ( !cmDynamicLoaderCache::Instance )
|
|
|
|
{
|
|
|
|
cmDynamicLoaderCache::Instance = new cmDynamicLoaderCache;
|
|
|
|
}
|
|
|
|
return cmDynamicLoaderCache::Instance;
|
|
|
|
}
|
|
|
|
|
2006-03-16 19:01:05 +03:00
|
|
|
cmsys::DynamicLoader::LibraryHandle cmDynamicLoader::OpenLibrary(
|
|
|
|
const char* libname )
|
2002-08-21 19:58:01 +04:00
|
|
|
{
|
2006-03-16 19:01:05 +03:00
|
|
|
cmsys::DynamicLoader::LibraryHandle lh;
|
2002-09-24 00:24:59 +04:00
|
|
|
if ( cmDynamicLoaderCache::GetInstance()->GetCacheFile(libname, lh) )
|
|
|
|
{
|
|
|
|
return lh;
|
|
|
|
}
|
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();
|
|
|
|
}
|