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 "data_types.hpp"
12#include "introspect.hpp"
13#include "processor.hpp"
14
15#include <type_traits>
16#include <utility>
17
18namespace tcspc {
19
20namespace internal {
21
22template <typename Event, typename OutEvent, typename Matcher,
23 bool PassMatched, typename Downstream>
24class match {
25 static_assert(processor<Downstream, Event, OutEvent>);
26
27 Matcher matcher;
28 Downstream downstream;
29
30 public:
31 explicit match(Matcher matcher, Downstream downstream)
32 : matcher(std::move(matcher)), downstream(std::move(downstream)) {}
33
34 [[nodiscard]] auto introspect_node() const -> processor_info {
35 return processor_info(this, "match");
36 }
37
38 [[nodiscard]] auto introspect_graph() const -> processor_graph {
39 return downstream.introspect_graph().push_entry_point(this);
40 }
41
42 template <typename E>
43 requires(std::convertible_to<std::remove_cvref_t<E>, Event> and
44 handler_for<Downstream, std::remove_cvref_t<E>>)
45 void handle(E &&event) {
46 auto const abstime = event.abstime;
47 bool const matched = matcher(event);
48 bool const pass = PassMatched ? true : not matched;
49 if (pass)
50 downstream.handle(std::forward<E>(event));
51 if (matched)
52 downstream.handle(OutEvent{abstime});
53 }
54
55 template <typename E>
56 requires(not std::convertible_to<std::remove_cvref_t<E>, Event> and
57 handler_for<Downstream, std::remove_cvref_t<E>>)
58 void handle(E &&event) {
59 downstream.handle(std::forward<E>(event));
60 }
61
62 void flush() { downstream.flush(); }
63};
64
65} // namespace internal
66
82template <typename Event, typename OutEvent, typename Matcher,
83 typename Downstream>
84auto match_replace(Matcher matcher, Downstream downstream) {
85 return internal::match<Event, OutEvent, Matcher, false, Downstream>(
86 std::move(matcher), std::move(downstream));
87}
88
123template <typename Event, typename OutEvent, typename Matcher,
124 typename Downstream>
125auto match(Matcher matcher, Downstream downstream) {
126 return internal::match<Event, OutEvent, Matcher, true, Downstream>(
127 std::move(matcher), std::move(downstream));
128}
129
136 public:
138 template <typename Event>
139 auto operator()(Event const & /* event */) const -> bool {
140 return true;
141 }
142};
143
150 public:
152 template <typename Event>
153 auto operator()(Event const & /* event */) const -> bool {
154 return false;
155 }
156};
157
167template <typename DataTypes = default_data_types> class channel_matcher {
168 typename DataTypes::channel_type channel;
169
170 public:
176 : channel(channel.value) {}
177
179 template <typename Event>
180 auto operator()(Event const &event) const -> bool {
181 static_assert(std::is_same_v<decltype(event.channel),
182 typename DataTypes::channel_type>);
183 return event.channel == channel;
184 }
185};
186
187} // namespace tcspc
Matcher that matches all events.
Definition match.hpp:135
auto operator()(Event const &) const -> bool
Implements matcher requirement; return true.
Definition match.hpp:139
channel_matcher(arg::channel< typename DataTypes::channel_type > channel)
Construct with the given channel to match.
Definition match.hpp:174
auto operator()(Event const &event) const -> bool
Implements matcher requirement.
Definition match.hpp:180
Matcher that matches no event.
Definition match.hpp:149
auto operator()(Event const &) const -> bool
Implements matcher requirement; return false.
Definition match.hpp:153
auto match(Matcher matcher, Downstream downstream)
Create a processor that detects events matching a criterion.
Definition match.hpp:125
auto match_replace(Matcher matcher, Downstream downstream)
Like tcspc::match(), but do not pass through matched events.
Definition match.hpp:84
libtcspc namespace.
Definition acquire.hpp:29
Function argument wrapper for channel parameter.
Definition arg_wrappers.hpp:77