tess 0.4.0
Performance-first tile and path simulation substrate
Loading...
Searching...
No Matches
warning_sink.h
1#pragma once
2
3#include <tess/diagnostics/diagnostics.h>
4
5#include <array>
6#include <cstddef>
7#include <cstdint>
8#include <source_location>
9#include <string_view>
10
11namespace tess::diagnostics {
12
13#if TESS_DIAGNOSTICS_ENABLED
14
16enum class WarningCategory : std::uint8_t {
17 General,
18 Storage,
19 Path,
20 Topology,
21 Queued,
22 Scheduler,
23 Render,
24};
25
33struct Warning {
34 WarningCategory category = WarningCategory::General;
35 std::string_view message;
36 std::uint64_t detail = 0;
37 std::source_location where = std::source_location::current();
38};
39
41template <typename T>
42concept WarningSink = requires(T sink, const Warning& warning) {
43 { sink.warn(warning) } noexcept;
44};
45
48 void warn(const Warning&) noexcept {}
49};
50
51static_assert(WarningSink<NullWarningSink>);
52
59template <std::size_t Capacity>
61 static_assert(Capacity > 0, "BufferedWarningSink needs a positive capacity");
62
63 public:
64 void warn(const Warning& warning) noexcept {
65 if (count_ == Capacity) {
66 // Full: advance the window, dropping the current oldest.
67 head_ = (head_ + 1) % Capacity;
68 ++dropped_;
69 } else {
70 ++count_;
71 }
72 const auto slot = (head_ + count_ - 1) % Capacity;
73 entries_[slot] = warning;
74 }
75
76 [[nodiscard]] auto size() const noexcept -> std::size_t { return count_; }
77
78 [[nodiscard]] static constexpr auto capacity() noexcept -> std::size_t {
79 return Capacity;
80 }
81
82 [[nodiscard]] bool empty() const noexcept { return count_ == 0; }
83
84 [[nodiscard]] bool full() const noexcept { return count_ == Capacity; }
85
86 // Total warnings overwritten (lost) because the ring was full when they
87 // arrived.
88 [[nodiscard]] auto dropped() const noexcept -> std::uint64_t {
89 return dropped_;
90 }
91
92 // Oldest-first access: index 0 is the oldest retained warning, size() - 1 is
93 // the newest. Behavior is undefined for index >= size().
94 [[nodiscard]] auto operator[](std::size_t index) const noexcept
95 -> const Warning& {
96 return entries_[(head_ + index) % Capacity];
97 }
98
99 void clear() noexcept {
100 head_ = 0;
101 count_ = 0;
102 dropped_ = 0;
103 }
104
105 private:
106 std::array<Warning, Capacity> entries_{};
107 std::size_t head_ = 0; // index of the oldest retained warning
108 std::size_t count_ = 0; // number of retained warnings (<= Capacity)
109 std::uint64_t dropped_ = 0;
110};
111
112#endif // TESS_DIAGNOSTICS_ENABLED
113
114} // namespace tess::diagnostics
Definition warning_sink.h:60
Definition warning_sink.h:42
Definition warning_sink.h:47
Definition warning_sink.h:33