libtcspc C++ API
Streaming TCSPC and time tag data processing
Loading...
Searching...
No Matches
gate.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 "processor.hpp"
13#include "type_list.hpp"
14
15#include <type_traits>
16#include <utility>
17
18namespace tcspc {
19
20namespace internal {
21
22template <typename GatedEventList, typename OpenEvent, typename CloseEvent,
23 typename Downstream>
24class gate {
25 static_assert(type_list_like<GatedEventList>);
26 // We do not require Downstream to handle all of GatedEventList.
27 static_assert(handler_for<Downstream, OpenEvent, CloseEvent>);
28
29 bool open;
30
31 Downstream downstream;
32
33 public:
34 explicit gate(arg::initially_open<bool> initially_open,
35 Downstream downstream)
36 : open(initially_open.value), downstream(std::move(downstream)) {}
37
38 [[nodiscard]] auto introspect_node() const -> processor_info {
39 return processor_info(this, "gate");
40 }
41
42 [[nodiscard]] auto introspect_graph() const -> processor_graph {
43 return downstream.introspect_graph().push_entry_point(this);
44 }
45
46 template <typename E>
47 requires handler_for<Downstream, std::remove_cvref_t<E>>
48 void handle(E &&event) {
49 if constexpr (std::is_convertible_v<std::remove_cvref_t<E>,
50 OpenEvent>) {
51 open = true;
52 downstream.handle(std::forward<E>(event));
53 } else if constexpr (std::is_convertible_v<std::remove_cvref_t<E>,
54 CloseEvent>) {
55 open = false;
56 downstream.handle(std::forward<E>(event));
57 } else if constexpr (convertible_to_type_list_member<
58 std::remove_cvref_t<E>, GatedEventList>) {
59 if (open)
60 downstream.handle(std::forward<E>(event));
61 } else {
62 downstream.handle(std::forward<E>(event));
63 }
64 }
65
66 void flush() { downstream.flush(); }
67};
68
69} // namespace internal
70
109template <typename GatedEventList, typename OpenEvent, typename CloseEvent,
110 typename Downstream>
111auto gate(arg::initially_open<bool> initially_open, Downstream downstream) {
112 return internal::gate<GatedEventList, OpenEvent, CloseEvent, Downstream>(
113 initially_open, std::move(downstream));
114}
115
116} // namespace tcspc
auto gate(arg::initially_open< bool > initially_open, Downstream downstream)
Create a processor that gates events depending on current state.
Definition gate.hpp:111
libtcspc namespace.
Definition acquire.hpp:29
Function argument wrapper for initially open parameter.
Definition arg_wrappers.hpp:157