libtcspc C++ API
Streaming TCSPC and time tag data processing
Loading...
Searching...
No Matches
view_as_bytes.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 "bucket.hpp"
10#include "introspect.hpp"
11#include "processor.hpp"
12
13#include <cstddef>
14#include <span>
15#include <type_traits>
16#include <utility>
17
18namespace tcspc {
19
20namespace internal {
21
22template <typename Downstream> class view_as_bytes {
23 static_assert(processor<Downstream, bucket<std::byte>>);
24
25 Downstream downstream;
26
27 public:
28 explicit view_as_bytes(Downstream downstream)
29 : downstream(std::move(downstream)) {}
30
31 [[nodiscard]] auto introspect_node() const -> processor_info {
32 return processor_info(this, "view_as_bytes");
33 }
34
35 [[nodiscard]] auto introspect_graph() const -> processor_graph {
36 return downstream.introspect_graph().push_entry_point(this);
37 }
38
39 template <typename Event>
40 requires std::is_trivial_v<Event>
41 void handle(Event const &event) {
42 // It is safe to const_cast the bytes because we emit the bucket by
43 // const ref.
44 auto const_byte_span = std::as_bytes(std::span(&event, 1));
45 auto const b = ad_hoc_bucket(std::span<std::byte>(
46 const_cast<std::byte *>(const_byte_span.data()),
47 const_byte_span.size()));
48 downstream.handle(b);
49 }
50
51 template <typename T>
52 requires std::is_trivial_v<T>
53 void handle(bucket<T> const &event) {
54 // It is safe to const_cast the bytes because we emit the bucket by
55 // const ref.
56 auto const_byte_span = std::as_bytes(std::span(event));
57 auto const b = ad_hoc_bucket(std::span<std::byte>(
58 const_cast<std::byte *>(const_byte_span.data()),
59 const_byte_span.size()));
60 downstream.handle(b);
61 }
62
63 void flush() { downstream.flush(); }
64};
65
66} // namespace internal
67
90template <typename Downstream> auto view_as_bytes(Downstream downstream) {
91 return internal::view_as_bytes<Downstream>(std::move(downstream));
92}
93
94} // namespace tcspc
auto ad_hoc_bucket(std::span< T > s) -> bucket< T >
Create a tcspc::bucket referencing a span.
Definition bucket.hpp:489
auto view_as_bytes(Downstream downstream)
Create a processor that views events as byte spans.
Definition view_as_bytes.hpp:90
libtcspc namespace.
Definition acquire.hpp:29