Last active
April 3, 2016 14:38
-
-
Save satoruhiga/f850cd2e340dd7cb378e to your computer and use it in GitHub Desktop.
ofxDelayLineQueue.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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