paulxstretch/deps/clap-juce-extensions/clap-libs/clap-helpers/include/clap/helpers/reducing-param-queue.hxx
essej 7fe70ed89b 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"
2022-06-14 21:35:21 -04:00

72 lines
1.7 KiB
C++

#pragma once
#include <cassert>
#include "reducing-param-queue.hh"
namespace clap { namespace helpers {
template <typename K, typename V>
ReducingParamQueue<K, V>::ReducingParamQueue() {
reset();
}
template <typename K, typename V>
void ReducingParamQueue<K, V>::reset() {
for (auto &q : _queues)
q.clear();
_free = &_queues[0];
_producer = &_queues[1];
_consumer = nullptr;
}
template <typename K, typename V>
void ReducingParamQueue<K, V>::setCapacity(size_t capacity) {
for (auto &q : _queues)
q.reserve(2 * capacity);
}
template <typename K, typename V>
void ReducingParamQueue<K, V>::set(const key_type &key, const value_type &value) {
_producer.load()->insert_or_assign(key, value);
}
template <typename K, typename V>
void ReducingParamQueue<K, V>::setOrUpdate(const key_type &key, const value_type &value) {
auto prod = _producer.load();
auto res = prod->insert({key, value});
if (!res.second && res.first != prod->end())
res.first->second.update(value);
}
template <typename K, typename V>
void ReducingParamQueue<K, V>::producerDone() {
if (_consumer)
return;
auto tmp = _producer.load();
_producer = _free.load();
_free = nullptr;
_consumer = tmp;
assert(_producer);
}
template <typename K, typename V>
void ReducingParamQueue<K, V>::consume(const consumer_type &consumer) {
assert(consumer);
if (!_consumer)
return;
for (auto &x : *_consumer)
consumer(x.first, x.second);
_consumer.load()->clear();
if (_free)
return;
_free = _consumer.load();
_consumer = nullptr;
}
}} // namespace clap::helpers