git subrepo clone (merge) https://github.com/free-audio/clap-helpers.git deps/clap-juce-extensions/clap-libs/clap-helpers
subrepo: subdir: "deps/clap-juce-extensions/clap-libs/clap-helpers" merged: "2bb43c187" upstream: origin: "https://github.com/free-audio/clap-helpers.git" branch: "main" commit: "2bb43c187" git-subrepo: version: "0.4.3" origin: "https://github.com/ingydotnet/git-subrepo.git" commit: "2f68596"
This commit is contained in:
71
deps/clap-juce-extensions/clap-libs/clap-helpers/include/clap/helpers/param-queue.hh
vendored
Normal file
71
deps/clap-juce-extensions/clap-libs/clap-helpers/include/clap/helpers/param-queue.hh
vendored
Normal file
@ -0,0 +1,71 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
|
||||
namespace clap { namespace helpers {
|
||||
template <typename T, size_t CAPACITY>
|
||||
class ParamQueue {
|
||||
public:
|
||||
using value_type = T;
|
||||
|
||||
ParamQueue() = default;
|
||||
|
||||
void push(const T &value) {
|
||||
while (!tryPush(value))
|
||||
;
|
||||
}
|
||||
|
||||
bool tryPush(const T &value) {
|
||||
int w = _writeOffset; // write element
|
||||
int wn = (w + 1) % CAPACITY; // next write element
|
||||
|
||||
if (wn == _readOffset)
|
||||
return false;
|
||||
|
||||
_data[w] = value;
|
||||
_writeOffset = wn;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool tryPeek(T &value) {
|
||||
int r = _readOffset;
|
||||
if (r == _writeOffset)
|
||||
return false; // empty
|
||||
|
||||
value = _data[r];
|
||||
return true;
|
||||
}
|
||||
|
||||
void consume() {
|
||||
int r = _readOffset;
|
||||
if (r == _writeOffset) {
|
||||
assert(false && "consume should not have been called");
|
||||
return; // empty
|
||||
}
|
||||
|
||||
int rn = (r + 1) % CAPACITY;
|
||||
_readOffset = rn;
|
||||
}
|
||||
|
||||
bool tryPop(T &value) {
|
||||
if (!tryPeek(value))
|
||||
return false;
|
||||
|
||||
consume();
|
||||
return true;
|
||||
}
|
||||
|
||||
void reset() {
|
||||
_writeOffset = 0;
|
||||
_readOffset = 0;
|
||||
}
|
||||
|
||||
private:
|
||||
std::array<T, CAPACITY> _data;
|
||||
std::atomic<int> _writeOffset = {0};
|
||||
std::atomic<int> _readOffset = {0};
|
||||
};
|
||||
}} // namespace clap::helpers
|
Reference in New Issue
Block a user