libtcspc C++ API
Streaming TCSPC and time tag data processing
Loading...
Searching...
No Matches
record_abstime_range.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 "event.hpp"
11#include "introspect.hpp"
12#include "numeric_traits.hpp"
13#include "processor.hpp"
14
15#include <functional>
16#include <optional>
17#include <type_traits>
18#include <utility>
19
20namespace tcspc {
21
27template <typename Abstime> class record_abstime_range_accessor {
28 std::function<std::optional<Abstime>()> min_fn;
29 std::function<std::optional<Abstime>()> max_fn;
30
31 public:
33 template <typename FMin, typename FMax>
34 explicit record_abstime_range_accessor(FMin min_func, FMax max_func)
35 : min_fn(min_func), max_fn(max_func) {}
36
41 auto min() -> std::optional<Abstime> { return min_fn(); }
42
47 auto max() -> std::optional<Abstime> { return max_fn(); }
48};
49
50namespace internal {
51
52template <typename NumericTraits, typename Downstream>
54 using abstime_type = typename NumericTraits::abstime_type;
55
56 std::optional<abstime_type> mn;
57 std::optional<abstime_type> mx;
58
59 Downstream downstream;
60
61 // Cold data after downstream.
62 access_tracker<record_abstime_range_accessor<abstime_type>> trk;
63
64 public:
65 explicit record_abstime_range(
66 access_tracker<record_abstime_range_accessor<abstime_type>> &&tracker,
67 Downstream downstream)
68 : downstream(std::move(downstream)), trk(std::move(tracker)) {
69 trk.register_accessor_factory([](auto &tracker) {
70 auto *self = LIBTCSPC_OBJECT_FROM_TRACKER(record_abstime_range,
71 trk, tracker);
72 return record_abstime_range_accessor<abstime_type>(
73 [self] { return self->mn; }, [self] { return self->mx; });
74 });
75 }
76
77 [[nodiscard]] auto introspect_node() const -> processor_info {
78 return processor_info(this, "record_abstime_range");
79 }
80
81 [[nodiscard]] auto introspect_graph() const -> processor_graph {
82 return downstream.introspect_graph().push_entry_point(this);
83 }
84
85 template <typename E>
86 requires handler_for<Downstream, std::remove_cvref_t<E>>
87 void handle(E &&event) {
88 if constexpr (abstime_stamped<std::remove_cvref_t<E>>) {
89 static_assert(
90 std::is_same_v<decltype(event.abstime), abstime_type>);
91 if (not mn || event.abstime < *mn)
92 mn = event.abstime;
93 if (not mx || event.abstime > *mx)
94 mx = event.abstime;
95 }
96 downstream.handle(std::forward<E>(event));
97 }
98
99 void flush() { downstream.flush(); }
100};
101
102} // namespace internal
103
133template <typename NumericTraits = default_numeric_traits, typename Downstream>
135 typename NumericTraits::abstime_type>> &&tracker,
136 Downstream downstream) {
137 return internal::record_abstime_range<NumericTraits, Downstream>(
138 std::move(tracker), std::move(downstream));
139}
140
141} // namespace tcspc
Tracker that mediates access to objects via a tcspc::context.
Definition context.hpp:39
Accessor for tcspc::record_abstime_range() processor data.
Definition record_abstime_range.hpp:27
auto max() -> std::optional< Abstime >
Return the maximum abstime observed, or empty if no abstime-stamped event was observed.
Definition record_abstime_range.hpp:47
auto min() -> std::optional< Abstime >
Return the minimum abstime observed, or empty if no abstime-stamped event was observed.
Definition record_abstime_range.hpp:41
#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_abstime_range(access_tracker< record_abstime_range_accessor< typename NumericTraits::abstime_type > > &&tracker, Downstream downstream)
Create a processor that records the range of abstime observed.
Definition record_abstime_range.hpp:134
libtcspc namespace.
Definition acquire.hpp:30