1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmDynamicLoader.cxx,v $
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
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
);
28 static cmDynamicLoaderCache
* GetInstance();
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() )
65 bool cmDynamicLoaderCache::FlushCache(const char* path
)
67 std::map
<cmStdString
, cmsys::DynamicLoader::LibraryHandle
>::iterator it
68 = this->CacheMap
.find(path
);
70 if ( it
!= this->CacheMap
.end() )
72 cmsys::DynamicLoader::CloseLibrary(it
->second
);
73 this->CacheMap
.erase(it
);
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
) )
109 lh
= cmsys::DynamicLoader::OpenLibrary(libname
);
110 cmDynamicLoaderCache::GetInstance()->CacheFile(libname
, lh
);
114 void cmDynamicLoader::FlushCache()
116 cmDynamicLoaderCache::GetInstance()->FlushCache();