CircularIndex: add static checks for index/size limits

The empty/full distinction fails to work if size matches the number of
representable positions for the index type.

Add a static check to ensure this invariant is met where possible.
pull/49/head
Yuri D'Elia 2021-07-06 00:08:55 +02:00 committed by DRracer
parent 4362d77083
commit f57a1c3b17
1 changed files with 8 additions and 0 deletions

View File

@ -2,6 +2,9 @@
#include <stdint.h>
#include <stddef.h>
#ifndef __AVR__
#include <limits>
#endif
/// A generic circular index class which can be used to build circular buffers
/// Can hold up to size elements
@ -12,6 +15,11 @@
template <typename index_t = uint_fast8_t, size_t size = 16>
class CircularIndex {
public:
#ifndef __AVR__
static_assert(size <= std::numeric_limits<index_t>::max(),
"index_t is too small for the requested size");
#endif
constexpr inline CircularIndex()
: tail(0)
, head(0) {}