Add assertion to make CounterType consistent with BufferSize.

CounterType is used to define type of _head and _tail counters. This may cause the following problems:
- counters are used as array indexes so only unsigned types should be used for counters,
- CounterType must be consistent with BufferSize and BufferSize is of uint32_t type (i.e. Circular Buffer with the following parameters: BufferSize = 1000, CounterType = unsigned char will not work properly).
pull/5578/head
Przemyslaw Stekiel 2017-11-07 09:43:09 +01:00
parent 47bae16a5c
commit c962913ed9
1 changed files with 28 additions and 1 deletions

View File

@ -19,6 +19,23 @@
#include "platform/mbed_critical.h"
namespace mbed {
namespace internal {
/* Detect if CounterType of the Circular buffer is of unsigned type. */
template<typename T>
struct is_unsigned { static const bool value = false; };
template<>
struct is_unsigned<unsigned char> { static const bool value = true; };
template<>
struct is_unsigned<unsigned short> { static const bool value = true; };
template<>
struct is_unsigned<unsigned int> { static const bool value = true; };
template<>
struct is_unsigned<unsigned long> { static const bool value = true; };
template<>
struct is_unsigned<unsigned long long> { static const bool value = true; };
};
/** \addtogroup platform */
/** @{*/
/**
@ -29,11 +46,22 @@ namespace mbed {
/** Templated Circular buffer class
*
* @note Synchronization level: Interrupt safe
* @note CounterType must be unsigned and consistent with BufferSize
*/
template<typename T, uint32_t BufferSize, typename CounterType = uint32_t>
class CircularBuffer {
public:
CircularBuffer() : _head(0), _tail(0), _full(false) {
MBED_STATIC_ASSERT(
internal::is_unsigned<CounterType>::value,
"CounterType must be unsigned"
);
MBED_STATIC_ASSERT(
(sizeof(CounterType) >= sizeof(uint32_t)) ||
(BufferSize < (((uint64_t) 1) << (sizeof(CounterType) * 8))),
"Invalid BufferSize for the CounterType"
);
}
~CircularBuffer() {
@ -140,4 +168,3 @@ private:
}
#endif