libtcspc C++ API
Streaming TCSPC and time tag data processing
Loading...
Searching...
No Matches
match.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 "arg_wrappers.hpp"
10#include "common.hpp"
11#include "introspect.hpp"
12#include "numeric_traits.hpp"
13#include "processor.hpp"
14
15#include <concepts>
16#include <type_traits>
17#include <utility>
18
19namespace tcspc {
20
30template <typename T, typename Event>
31concept matcher_for =
32 std::move_constructible<T> && requires(T const &m, Event const &e) {
33 { m(e) } -> std::same_as<bool>;
34 };
35
36namespace internal {
37
38template <typename Event, typename OutEvent, typename Matcher,
39 bool PassMatched, typename Downstream>
42class match {
43 Matcher matcher;
44 Downstream downstream;
45
46 public:
47 explicit match(Matcher matcher, Downstream downstream)
48 : matcher(std::move(matcher)), downstream(std::move(downstream)) {}
49
50 [[nodiscard]] auto introspect_node() const -> processor_info {
51 return processor_info(this, "match");
52 }
53
54 [[nodiscard]] auto introspect_graph() const -> processor_graph {
55 return downstream.introspect_graph().push_entry_point(this);
56 }
57
58 template <typename E>
59 requires(std::convertible_to<std::remove_cvref_t<E>, Event> and
60 handler_for<Downstream, std::remove_cvref_t<E>>)
61 void handle(E &&event) {
62 auto const abstime = event.abstime;
63 bool const matched = matcher(event);
64 bool const pass = PassMatched ? true : not matched;
65 if (pass)
66 downstream.handle(std::forward<E>(event));
67 if (matched)
68 downstream.handle(OutEvent{abstime});
69 }
70
71 template <typename E>
72 requires(not std::convertible_to<std::remove_cvref_t<E>, Event> and
73 handler_for<Downstream, std::remove_cvref_t<E>>)
74 void handle(E &&event) {
75 downstream.handle(std::forward<E>(event));
76 }
77
78 void flush() { downstream.flush(); }
79};
80
81} // namespace internal
82
98template <typename Event, typename OutEvent, typename Matcher,
99 typename Downstream>
100auto match_and_consume(Matcher matcher, Downstream downstream) {
101 return internal::match<Event, OutEvent, Matcher, false, Downstream>(
102 std::move(matcher), std::move(downstream));
103}
104
139template <typename Event, typename OutEvent, typename Matcher,
140 typename Downstream>
141auto match(Matcher matcher, Downstream downstream) {
142 return internal::match<Event, OutEvent, Matcher, true, Downstream>(
143 std::move(matcher), std::move(downstream));
144}
145
152 public:
154 template <typename Event>
155 auto operator()(Event const & /* event */) const -> bool {
156 return true;
157 }
158};
159
166 public:
168 template <typename Event>
169 auto operator()(Event const & /* event */) const -> bool {
170 return false;
171 }
172};
173
183template <typename NumericTraits = default_numeric_traits>
185 NumericTraits::channel_type channel;
186
187 public:
193 : channel(channel.value) {}
194
196 template <typename Event>
197 auto operator()(Event const &event) const -> bool {
198 static_assert(std::is_same_v<decltype(event.channel),
199 typename NumericTraits::channel_type>);
200 return event.channel == channel;
201 }
202};
203
204} // namespace tcspc
Matcher that matches all events.
Definition match.hpp:151
auto operator()(Event const &) const -> bool
Implements matcher requirement; return true.
Definition match.hpp:155
channel_matcher(arg::channel< typename NumericTraits::channel_type > channel)
Construct with the given channel to match.
Definition match.hpp:191
auto operator()(Event const &event) const -> bool
Implements matcher requirement.
Definition match.hpp:197
Matcher that matches no event.
Definition match.hpp:165
auto operator()(Event const &) const -> bool
Implements matcher requirement; return false.
Definition match.hpp:169
Concept that is satisfied when T conforms to the libtcspc matcher interface for event type Event.
Definition match.hpp:31
Concept that is satisfied when a processor handles the given event types and flush.
Definition processor.hpp:119
auto match(Matcher matcher, Downstream downstream)
Create a processor that detects events matching a criterion.
Definition match.hpp:141
auto match_and_consume(Matcher matcher, Downstream downstream)
Like tcspc::match(), but do not pass through matched events.
Definition match.hpp:100
libtcspc namespace.
Definition acquire.hpp:30
Function argument wrapper for channel parameter.
Definition arg_wrappers.hpp:77