Commit 31af2901 authored by Oleg.Korshul's avatar Oleg.Korshul Committed by Alexander Trofimov

git-svn-id:...

git-svn-id: svn://fileserver/activex/AVS/Sources/TeamlabOffice/trunk/ServerComponents@55217 954022d7-b5bf-4e40-9824-e11837661b57
parent 35b7c84e
#ifndef _BUILD_IMAGEFILESCACHE_H_ #ifndef _BUILD_IMAGEFILESCACHE_H_
#define _BUILD_IMAGEFILESCACHE_H_ #define _BUILD_IMAGEFILESCACHE_H_
#include "Image.h" #include "Image.h"
#include "TemporaryCS.h" #include "TemporaryCS.h"
#include <map> #include <map>
class CCacheImage class CCacheImage
{ {
private: private:
Aggplus::CImage m_oImage; Aggplus::CImage m_oImage;
LONG m_lRef; LONG m_lRef;
public: public:
CCacheImage() : m_oImage() CCacheImage() : m_oImage()
{ {
m_lRef = 1; m_lRef = 1;
} }
CCacheImage(const std::wstring& strFile) : m_oImage(strFile) CCacheImage(const std::wstring& strFile) : m_oImage(strFile)
{ {
m_lRef = 1; m_lRef = 1;
} }
LONG AddRef() LONG AddRef()
{ {
++m_lRef; ++m_lRef;
return m_lRef; return m_lRef;
} }
LONG Release() LONG Release()
{ {
--m_lRef; --m_lRef;
if (0 == m_lRef) if (0 == m_lRef)
{ {
delete this; delete this;
return 0; return 0;
} }
return m_lRef; return m_lRef;
} }
Aggplus::CImage* GetImage() Aggplus::CImage* GetImage()
{ {
return &m_oImage; return &m_oImage;
} }
}; };
class CImageFilesCache class CImageFilesCache
{ {
private: private:
std::map<std::wstring, CCacheImage*> m_mapImages; std::map<std::wstring, CCacheImage*> m_mapImages;
LONG m_lMaxCount; LONG m_lMaxCount;
LONG m_lRef; LONG m_lRef;
NSCriticalSection::CRITICAL_SECTION m_oCS; NSCriticalSection::CRITICAL_SECTION m_oCS;
public: public:
CImageFilesCache() CImageFilesCache()
{ {
m_lMaxCount = 10; m_lMaxCount = 10;
m_lRef = 1; m_lRef = 1;
m_oCS.InitializeCriticalSection(); m_oCS.InitializeCriticalSection();
} }
~CImageFilesCache() ~CImageFilesCache()
{ {
Clear(); Clear();
m_oCS.DeleteCriticalSection(); m_oCS.DeleteCriticalSection();
} }
void Clear() void Clear()
{ {
CTemporaryCS oCS(&m_oCS); CTemporaryCS oCS(&m_oCS);
for (std::map<std::wstring,CCacheImage*>::iterator it = m_mapImages.begin(); it != m_mapImages.end(); ++it) for (std::map<std::wstring,CCacheImage*>::iterator it = m_mapImages.begin(); it != m_mapImages.end(); ++it)
{ {
it->second->Release(); it->second->Release();
} }
m_mapImages.clear(); m_mapImages.clear();
} }
CCacheImage* Lock(const std::wstring& strFile) CCacheImage* Lock(const std::wstring& strFile)
{ {
CTemporaryCS oCS(&m_oCS); CTemporaryCS oCS(&m_oCS);
std::map<std::wstring,CCacheImage*>::iterator it = m_mapImages.find(strFile); std::map<std::wstring,CCacheImage*>::iterator it = m_mapImages.find(strFile);
if (it != m_mapImages.end()) if (it != m_mapImages.end())
{ {
CCacheImage* pImage = it->second; CCacheImage* pImage = it->second;
pImage->AddRef(); pImage->AddRef();
return pImage; return pImage;
} }
int nCount = (int)m_mapImages.size(); int nCount = (int)m_mapImages.size();
if (nCount >= m_lMaxCount) if (nCount >= m_lMaxCount)
{ {
int nNeedDelete = nCount - m_lMaxCount; int nNeedDelete = nCount - m_lMaxCount;
for (std::map<std::wstring,CCacheImage*>::iterator it2 = m_mapImages.begin(); it2 != m_mapImages.end(); ++it2) for (std::map<std::wstring,CCacheImage*>::iterator it2 = m_mapImages.begin(); it2 != m_mapImages.end(); ++it2)
{ {
if (nNeedDelete == 0) if (nNeedDelete == 0)
break; break;
it2->second->Release(); it2->second->Release();
m_mapImages.erase(it2); m_mapImages.erase(it2);
} }
} }
CCacheImage* pImage = new CCacheImage(strFile); CCacheImage* pImage = new CCacheImage(strFile);
m_mapImages[strFile] = pImage; m_mapImages[strFile] = pImage;
pImage->AddRef(); pImage->AddRef();
return pImage; return pImage;
} }
LONG AddRef() LONG AddRef()
{ {
++m_lRef; ++m_lRef;
return m_lRef; return m_lRef;
} }
LONG Release() LONG Release()
{ {
m_oCS.Enter(); m_oCS.Enter();
--m_lRef; --m_lRef;
if (0 == m_lRef) if (0 == m_lRef)
{ {
m_oCS.Leave(); m_oCS.Leave();
delete this; delete this;
return 0; return 0;
} }
m_oCS.Leave(); m_oCS.Leave();
return m_lRef; return m_lRef;
} }
}; };
#endif // _BUILD_IMAGEFILESCACHE_H_ #endif // _BUILD_IMAGEFILESCACHE_H_
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment