8#include <condition_variable>
10#include <unordered_map>
30 FrameData(T value, units::second_t timestamp): value{value}, ts{timestamp} {};
44 std::optional<T> value;
66 std::unique_lock<std::mutex> lock(
dataLock);
67 this->
data = std::optional<FrameData<T>>{
FrameData{value, timestamp}};
68 this->
cv.notify_all();
71 std::condition_variable&
cv;
75 std::optional<FrameData<T>>
data{std::nullopt};
97 Frame(T value, units::second_t timestamp): value{value}, ts{timestamp} {};
104 void Update(T value, units::second_t timestamp) {
105 std::unique_lock<std::mutex> lock(frameLock);
107 this->ts = timestamp;
109 fl->UpdateValue(value, ts);
111 for (
const auto& [key, cb] : callbacks) {
120 std::unique_lock<std::mutex> lock(frameLock);
128 std::unique_lock<std::mutex> lock(frameLock);
137 std::unique_lock<std::mutex> lock(frameLock);
159 callbacks[key++] = callback;
169 return callbacks.erase(key);
177 std::unique_lock<std::mutex> lock(frameLock);
178 listeners.insert(listener);
187 std::unique_lock<std::mutex> lock(frameLock);
188 listeners.erase(listener);
195 std::mutex frameLock;
196 std::set<FrameListener<T>*> listeners;
197 std::unordered_map<uint32_t, std::function<void(
FrameData<T>)>> callbacks;
242template<
typename...T>
244 constexpr auto sec = std::chrono::seconds(1);
245 std::condition_variable cv;
248 auto listeners = std::make_tuple(std::make_pair(
FrameListener<T>(cv, dataLock), &frames)...);
250 std::unique_lock<std::mutex> lock(dataLock);
253 if (!cv.wait_for(lock, timeout.to<
double>() * sec, [&]{
254 return std::apply([](std::pair<FrameListener<T>, Frame<T>*>&... i) { return ((i.first.data != std::nullopt) && ...); }, listeners);
258 std::apply([](std::pair<
FrameListener<T>,
Frame<T>*>&... i) {(i.second->RemoveListener(&i.first), ...);}, listeners);
261 std::apply([](std::pair<
FrameListener<T>,
Frame<T>*>&... i) {(i.second->RemoveListener(&i.first), ...);}, listeners);
263 return std::apply([](std::pair<FrameListener<T>, Frame<T>*>&... i) {
return std::make_tuple(*(i.first.data)...); }, listeners);
273template<
typename...T>
278 return *std::max_element(ilist.begin(), ilist.end());
units::second_t GetTimestamp()
Definition: Frame.h:42
FrameData(T value, units::second_t timestamp)
Definition: Frame.h:30
T GetValue()
Definition: Frame.h:35
FrameListener(std::condition_variable &cv, std::mutex &dataLock)
Definition: Frame.h:59
std::mutex & dataLock
Definition: Frame.h:73
std::optional< FrameData< T > > data
Definition: Frame.h:75
std::condition_variable & cv
Definition: Frame.h:71
void UpdateValue(T value, units::second_t timestamp)
Definition: Frame.h:65
void Update(T value, units::second_t timestamp)
Definition: Frame.h:104
void RemoveListener(FrameListener< T > *listener)
Definition: Frame.h:186
uint32_t AddCallback(std::function< void(FrameData< T >)> callback)
Definition: Frame.h:158
FrameData< T > GetFrameData()
Definition: Frame.h:119
T GetValue()
Definition: Frame.h:127
units::second_t GetTimestamp()
Definition: Frame.h:136
Frame(T value, units::second_t timestamp)
Definition: Frame.h:97
void AddListener(FrameListener< T > *listener)
Definition: Frame.h:176
bool RemoveCallback(uint32_t key)
Definition: Frame.h:168
std::optional< std::tuple< FrameData< T >... > > WaitForFrames(units::second_t timeout, Frame< T > &... frames)
Definition: Frame.h:243
units::second_t MaxTimestamp(std::tuple< FrameData< T >... > frameData)
Definition: Frame.h:274