libtcspc C++ API
Streaming TCSPC and time tag data processing
Loading...
Searching...
No Matches
record_last.hpp
1/*
2 * This file is part of libtcspc
3 * Copyright 2019-2026 Board of Regents of the University of Wisconsin System
4 * SPDX-License-Identifier: MIT
5 */
6
7#pragma once
8
9#include "context.hpp"
10#include "introspect.hpp"
11#include "processor.hpp"
12
13#include <functional>
14#include <optional>
15#include <type_traits>
16#include <utility>
17
18namespace tcspc {
19
25template <typename Event> class record_last_accessor {
26 std::function<std::optional<Event>()> get_fn;
27
28 public:
30 template <typename F>
31 explicit record_last_accessor(F get_func) : get_fn(get_func) {}
32
34 auto get() -> std::optional<Event> { return get_fn(); }
35};
36
37namespace internal {
38
39template <typename Event, typename Downstream> class record_last {
40 std::optional<Event> last;
41
42 Downstream downstream;
43
44 // Cold data after downstream.
45 access_tracker<record_last_accessor<Event>> trk;
46
47 public:
48 explicit record_last(access_tracker<record_last_accessor<Event>> &&tracker,
49 Downstream downstream)
50 : downstream(std::move(downstream)), trk(std::move(tracker)) {
51 trk.register_accessor_factory([](auto &tracker) {
52 auto *self =
53 LIBTCSPC_OBJECT_FROM_TRACKER(record_last, trk, tracker);
54 return record_last_accessor<Event>([self] { return self->last; });
55 });
56 }
57
58 [[nodiscard]] auto introspect_node() const -> processor_info {
59 return processor_info(this, "record_last");
60 }
61
62 [[nodiscard]] auto introspect_graph() const -> processor_graph {
63 return downstream.introspect_graph().push_entry_point(this);
64 }
65
66 template <typename E>
67 requires handler_for<Downstream, std::remove_cvref_t<E>>
68 void handle(E &&event) {
69 if constexpr (std::is_same_v<std::remove_cvref_t<E>, Event>)
70 last = event; // Retain a copy.
71 downstream.handle(std::forward<E>(event));
72 }
73
74 void flush() { downstream.flush(); }
75};
76
77} // namespace internal
78
112template <typename Event, typename Downstream>
114 Downstream downstream) {
115 return internal::record_last<Event, Downstream>(std::move(tracker),
116 std::move(downstream));
117}
118
119} // namespace tcspc
Tracker that mediates access to objects via a tcspc::context.
Definition context.hpp:39
Accessor for tcspc::record_last() processor data.
Definition record_last.hpp:25
auto get() -> std::optional< Event >
Return the last event observed, or empty if none was.
Definition record_last.hpp:34
#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 record_last(access_tracker< record_last_accessor< Event > > &&tracker, Downstream downstream)
Create a processor that remembers the last event of a given type.
Definition record_last.hpp:113
libtcspc namespace.
Definition acquire.hpp:30