📄 historymanager.h
字号:
/*-----------------------------------------------------------------------------------------
//模块名 :公共代码(工具)
//功能 :实现由Key为索引的历史记录管理
//版权 :新太科技股份有限公司
//创建日期 :2002-8-19
//作者 :Ada Li
//修改历史 :
//***********
//---------------------------------------------------------------------------------------*/
#ifndef INCLUDED_HISTORYMANAGER
#define INCLUDED_HISTORYMANAGER
#if defined(HAS_PRAGMA_ONCE)
#pragma PRAGMA_ONCE_DECLARE
#endif
/****
Note:
1、KeyType must comparable
2、ValueType must assignable(when take out history)
3、the record will be erease when you call take function
4、checkout will get the value but not erase it, and you can call checkin to update that record
****/
namespace stk
{
template <class KeyType, class ValueType>
class HistoryManager
{
public:
typedef std::map<KeyType, ValueType> StorageType;
typedef std::pair<bool, ValueType *> RefType;
public:
bool remember(const KeyType &key, const ValueType &value)
{
std::pair<StorageType::iterator, bool> ret;
CTScopeCS scope_cs(m_cs);
ret = m_storage.insert(std::make_pair(key, value));
return ret.second;
}
bool isInMemory(const KeyType &key)
{
StorageType::iterator it;
CTScopeCS scope_cs(m_cs);
it = m_storage.find(key);
return it != m_storage.end();
}
bool erase(const KeyType &key)
{
CTScopeCS scope_cs(m_cs);
return m_storage.erase(key) != 0;
}
bool take(const KeyType &key, ValueType &value)
{
StorageType::iterator it;
CTScopeCS scope_cs(m_cs);
it = m_storage.find(key);
if(it != m_storage.end())
{
//copy the value
value = it->second;
//and erase it
m_storage.erase(it);
return true;
}
else
return false;
}
RefType checkoutRef(const KeyType &key)
{
StorageType::iterator it;
m_cs.Lock();
// don't unlock! must lock until the user call checkin()
it = m_storage.find(key);
return std::make_pair(it != m_storage.end(), &(it->second));
}
void checkinRef()
{
// unlock the lock action by checkoutRef;
m_cs.Unlock();
}
bool checkout(const KeyType &key, ValueType &value)
{
StorageType::iterator it;
CTScopeCS scope_cs(m_cs);
it = m_storage.find(key);
if(it != m_storage.end())
{
//copy the value
value = it->second;
return true;
}
else
return false;
}
bool checkin(const KeyType &key, const ValueType &value)
{
StorageType::iterator it;
CTScopeCS scope_cs(m_cs);
it = m_storage.find(key);
if(it != m_storage.end())
{
//update the record
it->second = value;
return true;
}
else
return false;
}
void clear()
{
CTScopeCS scope_cs(m_cs);
m_storage.clear();
}
size_t size()
{
return m_storage.size();
}
void dump(TLog &log)
{
CTScopeCS scope_cs(m_cs);
StorageType::const_iterator it = m_storage.begin();
StorageType::const_iterator endit = m_storage.end();
for(int i=0; it != endit; ++i, ++it)
{
log.print("No.%d history\n", i);
log<<(it->second);
}
}
private:
CTCriticalSection m_cs;
StorageType m_storage;
};
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -