#ifndef __OBJ_KEY_GENERATOR_H__ #define __OBJ_KEY_GENERATOR_H__ //======================================================================================================================= /// Ű »ý¼º Ŭ·¡½º /** @history - 2006.8.9 : template classÈ­, csÁ¦°Å (taiyo) */ //======================================================================================================================= #pragma once #include //#include "CriticalSection.h" using namespace std; namespace util { /** Objectµé¿¡ Key¸¦ ¹ß±ÞÇϰí ȸ¼öÇÏ´Â ¸Å´ÏÀú */ template class CObjKeyGenerator { private: // CCriticalSection m_cs; deque m_dequeKey; DWORD m_dwKeyStart; DWORD m_dwKeyEnd; public: CObjKeyGenerator(){} CObjKeyGenerator(KeyType dwKeyStart, KeyType dwKeyEnd); ~CObjKeyGenerator(VOID); VOID Create( KeyType dwKeyStart, KeyType dwKeyEnd ); KeyType GetKey(); BOOL ReserveKey(KeyType key); VOID RestoreKey(KeyType key); BOOL IsEmpty(); BOOL IsExistKey(KeyType dwKey); size_t GetSize(); }; template CObjKeyGenerator::CObjKeyGenerator( KeyType dwKeyStart, KeyType dwKeyEnd ) { Create(dwKeyStart, dwKeyEnd); } template VOID CObjKeyGenerator::Create( KeyType dwKeyStart, KeyType dwKeyEnd ) { for( KeyType i = dwKeyStart; i <= dwKeyEnd; ++i ) { m_dequeKey.push_back( i ); } } template CObjKeyGenerator::~CObjKeyGenerator(VOID) { m_dequeKey.clear(); } template KeyType CObjKeyGenerator::GetKey() { KeyType key; if (IsEmpty() == TRUE) { return NULL; } // m_cs.Lock(); key = *m_dequeKey.begin(); m_dequeKey.pop_front(); // m_cs.Unlock(); return key; } template BOOL CObjKeyGenerator::ReserveKey(KeyType key) { BOOL bReserved = FALSE; if (IsEmpty() == TRUE) { return FALSE; } // m_cs.Lock(); deque::iterator itr = m_dequeKey.begin(); while (itr != m_dequeKey.end()) { if ( *itr == key ) { bReserved = TRUE; m_dequeKey.erase(itr); break; } itr++; } return bReserved; } template BOOL CObjKeyGenerator::IsEmpty() { return m_dequeKey.empty() == TRUE; } template BOOL CObjKeyGenerator::IsExistKey(KeyType dwKey) { deque::iterator itr = m_dequeKey.begin(); for (; itr != m_dequeKey.end(); itr++) { if (*itr == dwKey) { return TRUE; } } return FALSE; } template VOID CObjKeyGenerator::RestoreKey(KeyType key) { // CSyncCriticalSection sync_cs(m_cs); m_dequeKey.push_back(key); } template size_t CObjKeyGenerator::GetSize() { // CSyncCriticalSection sync_cs(m_cs); return m_dequeKey.size(); } }// End of Namespace Util #endif //__OBJ_KEY_GENERATOR_H__