This commit is contained in:
2023-06-05 11:49:38 -07:00
parent bbd442b191
commit 3ca30721b6
12 changed files with 405 additions and 210 deletions

View File

@ -18,6 +18,7 @@ set(
DAWN_SHARED_SOURCES
${D}/assert/assert.cpp
${D}/util/Xml.cpp
${D}/util/UsageLock.cpp
CACHE INTERNAL
${DAWN_CACHE_TARGET}

View File

@ -0,0 +1,27 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "util/UsageLock.hpp"
using namespace Dawn;
UsageLock::UsageLock() {
this->onEmpty = []() {};
this->onFirstLock = []() {};
}
usagelockid_t UsageLock::createLock() {
usagelockid_t lock = this->nextLock++;
this->locks.push_back(lock);
if(this->locks.size() == 1) this->onFirstLock();
return lock;
}
void UsageLock::removeLock(usagelockid_t lock) {
auto it = std::find(this->locks.begin(), this->locks.end(), lock);
if(it == this->locks.end()) return;
this->locks.erase(it);
if(this->locks.size() == 0) this->onEmpty();
}

View File

@ -0,0 +1,39 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "assert/assert.hpp"
namespace Dawn {
typedef uint32_t usagelockid_t;
class UsageLock {
protected:
usagelockid_t nextLock = 0;
std::vector<usagelockid_t> locks;
public:
std::function<void()> onEmpty;
std::function<void()> onFirstLock;
/**
* Construct a new usage lock object.
*/
UsageLock();
/**
* Creates a new lock.
*
* @return Lock created for this specific instance.
*/
usagelockid_t createLock();
/**
* Removes a lock.
*
* @param lock Lck to remove.
*/
void removeLock(usagelockid_t lock);
};
}