tin  1.5.9
static_ring.h
Go to the documentation of this file.
1 // Copyright (c) 2026 Tinverse LLC. All rights reserved.
2 // SPDX-License-Identifier: LicenseRef-Tinverse-Commercial
3 
13 
14 #pragma once
15 
16 #include <array>
17 #include <cstddef>
18 #include <type_traits>
19 #include <utility>
20 
21 #include "tsm/runtime/policy.h"
22 
23 namespace tsm::runtime::storage {
24 
33 template<typename Event, std::size_t Capacity>
35 {
36  static_assert(Capacity > 1,
37  "tsm: fixed_ring_queue needs at least two slots");
38 
39  public:
40  using value_type = Event;
41  static constexpr std::size_t capacity = Capacity;
42 
43  [[nodiscard]] bool try_push(Event const& event)
44  {
45  if (full()) {
46  return false;
47  }
48  data_[push_index_] = event;
49  push_index_ = next(push_index_);
50  ++size_;
51  return true;
52  }
53 
54  [[nodiscard]] bool try_push(Event&& event)
55  {
56  if (full()) {
57  return false;
58  }
59  data_[push_index_] = std::move(event);
60  push_index_ = next(push_index_);
61  ++size_;
62  return true;
63  }
64 
65  [[nodiscard]] bool try_pop(Event& event)
66  {
67  if (empty()) {
68  return false;
69  }
70  event = std::move(data_[pop_index_]);
71  pop_index_ = next(pop_index_);
72  --size_;
73  return true;
74  }
75 
76  template<typename Value>
77  [[nodiscard]] bool try_overwrite_latest(Value&& event)
78  {
79  if (empty()) {
80  return false;
81  }
82  data_[previous(push_index_)] = std::forward<Value>(event);
83  return true;
84  }
85 
86  [[nodiscard]] bool empty() const
87  {
88  return size_ == 0;
89  }
90  [[nodiscard]] bool full() const
91  {
92  return size_ == Capacity;
93  }
94  [[nodiscard]] std::size_t size() const
95  {
96  return size_;
97  }
98 
99  void clear()
100  {
101  push_index_ = 0;
102  pop_index_ = 0;
103  size_ = 0;
104  }
105 
106  private:
107  static constexpr std::size_t next(std::size_t index)
108  {
109  return (index + 1U) % Capacity;
110  }
111 
112  static constexpr std::size_t previous(std::size_t index)
113  {
114  return index == 0U ? Capacity - 1U : index - 1U;
115  }
116 
117  std::array<Event, Capacity> data_{};
118  std::size_t push_index_{};
119  std::size_t pop_index_{};
120  std::size_t size_{};
121 };
122 
129 template<std::size_t Capacity>
131 {
132  static constexpr std::size_t capacity = Capacity;
133 
134  template<typename Event>
136 };
137 
139 {};
141 {};
143 {};
144 
145 #if defined(__FREE_RTOS__)
147 #elif defined(__ZEPHYR__)
149 #else
151 #endif
152 
153 template<std::size_t Capacity, typename StorageTag = active_storage_tag>
155 
156 template<std::size_t Capacity>
158 {
160 };
161 
168 template<std::size_t Capacity>
170 
171 } // namespace tsm::runtime::storage
172 
173 namespace tsm {
174 
175 template<std::size_t Capacity>
177 
178 template<std::size_t Capacity>
180 
186 template<typename T,
187  typename StoragePolicy,
188  typename OverflowPolicy = overflow::reject_newest>
189 class queue
190 {
191  static_assert(runtime::queue_storage_policy<StoragePolicy, T>,
192  "tsm: queue requires a storage policy for T");
193  static_assert(overflow_policy<OverflowPolicy>,
194  "tsm: queue requires a known overflow policy");
195 
196  using storage_type = typename StoragePolicy::template queue<T>;
197 
198  public:
199  using value_type = T;
200  using storage_policy = StoragePolicy;
201  using overflow_policy = OverflowPolicy;
202 
203  [[nodiscard]] bool try_push(T const& value)
204  {
205  return push(value);
206  }
207 
208  [[nodiscard]] bool try_push(T&& value)
209  {
210  return push(std::move(value));
211  }
212 
213  [[nodiscard]] bool try_pop(T& value)
214  {
215  return storage_.try_pop(value);
216  }
217 
218  [[nodiscard]] bool empty() const
219  {
220  return storage_.empty();
221  }
222  [[nodiscard]] bool full() const
223  {
224  return storage_.full();
225  }
226  [[nodiscard]] std::size_t size() const
227  {
228  return storage_.size();
229  }
230 
231  private:
232  template<typename Value>
233  [[nodiscard]] bool push(Value&& value)
234  {
235  if constexpr (std::same_as<OverflowPolicy, overflow::reject_newest>) {
236  return storage_.try_push(std::forward<Value>(value));
237  } else if constexpr (std::same_as<OverflowPolicy,
239  if (storage_.try_push(std::forward<Value>(value))) {
240  return true;
241  }
242  T discarded{};
243  if (!storage_.try_pop(discarded)) {
244  return false;
245  }
246  return storage_.try_push(std::forward<Value>(value));
247  } else {
248  if (storage_.try_push(std::forward<Value>(value))) {
249  return true;
250  }
251  return storage_.try_overwrite_latest(std::forward<Value>(value));
252  }
253  }
254 
255  storage_type storage_{};
256 };
257 
258 } // namespace tsm
Definition: static_ring.h:190
bool try_pop(T &value)
Definition: static_ring.h:213
T value_type
Definition: static_ring.h:199
std::size_t size() const
Definition: static_ring.h:226
bool full() const
Definition: static_ring.h:222
StoragePolicy storage_policy
Definition: static_ring.h:200
bool empty() const
Definition: static_ring.h:218
bool try_push(T const &value)
Definition: static_ring.h:203
OverflowPolicy overflow_policy
Definition: static_ring.h:201
bool try_push(T &&value)
Definition: static_ring.h:208
Definition: static_ring.h:35
static constexpr std::size_t capacity
Definition: static_ring.h:41
bool empty() const
Definition: static_ring.h:86
void clear()
Definition: static_ring.h:99
bool try_overwrite_latest(Value &&event)
Definition: static_ring.h:77
std::size_t size() const
Definition: static_ring.h:94
bool try_push(Event &&event)
Definition: static_ring.h:54
Event value_type
Definition: static_ring.h:40
bool full() const
Definition: static_ring.h:90
bool try_push(Event const &event)
Definition: static_ring.h:43
bool try_pop(Event &event)
Definition: static_ring.h:65
Definition: freertos.h:26
typename target_storage< Capacity >::type target_queue
Definition: static_ring.h:169
Definition: bare_metal.h:20
runtime::storage::target_queue< Capacity > target_storage
Definition: static_ring.h:179
Runtime policy tags for dispatch and bounded queue admission.
Definition: policy.h:40
Definition: policy.h:38
Definition: static_ring.h:141
Definition: static_ring.h:131
static constexpr std::size_t capacity
Definition: static_ring.h:132
Definition: static_ring.h:139
Definition: static_ring.h:154
Definition: static_ring.h:143