#ifndef F0270054_CDC4_4A91_B09F_73A4C75D9356 #define F0270054_CDC4_4A91_B09F_73A4C75D9356 /* ******************************************************************************* * INCLUDE ******************************************************************************* */ #include #include namespace Util { /* ******************************************************************************* * CLASS DECLARE ******************************************************************************* */ template class HandlesManager { public: using HandleCollection = std::set; 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 _freeHandles; Poco::FastMutex _lock; }; } #endif