Static Ring Buffer Template

Medium C++ Memory Asked at Qualcomm
Solve this problem

Write a capacity-parameterised ring buffer template suitable for an ISR producer and a main-loop consumer on a single-core MCU.

Constraints

  • Capacity is a template parameter and must be a power of two.
  • push and pop return bool; no exceptions.
  • Use masking instead of modulo for index wrap.
  • No dynamic allocation.
  • Head and tail must be declared volatile so the ISR's writes are observed.
  • Empty and full must be distinguishable without a separate count.
Starter codeC++
1#include <cstddef>2#include <cstdint>3 4template <typename T, size_t Capacity>5class RingBuffer {6    // TODO7};