Files
2022-10-26 12:25:11 +08:00

157 lines
2.9 KiB
C++

#ifndef __OBJ_KEY_GENERATOR_H__
#define __OBJ_KEY_GENERATOR_H__
//=======================================================================================================================
/// 키 생성 클래스
/**
@history
- 2006.8.9 : template class화, cs제거 (taiyo)
*/
//=======================================================================================================================
#pragma once
#include <deque>
//#include "CriticalSection.h"
using namespace std;
namespace util {
/**
Object들에 Key를 발급하고 회수하는 매니저
*/
template<typename KeyType = DWORD>
class CObjKeyGenerator
{
private:
// CCriticalSection m_cs;
deque<KeyType> 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<typename KeyType>
CObjKeyGenerator<KeyType>::CObjKeyGenerator( KeyType dwKeyStart, KeyType dwKeyEnd )
{
Create(dwKeyStart, dwKeyEnd);
}
template<typename KeyType>
VOID CObjKeyGenerator<KeyType>::Create( KeyType dwKeyStart, KeyType dwKeyEnd )
{
for( KeyType i = dwKeyStart; i <= dwKeyEnd; ++i )
{
m_dequeKey.push_back( i );
}
}
template<typename KeyType>
CObjKeyGenerator<KeyType>::~CObjKeyGenerator(VOID)
{
m_dequeKey.clear();
}
template<typename KeyType>
KeyType CObjKeyGenerator<KeyType>::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<typename KeyType>
BOOL CObjKeyGenerator<KeyType>::ReserveKey(KeyType key)
{
BOOL bReserved = FALSE;
if (IsEmpty() == TRUE) {
return FALSE;
}
// m_cs.Lock();
deque<KeyType>::iterator itr = m_dequeKey.begin();
while (itr != m_dequeKey.end())
{
if ( *itr == key )
{
bReserved = TRUE;
m_dequeKey.erase(itr);
break;
}
itr++;
}
return bReserved;
}
template<typename KeyType>
BOOL CObjKeyGenerator<KeyType>::IsEmpty()
{
return m_dequeKey.empty() == TRUE;
}
template<typename KeyType>
BOOL CObjKeyGenerator<KeyType>::IsExistKey(KeyType dwKey)
{
deque<KeyType>::iterator itr = m_dequeKey.begin();
for (; itr != m_dequeKey.end(); itr++)
{
if (*itr == dwKey)
{
return TRUE;
}
}
return FALSE;
}
template<typename KeyType>
VOID CObjKeyGenerator<KeyType>::RestoreKey(KeyType key)
{
// CSyncCriticalSection sync_cs(m_cs);
m_dequeKey.push_back(key);
}
template<typename KeyType>
size_t CObjKeyGenerator<KeyType>::GetSize()
{
// CSyncCriticalSection sync_cs(m_cs);
return m_dequeKey.size();
}
}// End of Namespace Util
#endif //__OBJ_KEY_GENERATOR_H__