9#include "arg_wrappers.hpp"
12#include "int_types.hpp"
13#include "introspect.hpp"
14#include "processor.hpp"
26template <
typename TickEvent,
typename FireEvent,
typename ResetEvent,
27 bool FireAfterTick,
typename Downstream>
28 requires processor<Downstream, TickEvent, FireEvent>
35 Downstream downstream;
37 template <
typename Abstime>
void pre_tick(Abstime abstime) {
38 if constexpr (!FireAfterTick) {
40 downstream.handle(FireEvent{abstime});
44 template <
typename Abstime>
void post_tick(Abstime abstime) {
47 if constexpr (FireAfterTick) {
49 downstream.handle(FireEvent{abstime});
57 explicit count_up_to(arg::threshold<u64> threshold, arg::limit<u64> limit,
58 arg::initial_count<u64> initial_count,
59 Downstream downstream)
60 : count(initial_count.value), init(initial_count.value),
61 thresh(threshold.value), lmt(limit.value),
62 downstream(std::move(downstream)) {
64 throw std::invalid_argument(
65 "count_up_to limit must be greater than initial_count");
68 [[nodiscard]]
auto introspect_node() const -> processor_info {
69 return processor_info(
this,
"count_up_to");
72 [[nodiscard]]
auto introspect_graph() const -> processor_graph {
73 return downstream.introspect_graph().push_entry_point(
this);
76 void handle(TickEvent
const &event) {
77 pre_tick(event.abstime);
78 downstream.handle(event);
79 post_tick(event.abstime);
82 void handle(TickEvent &&event) {
83 auto const abstime =
event.abstime;
85 downstream.handle(std::move(event));
90 requires handler_for<Downstream, std::remove_cvref_t<E>>
91 void handle(E &&event) {
92 if constexpr (std::is_convertible_v<std::remove_cvref_t<E>,
96 downstream.handle(std::forward<E>(event));
99 void flush() { downstream.flush(); }
175template <
typename TickEvent,
typename FireEvent,
typename ResetEvent,
176 bool FireAfterTick,
typename Downstream>
179 Downstream downstream) {
180 return internal::count_up_to<TickEvent, FireEvent, ResetEvent,
181 FireAfterTick, Downstream>(
182 threshold, limit, initial_count, std::move(downstream));
196template <
typename TickEvent,
typename FireEvent,
typename ResetEvent,
197 bool FireAfterTick,
typename Downstream>
200 Downstream downstream) {
203 throw std::invalid_argument(
204 "count_down_to limit must be less than initial_count");
216 return internal::count_up_to<TickEvent, FireEvent, ResetEvent,
217 FireAfterTick, Downstream>(
218 threshold, limit, initial_count, std::move(downstream));
226class count_accessor {
227 std::function<
u64()> count_fn;
231 template <
typename Func>
232 explicit count_accessor(Func count_func) : count_fn(count_func) {}
242template <
typename Event,
typename Downstream>
243 requires processor<Downstream, Event>
247 Downstream downstream;
250 access_tracker<count_accessor> trk;
253 explicit count(access_tracker<count_accessor> &&tracker,
254 Downstream downstream)
255 : downstream(std::move(downstream)), trk(std::move(tracker)) {
256 trk.register_accessor_factory([](
auto &tracker) {
258 return count_accessor([self] {
return self->ct; });
262 [[nodiscard]]
auto introspect_node() const -> processor_info {
263 return processor_info(
this,
"count");
266 [[nodiscard]]
auto introspect_graph() const -> processor_graph {
267 return downstream.introspect_graph().push_entry_point(
this);
270 template <
typename E>
271 requires handler_for<Downstream, std::remove_cvref_t<E>>
272 void handle(E &&event) {
273 if constexpr (std::is_convertible_v<std::remove_cvref_t<E>, Event>)
275 downstream.handle(std::forward<E>(event));
278 void flush() { downstream.flush(); }
311template <
typename Event,
typename Downstream>
313 return internal::count<Event, Downstream>(std::move(tracker),
314 std::move(downstream));
Tracker that mediates access to objects via a tcspc::context.
Definition context.hpp:39
auto count() -> u64
Return the count value of the associated processor.
Definition count.hpp:237
#define LIBTCSPC_OBJECT_FROM_TRACKER(obj_type, tracker_field_name, tracker)
Recover the object address from a tcspc::access_tracker embedded in the object.
Definition context.hpp:255
auto count(access_tracker< count_accessor > &&tracker, Downstream downstream)
Create a processor that counts events of a given type.
Definition count.hpp:312
auto count_down_to(arg::threshold< u64 > threshold, arg::limit< u64 > limit, arg::initial_count< u64 > initial_count, Downstream downstream)
Like tcspc::count_up_to(), but decrement the count on each tick event.
Definition count.hpp:198
auto count_up_to(arg::threshold< u64 > threshold, arg::limit< u64 > limit, arg::initial_count< u64 > initial_count, Downstream downstream)
Create a processor that counts a specific event and emits an event when the count reaches a threshold...
Definition count.hpp:177
std::uint64_t u64
Short name for uint64_t.
Definition int_types.hpp:33
libtcspc namespace.
Definition acquire.hpp:30
Function argument wrapper for initial count parameter.
Definition arg_wrappers.hpp:167
T value
The argument value.
Definition arg_wrappers.hpp:169
Function argument wrapper for limit parameter.
Definition arg_wrappers.hpp:207
T value
The argument value.
Definition arg_wrappers.hpp:209
Function argument wrapper for threshold parameter.
Definition arg_wrappers.hpp:397
T value
The argument value.
Definition arg_wrappers.hpp:399