libtcspc C++ API
Streaming TCSPC and time tag data processing
Loading...
Searching...
No Matches
prepend_append.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 "common.hpp"
10#include "introspect.hpp"
11#include "processor.hpp"
12
13#include <type_traits>
14
15namespace tcspc {
16
17namespace internal {
18
19template <typename Event, typename Downstream> class prepend {
20 static_assert(processor<Downstream, Event>);
21
22 bool prepended = false;
23 Downstream downstream;
24
25 // Cold data after downstream:
26 Event evt;
27
28 public:
29 explicit prepend(Event event, Downstream downstream)
30 : downstream(std::move(downstream)), evt(std::move(event)) {}
31
32 [[nodiscard]] auto introspect_node() const -> processor_info {
33 return processor_info(this, "prepend");
34 }
35
36 [[nodiscard]] auto introspect_graph() const -> processor_graph {
37 return downstream.introspect_graph().push_entry_point(this);
38 }
39
40 template <typename AnyEvent>
41 requires handler_for<Downstream, std::remove_cvref_t<AnyEvent>>
42 void handle(AnyEvent &&event) {
43 if (not prepended) {
44 downstream.handle(std::move(evt));
45 prepended = true;
46 }
47 downstream.handle(std::forward<AnyEvent>(event));
48 }
49
50 void flush() { downstream.flush(); }
51};
52
53template <typename Event, typename Downstream> class append {
54 static_assert(processor<Downstream, Event>);
55
56 Downstream downstream;
57
58 // Cold data after downstream:
59 Event evt;
60
61 public:
62 explicit append(Event event, Downstream downstream)
63 : downstream(std::move(downstream)), evt(std::move(event)) {}
64
65 [[nodiscard]] auto introspect_node() const -> processor_info {
66 return processor_info(this, "append");
67 }
68
69 [[nodiscard]] auto introspect_graph() const -> processor_graph {
70 return downstream.introspect_graph().push_entry_point(this);
71 }
72
73 template <typename AnyEvent>
74 requires handler_for<Downstream, std::remove_cvref_t<AnyEvent>>
75 void handle(AnyEvent &&event) {
76 downstream.handle(std::forward<AnyEvent>(event));
77 }
78
79 void flush() {
80 downstream.handle(std::move(evt));
81 downstream.flush();
82 }
83};
84
85} // namespace internal
86
110template <typename Event, typename Downstream>
111auto prepend(Event event, Downstream downstream) {
112 return internal::prepend<Event, Downstream>(std::move(event),
113 std::move(downstream));
114}
115
142template <typename Event, typename Downstream>
143auto append(Event event, Downstream downstream) {
144 return internal::append<Event, Downstream>(std::move(event),
145 std::move(downstream));
146}
147
148} // namespace tcspc
auto append(Event event, Downstream downstream)
Create a processor that inserts an event at the end of the stream.
Definition prepend_append.hpp:143
auto prepend(Event event, Downstream downstream)
Create a processor that inserts an event at the beginning of the stream.
Definition prepend_append.hpp:111
libtcspc namespace.
Definition acquire.hpp:29