Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / cmDynamicLoader.cxx
blobfa05d6b02f616edbfede0fa5b481c5eb91a4f2a5
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmDynamicLoader.cxx,v $
5 Language: C++
6 Date: $Date: 2006/03/16 22:09:08 $
7 Version: $Revision: 1.29 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include "cmDynamicLoader.h"
19 class cmDynamicLoaderCache
21 public:
22 ~cmDynamicLoaderCache();
23 void CacheFile(const char* path,
24 const cmsys::DynamicLoader::LibraryHandle&);
25 bool GetCacheFile(const char* path, cmsys::DynamicLoader::LibraryHandle&);
26 bool FlushCache(const char* path);
27 void FlushCache();
28 static cmDynamicLoaderCache* GetInstance();
30 private:
31 std::map<cmStdString, cmsys::DynamicLoader::LibraryHandle> CacheMap;
32 static cmDynamicLoaderCache* Instance;
35 cmDynamicLoaderCache* cmDynamicLoaderCache::Instance = 0;
37 cmDynamicLoaderCache::~cmDynamicLoaderCache()
41 void cmDynamicLoaderCache::CacheFile(const char* path,
42 const cmsys::DynamicLoader::LibraryHandle& p)
44 cmsys::DynamicLoader::LibraryHandle h;
45 if ( this->GetCacheFile(path, h) )
47 this->FlushCache(path);
49 this->CacheMap[path] = p;
52 bool cmDynamicLoaderCache::GetCacheFile(const char* path,
53 cmsys::DynamicLoader::LibraryHandle& p)
55 std::map<cmStdString, cmsys::DynamicLoader::LibraryHandle>::iterator it
56 = this->CacheMap.find(path);
57 if ( it != this->CacheMap.end() )
59 p = it->second;
60 return true;
62 return false;
65 bool cmDynamicLoaderCache::FlushCache(const char* path)
67 std::map<cmStdString, cmsys::DynamicLoader::LibraryHandle>::iterator it
68 = this->CacheMap.find(path);
69 bool ret = false;
70 if ( it != this->CacheMap.end() )
72 cmsys::DynamicLoader::CloseLibrary(it->second);
73 this->CacheMap.erase(it);
74 ret = true;
76 return ret;
79 void cmDynamicLoaderCache::FlushCache()
81 for ( std::map<cmStdString,
82 cmsys::DynamicLoader::LibraryHandle>::iterator it
83 = this->CacheMap.begin();
84 it != this->CacheMap.end(); it++ )
86 cmsys::DynamicLoader::CloseLibrary(it->second);
88 delete cmDynamicLoaderCache::Instance;
89 cmDynamicLoaderCache::Instance = 0;
92 cmDynamicLoaderCache* cmDynamicLoaderCache::GetInstance()
94 if ( !cmDynamicLoaderCache::Instance )
96 cmDynamicLoaderCache::Instance = new cmDynamicLoaderCache;
98 return cmDynamicLoaderCache::Instance;
101 cmsys::DynamicLoader::LibraryHandle cmDynamicLoader::OpenLibrary(
102 const char* libname )
104 cmsys::DynamicLoader::LibraryHandle lh;
105 if ( cmDynamicLoaderCache::GetInstance()->GetCacheFile(libname, lh) )
107 return lh;
109 lh = cmsys::DynamicLoader::OpenLibrary(libname);
110 cmDynamicLoaderCache::GetInstance()->CacheFile(libname, lh);
111 return lh;
114 void cmDynamicLoader::FlushCache()
116 cmDynamicLoaderCache::GetInstance()->FlushCache();