| 1 | // Copyright 2021-2022, Collabora, Ltd.cxx . . $ `<file>/src/xrt/auxiliary/util/u_template_historybuf.hpp`/ |
| 2 | // |
| 3 | // SPDX-License-Identifier: BSL-1.0 |
| 4 | /*! |
| 5 | * @file |
| 6 | * @brief Ringbuffer implementation for keeping track of the past state of things |
| 7 | * @author Rylie Pavlik <[email protected]> |
| 8 | * @author Moshi Turner <[email protected]> |
| 9 | * @ingroup aux_util |
| 10 | */ |
| 11 | |
| 12 | #pragma once |
| 13 | |
| 14 | #include "u_template_historybuf_impl_helpers.hpp"cxx . . $ `<file>/src/xrt/auxiliary/util/u_template_historybuf_impl_helpers.hpp`/ |
| 15 | #include "u_iterator_base.hpp"cxx . . $ `<file>/src/xrt/auxiliary/util/u_iterator_base.hpp`/ |
| 16 | |
| 17 | #include <limits> |
| 18 | #include <array> |
| 19 | |
| 20 | namespace xrtcxx . . $ xrt/::auxiliarycxx . . $ xrt/auxiliary/::utilcxx . . $ xrt/auxiliary/util/ { |
| 21 | |
| 22 | namespace detailcxx . . $ xrt/auxiliary/util/detail/ { |
| 23 | template <typename T, size_t MaxSize> class HistoryBufConstIteratorcxx . . $ xrt/auxiliary/util/detail/HistoryBufConstIterator#; |
| 24 | template <typename T, size_t MaxSize> class HistoryBufIteratorcxx . . $ xrt/auxiliary/util/detail/HistoryBufIterator#; |
| 25 | } // namespace detail |
| 26 | |
| 27 | /*! |
| 28 | * @brief Stores some number of values in a ring buffer, overwriting the earliest-pushed-remaining element if out of |
| 29 | * room. |
| 30 | * |
| 31 | * Note this should only store value types, since there's no way to destroy elements other than overwriting them, and |
| 32 | * all elements are default-initialized upon construction of the container. |
| 33 | * |
| 34 | * @note Unlike @ref m_relation_history which is based on this, this data structure is |
| 35 | * **not inherently safe for concurrent/threaded use**: there are no locks built-in. |
| 36 | */ |
| 37 | template <typename T, size_t MaxSize> class HistoryBuffercxx . . $ xrt/auxiliary/util/HistoryBuffer# |
| 38 | { |
| 39 | public: |
| 40 | //! Is the buffer empty? |
| 41 | bool |
| 42 | emptycxx . . $ xrt/auxiliary/util/HistoryBuffer#empty(3482b152b9333168).() const noexcept; |
| 43 | |
| 44 | //! How many elements are in the buffer? |
| 45 | size_t |
| 46 | sizecxx . . $ xrt/auxiliary/util/HistoryBuffer#size(c9b1e749b64d5edc).() const noexcept; |
| 47 | |
| 48 | |
| 49 | /*! |
| 50 | * @brief Put something at the back, overwriting whatever was at the front if necessary. |
| 51 | * |
| 52 | * This is permitted to invalidate iterators. They won't be poisoned, but they will return something you don't |
| 53 | * expect. |
| 54 | */ |
| 55 | void |
| 56 | push_backcxx . . $ xrt/auxiliary/util/HistoryBuffer#push_back(77d9f17553126ecb).(const T &element); |
| 57 | |
| 58 | /*! |
| 59 | * @brief Logically remove the newest element from the buffer. |
| 60 | * |
| 61 | * This is permitted to invalidate iterators. They won't be poisoned, |
| 62 | * but they will return something you don't expect. |
| 63 | * |
| 64 | * @return true if there was something to pop. |
| 65 | */ |
| 66 | bool |
| 67 | pop_backcxx . . $ xrt/auxiliary/util/HistoryBuffer#pop_back(77ce49a7840d23c7).() noexcept |
| 68 | { |
| 69 | return helper_cxx . . $ xrt/auxiliary/util/HistoryBuffer#helper_..pop_backcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#pop_back(77ce49a7840d23c7).(); |
| 70 | } |
| 71 | |
| 72 | /*! |
| 73 | * @brief Logically remove the oldest element from the buffer. |
| 74 | * |
| 75 | * The value still remains in the backing container until overwritten, but it isn't accessible anymore. |
| 76 | * |
| 77 | * This invalidates iterators. They won't be poisoned, but they will return something you don't expect. |
| 78 | */ |
| 79 | void |
| 80 | pop_frontcxx . . $ xrt/auxiliary/util/HistoryBuffer#pop_front(ced63f7c635d850d).() noexcept |
| 81 | { |
| 82 | helper_cxx . . $ xrt/auxiliary/util/HistoryBuffer#helper_..pop_frontcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#pop_front(ced63f7c635d850d).(); |
| 83 | } |
| 84 | |
| 85 | /*! |
| 86 | * @brief Use a value at a given age, where age 0 is the most recent value, age 1 precedes it, etc. |
| 87 | * (reverse chronological order) |
| 88 | * |
| 89 | * Out of bounds accesses will return nullptr. |
| 90 | */ |
| 91 | T * |
| 92 | get_at_agecxx . . $ xrt/auxiliary/util/HistoryBuffer#get_at_age(5895172676490633).(size_t age) noexcept; |
| 93 | |
| 94 | //! @overload |
| 95 | const T * |
| 96 | get_at_agecxx . . $ xrt/auxiliary/util/HistoryBuffer#get_at_age(a26c718ad78c386b).(size_t age) const noexcept; |
| 97 | |
| 98 | /*! |
| 99 | * @brief Like get_at_age() but values larger than the oldest age are clamped. |
| 100 | */ |
| 101 | T * |
| 102 | get_at_clamped_agecxx . . $ xrt/auxiliary/util/HistoryBuffer#get_at_clamped_age(5895172676490633).(size_t age) noexcept |
| 103 | { |
| 104 | size_t inner_index = 0; |
| 105 | if (helper_cxx . . $ xrt/auxiliary/util/HistoryBuffer#helper_..clamped_age_to_inner_indexcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#clamped_age_to_inner_index(597887df2858fe53).(age, inner_index)) { |
| 106 | return &internalBuffercxx . . $ xrt/auxiliary/util/HistoryBuffer#internalBuffer.[inner_index]; |
| 107 | } |
| 108 | return nullptr; |
| 109 | } |
| 110 | |
| 111 | //! @overload |
| 112 | const T * |
| 113 | get_at_clamped_agecxx . . $ xrt/auxiliary/util/HistoryBuffer#get_at_clamped_age(a26c718ad78c386b).(size_t age) const noexcept |
| 114 | { |
| 115 | size_t inner_index = 0; |
| 116 | if (helper_cxx . . $ xrt/auxiliary/util/HistoryBuffer#helper_..clamped_age_to_inner_indexcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#clamped_age_to_inner_index(597887df2858fe53).(age, inner_index)) { |
| 117 | return &internalBuffercxx . . $ xrt/auxiliary/util/HistoryBuffer#internalBuffer.[inner_index]; |
| 118 | } |
| 119 | return nullptr; |
| 120 | } |
| 121 | |
| 122 | /*! |
| 123 | * @brief Use a value at a given index, where 0 is the least-recent value still stored, index 1 follows it, |
| 124 | * etc. (chronological order) |
| 125 | * |
| 126 | * Out of bounds accesses will return nullptr. |
| 127 | */ |
| 128 | T * |
| 129 | get_at_indexcxx . . $ xrt/auxiliary/util/HistoryBuffer#get_at_index(5895172676490633).(size_t index) noexcept; |
| 130 | |
| 131 | //! @overload |
| 132 | const T * |
| 133 | get_at_indexcxx . . $ xrt/auxiliary/util/HistoryBuffer#get_at_index(a26c718ad78c386b).(size_t index) const noexcept; |
| 134 | |
| 135 | using iteratorcxx . . $ xrt/auxiliary/util/HistoryBuffer#iterator# = detailcxx . . $ xrt/auxiliary/util/detail/::HistoryBufIteratorcxx . . $ xrt/auxiliary/util/detail/HistoryBufIterator#<T, MaxSize>; |
| 136 | using const_iteratorcxx . . $ xrt/auxiliary/util/HistoryBuffer#const_iterator# = detailcxx . . $ xrt/auxiliary/util/detail/::HistoryBufConstIteratorcxx . . $ xrt/auxiliary/util/detail/HistoryBufConstIterator#<T, MaxSize>; |
| 137 | |
| 138 | //! Get a const iterator for the oldest element. |
| 139 | const_iteratorcxx . . $ xrt/auxiliary/util/HistoryBuffer#const_iterator# |
| 140 | cbegincxx . . $ xrt/auxiliary/util/HistoryBuffer#cbegin(da0a19c4496e2d95).() const noexcept; |
| 141 | |
| 142 | //! Get a "past the end" (past the newest) const iterator |
| 143 | const_iteratorcxx . . $ xrt/auxiliary/util/HistoryBuffer#const_iterator# |
| 144 | cendcxx . . $ xrt/auxiliary/util/HistoryBuffer#cend(da0a19c4496e2d95).() const noexcept; |
| 145 | |
| 146 | //! Get a const iterator for the oldest element. |
| 147 | const_iteratorcxx . . $ xrt/auxiliary/util/HistoryBuffer#const_iterator# |
| 148 | begincxx . . $ xrt/auxiliary/util/HistoryBuffer#begin(da0a19c4496e2d95).() const noexcept; |
| 149 | |
| 150 | //! Get a "past the end" (past the newest) const iterator |
| 151 | const_iteratorcxx . . $ xrt/auxiliary/util/HistoryBuffer#const_iterator# |
| 152 | endcxx . . $ xrt/auxiliary/util/HistoryBuffer#end(da0a19c4496e2d95).() const noexcept; |
| 153 | |
| 154 | //! Get an iterator for the oldest element. |
| 155 | iteratorcxx . . $ xrt/auxiliary/util/HistoryBuffer#iterator# |
| 156 | begincxx . . $ xrt/auxiliary/util/HistoryBuffer#begin(80f738d4d7381cf5).() noexcept; |
| 157 | |
| 158 | //! Get a "past the end" (past the newest) iterator |
| 159 | iteratorcxx . . $ xrt/auxiliary/util/HistoryBuffer#iterator# |
| 160 | endcxx . . $ xrt/auxiliary/util/HistoryBuffer#end(80f738d4d7381cf5).() noexcept; |
| 161 | |
| 162 | /*! |
| 163 | * @brief Gets a reference to the front (oldest) element in the buffer. |
| 164 | * @throws std::logic_error if buffer is empty |
| 165 | */ |
| 166 | T & |
| 167 | frontcxx . . $ xrt/auxiliary/util/HistoryBuffer#front(19b2b6f17af048b).(); |
| 168 | |
| 169 | //! @overload |
| 170 | const T & |
| 171 | frontcxx . . $ xrt/auxiliary/util/HistoryBuffer#front(ea465e50d690edfe).() const; |
| 172 | |
| 173 | /*! |
| 174 | * @brief Gets a reference to the back (newest) element in the buffer. |
| 175 | * @throws std::logic_error if buffer is empty |
| 176 | */ |
| 177 | T & |
| 178 | backcxx . . $ xrt/auxiliary/util/HistoryBuffer#back(19b2b6f17af048b).(); |
| 179 | |
| 180 | //! @overload |
| 181 | const T & |
| 182 | backcxx . . $ xrt/auxiliary/util/HistoryBuffer#back(ea465e50d690edfe).() const; |
| 183 | |
| 184 | void |
| 185 | clearcxx . . $ xrt/auxiliary/util/HistoryBuffer#clear(49f6e7a06ebc5aa8).(); |
| 186 | |
| 187 | private: |
| 188 | // Make sure all valid indices can be represented in a signed integer of the same size |
| 189 | static_assert(MaxSize < (stdcxx . . $ std/::numeric_limitscxx . . $ std/numeric_limits#<size_t>::maxcxx . . $ std/numeric_limits#max(cee159cc271e827).() >> 1), "Cannot use most significant bit"); |
| 190 | |
| 191 | using container_tcxx . . $ xrt/auxiliary/util/HistoryBuffer#container_t# = stdcxx . . $ std/::arraycxx . . $ std/array#<T, MaxSize>; |
| 192 | container_tcxx . . $ xrt/auxiliary/util/HistoryBuffer#container_t# internalBuffercxx . . $ xrt/auxiliary/util/HistoryBuffer#internalBuffer.{}; |
| 193 | detailcxx . . $ xrt/auxiliary/util/detail/::RingBufferHelpercxx . . $ xrt/auxiliary/util/detail/RingBufferHelper# helper_cxx . . $ xrt/auxiliary/util/HistoryBuffer#helper_.{cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#RingBufferHelper(2c44cd906ae4e829).MaxSize}; |
| 194 | }; |
| 195 | |
| 196 | |
| 197 | template <typename T, size_t MaxSize> |
| 198 | void |
| 199 | HistoryBuffercxx . . $ xrt/auxiliary/util/HistoryBuffer#<T, MaxSize>::clearcxx . . $ xrt/auxiliary/util/HistoryBuffer#clear(49f6e7a06ebc5aa8).() |
| 200 | { |
| 201 | helper_cxx . . $ xrt/auxiliary/util/HistoryBuffer#helper_..clearcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#clear(49f6e7a06ebc5aa8).(); |
| 202 | } |
| 203 | |
| 204 | template <typename T, size_t MaxSize> |
| 205 | inline bool |
| 206 | HistoryBuffercxx . . $ xrt/auxiliary/util/HistoryBuffer#<T, MaxSize>::emptycxx . . $ xrt/auxiliary/util/HistoryBuffer#empty(3482b152b9333168).() const noexcept |
| 207 | { |
| 208 | return helper_cxx . . $ xrt/auxiliary/util/HistoryBuffer#helper_..emptycxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#empty(3482b152b9333168).(); |
| 209 | } |
| 210 | |
| 211 | template <typename T, size_t MaxSize> |
| 212 | inline size_t |
| 213 | HistoryBuffercxx . . $ xrt/auxiliary/util/HistoryBuffer#<T, MaxSize>::sizecxx . . $ xrt/auxiliary/util/HistoryBuffer#size(c9b1e749b64d5edc).() const noexcept |
| 214 | { |
| 215 | return helper_cxx . . $ xrt/auxiliary/util/HistoryBuffer#helper_..sizecxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#size(c9b1e749b64d5edc).(); |
| 216 | } |
| 217 | |
| 218 | template <typename T, size_t MaxSize> |
| 219 | inline void |
| 220 | HistoryBuffercxx . . $ xrt/auxiliary/util/HistoryBuffer#<T, MaxSize>::push_backcxx . . $ xrt/auxiliary/util/HistoryBuffer#push_back(77d9f17553126ecb).(const T &element) |
| 221 | { |
| 222 | auto inner_index = helper_cxx . . $ xrt/auxiliary/util/HistoryBuffer#helper_..push_back_locationcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#push_back_location(cee159cc271e827).(); |
| 223 | internalBuffercxx . . $ xrt/auxiliary/util/HistoryBuffer#internalBuffer.[inner_index] = element; |
| 224 | } |
| 225 | |
| 226 | template <typename T, size_t MaxSize> |
| 227 | inline T * |
| 228 | HistoryBuffercxx . . $ xrt/auxiliary/util/HistoryBuffer#<T, MaxSize>::get_at_agecxx . . $ xrt/auxiliary/util/HistoryBuffer#get_at_age(5895172676490633).(size_t age) noexcept |
| 229 | { |
| 230 | size_t inner_index = 0; |
| 231 | if (helper_cxx . . $ xrt/auxiliary/util/HistoryBuffer#helper_..age_to_inner_indexcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#age_to_inner_index(597887df2858fe53).(age, inner_index)) { |
| 232 | return &internalBuffercxx . . $ xrt/auxiliary/util/HistoryBuffer#internalBuffer.[inner_index]; |
| 233 | } |
| 234 | return nullptr; |
| 235 | } |
| 236 | template <typename T, size_t MaxSize> |
| 237 | inline const T * |
| 238 | HistoryBuffercxx . . $ xrt/auxiliary/util/HistoryBuffer#<T, MaxSize>::get_at_agecxx . . $ xrt/auxiliary/util/HistoryBuffer#get_at_age(a26c718ad78c386b).(size_t age) const noexcept |
| 239 | { |
| 240 | size_t inner_index = 0; |
| 241 | if (helper_cxx . . $ xrt/auxiliary/util/HistoryBuffer#helper_..age_to_inner_indexcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#age_to_inner_index(597887df2858fe53).(age, inner_index)) { |
| 242 | return &internalBuffercxx . . $ xrt/auxiliary/util/HistoryBuffer#internalBuffer.[inner_index]; |
| 243 | } |
| 244 | return nullptr; |
| 245 | } |
| 246 | |
| 247 | template <typename T, size_t MaxSize> |
| 248 | inline T * |
| 249 | HistoryBuffercxx . . $ xrt/auxiliary/util/HistoryBuffer#<T, MaxSize>::get_at_indexcxx . . $ xrt/auxiliary/util/HistoryBuffer#get_at_index(5895172676490633).(size_t index) noexcept |
| 250 | { |
| 251 | size_t inner_index = 0; |
| 252 | if (helper_cxx . . $ xrt/auxiliary/util/HistoryBuffer#helper_..index_to_inner_indexcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#index_to_inner_index(597887df2858fe53).(index, inner_index)) { |
| 253 | return &internalBuffercxx . . $ xrt/auxiliary/util/HistoryBuffer#internalBuffer.[inner_index]; |
| 254 | } |
| 255 | return nullptr; |
| 256 | } |
| 257 | |
| 258 | template <typename T, size_t MaxSize> |
| 259 | inline const T * |
| 260 | HistoryBuffercxx . . $ xrt/auxiliary/util/HistoryBuffer#<T, MaxSize>::get_at_indexcxx . . $ xrt/auxiliary/util/HistoryBuffer#get_at_index(a26c718ad78c386b).(size_t index) const noexcept |
| 261 | { |
| 262 | size_t inner_index = 0; |
| 263 | if (helper_cxx . . $ xrt/auxiliary/util/HistoryBuffer#helper_..index_to_inner_indexcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#index_to_inner_index(597887df2858fe53).(index, inner_index)) { |
| 264 | return &internalBuffercxx . . $ xrt/auxiliary/util/HistoryBuffer#internalBuffer.[inner_index]; |
| 265 | } |
| 266 | return nullptr; |
| 267 | } |
| 268 | |
| 269 | template <typename T, size_t MaxSize> |
| 270 | inline T & |
| 271 | HistoryBuffercxx . . $ xrt/auxiliary/util/HistoryBuffer#<T, MaxSize>::frontcxx . . $ xrt/auxiliary/util/HistoryBuffer#front(19b2b6f17af048b).() |
| 272 | { |
| 273 | if (emptycxx . . $ xrt/auxiliary/util/HistoryBuffer#empty(3482b152b9333168).()) { |
| 274 | throw stdcxx . . $ std/::logic_errorcxx . . $ std/logic_error#("Cannot get the front of an empty buffer"); |
| 275 | } |
| 276 | return internalBuffercxx . . $ xrt/auxiliary/util/HistoryBuffer#internalBuffer.[helper_cxx . . $ xrt/auxiliary/util/HistoryBuffer#helper_..front_inner_indexcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#front_inner_index(c9b1e749b64d5edc).()]; |
| 277 | } |
| 278 | |
| 279 | template <typename T, size_t MaxSize> |
| 280 | inline const T & |
| 281 | HistoryBuffercxx . . $ xrt/auxiliary/util/HistoryBuffer#<T, MaxSize>::frontcxx . . $ xrt/auxiliary/util/HistoryBuffer#front(ea465e50d690edfe).() const |
| 282 | { |
| 283 | if (emptycxx . . $ xrt/auxiliary/util/HistoryBuffer#empty(3482b152b9333168).()) { |
| 284 | throw stdcxx . . $ std/::logic_errorcxx . . $ std/logic_error#("Cannot get the front of an empty buffer"); |
| 285 | } |
| 286 | return internalBuffercxx . . $ xrt/auxiliary/util/HistoryBuffer#internalBuffer.[helper_cxx . . $ xrt/auxiliary/util/HistoryBuffer#helper_..front_inner_indexcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#front_inner_index(c9b1e749b64d5edc).()]; |
| 287 | } |
| 288 | |
| 289 | template <typename T, size_t MaxSize> |
| 290 | inline T & |
| 291 | HistoryBuffercxx . . $ xrt/auxiliary/util/HistoryBuffer#<T, MaxSize>::backcxx . . $ xrt/auxiliary/util/HistoryBuffer#back(19b2b6f17af048b).() |
| 292 | { |
| 293 | if (emptycxx . . $ xrt/auxiliary/util/HistoryBuffer#empty(3482b152b9333168).()) { |
| 294 | throw stdcxx . . $ std/::logic_errorcxx . . $ std/logic_error#("Cannot get the back of an empty buffer"); |
| 295 | } |
| 296 | return internalBuffercxx . . $ xrt/auxiliary/util/HistoryBuffer#internalBuffer.[helper_cxx . . $ xrt/auxiliary/util/HistoryBuffer#helper_..back_inner_indexcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#back_inner_index(c9b1e749b64d5edc).()]; |
| 297 | } |
| 298 | |
| 299 | template <typename T, size_t MaxSize> |
| 300 | inline const T & |
| 301 | HistoryBuffercxx . . $ xrt/auxiliary/util/HistoryBuffer#<T, MaxSize>::backcxx . . $ xrt/auxiliary/util/HistoryBuffer#back(ea465e50d690edfe).() const |
| 302 | { |
| 303 | if (emptycxx . . $ xrt/auxiliary/util/HistoryBuffer#empty(3482b152b9333168).()) { |
| 304 | throw stdcxx . . $ std/logic_error#logic_error(85c52e162fed56f9).::logic_errorcxx . . $ std/logic_error#("Cannot get the back of an empty buffer"); |
| 305 | } |
| 306 | return internalBuffercxx . . $ xrt/auxiliary/util/HistoryBuffer#internalBuffer.[helper_cxx . . $ xrt/auxiliary/util/HistoryBuffer#helper_..back_inner_indexcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#back_inner_index(c9b1e749b64d5edc).()]; |
| 307 | } |
| 308 | |
| 309 | |
| 310 | } // namespace xrt::auxiliary::util |
| 311 | |
| 312 | #include "u_template_historybuf_const_iterator.inl"cxx . . $ `<file>/src/xrt/auxiliary/util/u_template_historybuf_const_iterator.inl`/ |
| 313 | #include "u_template_historybuf_iterator.inl"cxx . . $ `<file>/src/xrt/auxiliary/util/u_template_historybuf_iterator.inl`/ |