2009-09-28 15:43:28 +00:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
2002-08-21 15:58:01 +00:00
|
|
|
|
2009-09-28 15:43:28 +00:00
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
2002-08-21 15:58:01 +00:00
|
|
|
|
2009-09-28 15:43:28 +00: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 15:58:01 +00:00
|
|
|
#include "cmDynamicLoader.h"
|
|
|
|
|
2006-03-16 16:01:05 +00:00
|
|
|
class cmDynamicLoaderCache
|
2002-09-23 20:24:59 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
~cmDynamicLoaderCache();
|
2006-03-16 16:01:05 +00:00
|
|
|
void CacheFile(const char* path,
|
|
|
|
const cmsys::DynamicLoader::LibraryHandle&);
|
|
|
|
bool GetCacheFile(const char* path, cmsys::DynamicLoader::LibraryHandle&);
|
2002-09-23 20:24:59 +00:00
|
|
|
bool FlushCache(const char* path);
|
|
|
|
void FlushCache();
|
|
|
|
static cmDynamicLoaderCache* GetInstance();
|
|
|
|
|
|
|
|
private:
|
2006-03-16 16:01:05 +00:00
|
|
|
std::map<cmStdString, cmsys::DynamicLoader::LibraryHandle> CacheMap;
|
2002-09-23 20:24:59 +00:00
|
|
|
static cmDynamicLoaderCache* Instance;
|
|
|
|
};
|
|
|
|
|
|
|
|
cmDynamicLoaderCache* cmDynamicLoaderCache::Instance = 0;
|
|
|
|
|
|
|
|
cmDynamicLoaderCache::~cmDynamicLoaderCache()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2006-03-16 16:01:05 +00:00
|
|
|
void cmDynamicLoaderCache::CacheFile(const char* path,
|
|
|
|
const cmsys::DynamicLoader::LibraryHandle& p)
|
2002-09-23 20:24:59 +00:00
|
|
|
{
|
2006-03-16 16:01:05 +00:00
|
|
|
cmsys::DynamicLoader::LibraryHandle h;
|
2002-09-23 20:24:59 +00:00
|
|
|
if ( this->GetCacheFile(path, h) )
|
|
|
|
{
|
|
|
|
this->FlushCache(path);
|
|
|
|
}
|
2006-03-15 16:02:08 +00:00
|
|
|
this->CacheMap[path] = p;
|
2002-09-23 20:24:59 +00:00
|
|
|
}
|
|
|
|
|
2006-03-16 16:01:05 +00:00
|
|
|
bool cmDynamicLoaderCache::GetCacheFile(const char* path,
|
|
|
|
cmsys::DynamicLoader::LibraryHandle& p)
|
2002-09-23 20:24:59 +00:00
|
|
|
{
|
2006-03-16 16:01:05 +00:00
|
|
|
std::map<cmStdString, cmsys::DynamicLoader::LibraryHandle>::iterator it
|
|
|
|
= this->CacheMap.find(path);
|
2006-03-15 16:02:08 +00:00
|
|
|
if ( it != this->CacheMap.end() )
|
2002-09-23 20:24:59 +00:00
|
|
|
{
|
|
|
|
p = it->second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmDynamicLoaderCache::FlushCache(const char* path)
|
|
|
|
{
|
2006-03-16 16:01:05 +00:00
|
|
|
std::map<cmStdString, cmsys::DynamicLoader::LibraryHandle>::iterator it
|
|
|
|
= this->CacheMap.find(path);
|
2002-09-30 20:25:02 +00:00
|
|
|
bool ret = false;
|
2006-03-15 16:02:08 +00:00
|
|
|
if ( it != this->CacheMap.end() )
|
2002-09-23 20:24:59 +00:00
|
|
|
{
|
2006-03-16 16:01:05 +00:00
|
|
|
cmsys::DynamicLoader::CloseLibrary(it->second);
|
2006-03-15 16:02:08 +00:00
|
|
|
this->CacheMap.erase(it);
|
2002-09-30 20:25:02 +00:00
|
|
|
ret = true;
|
2002-09-23 20:24:59 +00:00
|
|
|
}
|
2002-09-30 20:25:02 +00:00
|
|
|
return ret;
|
2002-09-23 20:24:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmDynamicLoaderCache::FlushCache()
|
|
|
|
{
|
2006-03-16 16:01:05 +00:00
|
|
|
for ( std::map<cmStdString,
|
|
|
|
cmsys::DynamicLoader::LibraryHandle>::iterator it
|
|
|
|
= this->CacheMap.begin();
|
2006-03-15 16:02:08 +00:00
|
|
|
it != this->CacheMap.end(); it++ )
|
2002-09-23 20:24:59 +00:00
|
|
|
{
|
2006-03-16 16:01:05 +00:00
|
|
|
cmsys::DynamicLoader::CloseLibrary(it->second);
|
2002-09-23 20:24:59 +00:00
|
|
|
}
|
2002-10-09 21:47:24 +00:00
|
|
|
delete cmDynamicLoaderCache::Instance;
|
|
|
|
cmDynamicLoaderCache::Instance = 0;
|
2002-09-23 20:24:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cmDynamicLoaderCache* cmDynamicLoaderCache::GetInstance()
|
|
|
|
{
|
|
|
|
if ( !cmDynamicLoaderCache::Instance )
|
|
|
|
{
|
|
|
|
cmDynamicLoaderCache::Instance = new cmDynamicLoaderCache;
|
|
|
|
}
|
|
|
|
return cmDynamicLoaderCache::Instance;
|
|
|
|
}
|
|
|
|
|
2006-03-16 16:01:05 +00:00
|
|
|
cmsys::DynamicLoader::LibraryHandle cmDynamicLoader::OpenLibrary(
|
|
|
|
const char* libname )
|
2002-08-21 15:58:01 +00:00
|
|
|
{
|
2006-03-16 16:01:05 +00:00
|
|
|
cmsys::DynamicLoader::LibraryHandle lh;
|
2002-09-23 20:24:59 +00:00
|
|
|
if ( cmDynamicLoaderCache::GetInstance()->GetCacheFile(libname, lh) )
|
|
|
|
{
|
|
|
|
return lh;
|
|
|
|
}
|
2006-03-16 16:01:05 +00:00
|
|
|
lh = cmsys::DynamicLoader::OpenLibrary(libname);
|
2002-09-23 20:24:59 +00:00
|
|
|
cmDynamicLoaderCache::GetInstance()->CacheFile(libname, lh);
|
|
|
|
return lh;
|
2002-08-21 15:58:01 +00:00
|
|
|
}
|
|
|
|
|
2002-09-24 14:24:56 +00:00
|
|
|
void cmDynamicLoader::FlushCache()
|
|
|
|
{
|
|
|
|
cmDynamicLoaderCache::GetInstance()->FlushCache();
|
|
|
|
}
|