整理代码
This commit is contained in:
commit
c7b4e178ec
364 changed files with 59691 additions and 0 deletions
67
util/HandlesManager.h
Normal file
67
util/HandlesManager.h
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
#ifndef F0270054_CDC4_4A91_B09F_73A4C75D9356
|
||||
#define F0270054_CDC4_4A91_B09F_73A4C75D9356
|
||||
/*
|
||||
*******************************************************************************
|
||||
* INCLUDE
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <Poco/Mutex.h>
|
||||
#include <set>
|
||||
|
||||
namespace Util
|
||||
{
|
||||
/*
|
||||
*******************************************************************************
|
||||
* CLASS DECLARE
|
||||
*******************************************************************************
|
||||
*/
|
||||
template <typename T> class HandlesManager
|
||||
{
|
||||
public:
|
||||
using HandleCollection = std::set<T>;
|
||||
|
||||
public:
|
||||
HandlesManager(unsigned count): HandlesManager(0, count)
|
||||
{
|
||||
}
|
||||
|
||||
HandlesManager(unsigned begin, unsigned count)
|
||||
{
|
||||
for (T h = begin; h < (begin + count); h++)
|
||||
_freeHandles.insert(h);
|
||||
}
|
||||
|
||||
HandlesManager(HandleCollection& c)
|
||||
{
|
||||
_freeHandles.insert(c);
|
||||
}
|
||||
|
||||
T allocHandle()
|
||||
{
|
||||
Poco::FastMutex::ScopedLock lock(_lock);
|
||||
|
||||
if (_freeHandles.size() == 0)
|
||||
throw Poco::NotFoundException("Collection is empty", -ENODATA);
|
||||
|
||||
T h = *_freeHandles.begin();
|
||||
_freeHandles.erase(h);
|
||||
|
||||
return h;
|
||||
}
|
||||
|
||||
void freeHandle(T handle)
|
||||
{
|
||||
Poco::FastMutex::ScopedLock lock(_lock);
|
||||
|
||||
if (_freeHandles.count(handle) != 0)
|
||||
throw Poco::NotFoundException("Double free", -EINVAL);
|
||||
|
||||
_freeHandles.insert(handle);
|
||||
}
|
||||
|
||||
private:
|
||||
std::set<T> _freeHandles;
|
||||
Poco::FastMutex _lock;
|
||||
};
|
||||
}
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue