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
parent
4362d77083
commit
f57a1c3b17
|
|
@ -2,6 +2,9 @@
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#ifndef __AVR__
|
||||||
|
#include <limits>
|
||||||
|
#endif
|
||||||
|
|
||||||
/// A generic circular index class which can be used to build circular buffers
|
/// A generic circular index class which can be used to build circular buffers
|
||||||
/// Can hold up to size elements
|
/// Can hold up to size elements
|
||||||
|
|
@ -12,6 +15,11 @@
|
||||||
template <typename index_t = uint_fast8_t, size_t size = 16>
|
template <typename index_t = uint_fast8_t, size_t size = 16>
|
||||||
class CircularIndex {
|
class CircularIndex {
|
||||||
public:
|
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()
|
constexpr inline CircularIndex()
|
||||||
: tail(0)
|
: tail(0)
|
||||||
, head(0) {}
|
, head(0) {}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue