67 lines
1.6 KiB
C++
67 lines
1.6 KiB
C++
#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
|