forked from rainmeter/rainmeter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageCache.cpp
75 lines (63 loc) · 1.59 KB
/
ImageCache.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* Copyright (C) 2018 Rainmeter Project Developers
*
* This Source Code Form is subject to the terms of the GNU General Public
* License; either version 2 of the License, or (at your option) any later
* version. If a copy of the GPL was not distributed with this file, You can
* obtain one at <https://www.gnu.org/licenses/gpl-2.0.html>. */
#include "StdAfx.h"
#include "ImageCache.h"
void ImageCache::Update(const ImageOptions& key, Gfx::D2DBitmap* item)
{
if (m_Bitmap)
{
delete m_Bitmap;
m_Bitmap = nullptr;
}
m_Key = key;
m_Bitmap = item;
}
ImageCacheHandle::~ImageCacheHandle()
{
--m_Cache->m_Instances;
if (m_Cache->m_Instances == 0)
{
m_Cache->m_Pool->Remove(m_Cache->m_Key);
}
}
ImageCachePool::ImageCachePool()
{
}
ImageCachePool::~ImageCachePool()
{
}
ImageCachePool& ImageCachePool::GetInstance()
{
static ImageCachePool s_CachePool;
return s_CachePool;
}
ImageCacheHandle* ImageCachePool::Get(const ImageOptions& key)
{
const auto find = m_CachePool.find(key);
if (find == m_CachePool.end()) return nullptr;
return new ImageCacheHandle(find->second);
}
void ImageCachePool::Put(const ImageOptions& key, Gfx::D2DBitmap* item)
{
if (m_CachePool.find(key) == m_CachePool.end())
{
m_CachePool[key] = new ImageCache(key, item, this);
return;
}
// sanity check
if (item != nullptr)
{
m_CachePool[key]->Update(key, item);
}
}
void ImageCachePool::Remove(const ImageOptions& item)
{
auto it = m_CachePool.find(item);
if (it == m_CachePool.end()) return;
delete it->second;
m_CachePool.erase(it);
}