//*************************************************************************** // // (c) 1999-2001 by Microsoft Corp. All Rights Reserved. // // objcache.h // // cvadai 19-Mar-99 Created as prototype for Quasar. // //*************************************************************************** #ifndef _OBJCACHE_H_ #define _OBJCACHE_H_ typedef __int64 SQL_ID; #pragma warning( disable : 4251 ) // needs to have dll-interface to be used by clients of class #define NUM_BUCKETS 100 //*************************************************************************** // CHashCache //*************************************************************************** class POLARITY CListElement { public: CListElement() {}; ~CListElement() {}; void *m_pObj; SQL_ID m_dId; }; #define CHASHCACHE_INLINED #ifndef CHASHCACHE_INLINED class POLARITY CHashCache { typedef std::map CacheInfo; public: CHashCache(); ~CHashCache(); HRESULT Insert(SQL_ID dId, void *pNew); HRESULT Delete(SQL_ID dId); bool Exists (SQL_ID dId); HRESULT Get(SQL_ID dId, void **ppObj); HRESULT Empty(); CListElement *FindFirst(); CListElement *FindNext(SQL_ID dLast); private: CacheInfo m_info; }; #else class _WMILockit11 { public: _WMILockit11(CRITICAL_SECTION *pCS) { EnterCriticalSection(pCS); m_cs = pCS; } ~_WMILockit11() { LeaveCriticalSection(m_cs); } private: CRITICAL_SECTION *m_cs; }; template class POLARITY CHashCache { public: CHashCache(); ~CHashCache(); HRESULT Insert(SQL_ID dId, T pNew); HRESULT Delete(SQL_ID dId); bool Exists (SQL_ID dId); HRESULT Get(SQL_ID dId, T *ppObj); HRESULT Empty(); CListElement *FindFirst(); CListElement *FindNext(SQL_ID dLast); private: std::map m_info; CRITICAL_SECTION m_cs; }; //*************************************************************************** // // CHashCache::CHashCache // //*************************************************************************** template CHashCache::CHashCache() { InitializeCriticalSection(&m_cs); } //*************************************************************************** // // CHashCache::~CHashCache // //*************************************************************************** template CHashCache::~CHashCache() { Empty(); DeleteCriticalSection(&m_cs); } //*************************************************************************** // // CHashCache::Insert // //*************************************************************************** template HRESULT CHashCache::Insert(SQL_ID dId, T pUnk) { _WMILockit11 _Lk(&m_cs); HRESULT hr = WBEM_S_NO_ERROR; T pTmp = m_info[dId]; if (pTmp != pUnk) delete pTmp; m_info[dId] = pUnk; return hr; } //*************************************************************************** // // CHashCache::Delete // //*************************************************************************** template HRESULT CHashCache::Delete(SQL_ID dId) { _WMILockit11 _Lk(&m_cs); HRESULT hr = WBEM_S_NO_ERROR; delete m_info[dId]; m_info[dId] = NULL; std::map ::iterator it = m_info.find(dId); if (it != m_info.end()) m_info.erase(it); return hr; } //*************************************************************************** // // CHashCache::Exists // //*************************************************************************** template bool CHashCache::Exists (SQL_ID dId) { _WMILockit11 _Lk(&m_cs); bool bRet = false; std::map ::iterator it = m_info.find(dId); if (it != m_info.end()) bRet = true; return bRet; } //*************************************************************************** // // CHashCache::Get // //*************************************************************************** template HRESULT CHashCache::Get(SQL_ID dId, T *ppObj) { _WMILockit11 _Lk(&m_cs); HRESULT hr = WBEM_S_NO_ERROR; std::map ::iterator it = m_info.find(dId); if (it != m_info.end()) { *ppObj = (*it).second; } else hr = WBEM_E_NOT_FOUND; return hr; } //*************************************************************************** // // CHashCache::FindFirst // //*************************************************************************** template CListElement *CHashCache::FindFirst() { _WMILockit11 _Lk(&m_cs); CListElement *pRet = NULL; std::map ::iterator it = m_info.begin(); while (it != m_info.end()) { if ((*it).second) { pRet = new CListElement; if (pRet) { pRet->m_dId = (*it).first; pRet->m_pObj = (*it).second; } break; } it++; } return pRet; } //*************************************************************************** // // CHashCache::FindNext // //*************************************************************************** template CListElement *CHashCache::FindNext(SQL_ID dId) { _WMILockit11 _Lk(&m_cs); CListElement *pRet = NULL; std::map ::iterator it = m_info.find(dId); if (it == m_info.end()) { it = m_info.begin(); while (it != m_info.end() && (*it).first < dId) { it++; } } if (it != m_info.end()) it++; while (it != m_info.end()) { if ((*it).second) { pRet = new CListElement; if (pRet) { pRet->m_dId = (*it).first; pRet->m_pObj = (*it).second; } break; } it++; } return pRet; } //*************************************************************************** // // CHashCache::Empty // //*************************************************************************** template HRESULT CHashCache::Empty() { _WMILockit11 _Lk(&m_cs); HRESULT hr = WBEM_S_NO_ERROR; CListElement *pRet = NULL; std::map ::iterator it = m_info.begin(); while (it != m_info.end()) { delete (*it).second; it++; } m_info.clear(); return hr; } #endif #endif