Loading...
Searching...
No Matches
Monitor.hpp
1#pragma once
2#include <ossia/detail/triple_buffer.hpp>
3
4#include <score_lib_process_export.h>
5
6#include <atomic>
7#include <cstdint>
8#include <utility>
9
10namespace Process
11{
26struct SCORE_LIB_PROCESS_EXPORT Monitor
27{
28 Monitor() noexcept;
29 Monitor(const Monitor&) = delete;
30 Monitor& operator=(const Monitor&) = delete;
31 virtual ~Monitor();
32
35 std::atomic<int> viewers{0};
36
37 bool watched() const noexcept
38 {
39 return viewers.load(std::memory_order_relaxed) > 0;
40 }
41};
42
48SCORE_LIB_PROCESS_EXPORT uint64_t monitorFrame() noexcept;
49
57template <typename T>
59{
60public:
61 LatestValue() = default;
62 explicit LatestValue(const T& init)
63 : m_buffer{init}
64 , m_last{init}
65 {
66 }
67 LatestValue(const LatestValue&) = delete;
68 LatestValue& operator=(const LatestValue&) = delete;
69
71 template <typename U>
72 void produce(U&& v)
73 {
74 m_buffer.produce(std::forward<U>(v));
75 }
76
79 bool consume(T& out)
80 {
81 const uint64_t frame = monitorFrame();
82 if(frame == 0)
83 return m_buffer.consume(out);
84 if(frame != m_frame)
85 {
86 m_frame = frame;
87 m_fresh = m_buffer.consume(m_last);
88 }
89 if(m_fresh)
90 out = m_last;
91 return m_fresh;
92 }
93
94private:
95 ossia::triple_buffer<T> m_buffer;
96 T m_last{};
97 uint64_t m_frame{};
98 bool m_fresh{};
99};
100}
Definition Monitor.hpp:59
bool consume(T &out)
Definition Monitor.hpp:79
void produce(U &&v)
Writer.
Definition Monitor.hpp:72
Base classes and tools to implement processes and layers.
Definition JSONVisitor.hpp:1115
uint64_t monitorFrame() noexcept
Definition MonitorLink.cpp:27
Definition Monitor.hpp:27