Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ofxDelayLineQueue.h
#pragma once
#include <chrono>
#include <deque>
template <typename T>
class ofxDelayLineQueue
{
public:
void update()
{
current_timestamp = std::chrono::system_clock::now();
}
void push(const T& data)
{
buffer.emplace_back(current_timestamp, data);
}
bool pop(T& out)
{
if (buffer.empty()) return false;
if (buffer.front().timestamp + delay_time < current_timestamp)
{
out = buffer.front().data;
buffer.pop_front();
return true;
}
return false;
}
void setDelayTime(std::chrono::seconds sec) { delay_time = sec; }
std::chrono::seconds getDelayTime() const { return delay_time; }
private:
struct Data {
std::chrono::system_clock::time_point timestamp;
T data;
Data(std::chrono::system_clock::time_point timestamp, const T& data)
: timestamp(timestamp)
, data(data)
{}
};
std::chrono::seconds delay_time;
std::chrono::system_clock::time_point current_timestamp;
std::deque<Data> buffer;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment