mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
Diff: platform/CircularBuffer.h
- Revision:
- 184:08ed48f1de7f
- Parent:
- 180:96ed750bd169
- Child:
- 186:707f6e361f3e
--- a/platform/CircularBuffer.h Tue Mar 20 17:01:51 2018 +0000 +++ b/platform/CircularBuffer.h Thu Apr 19 17:12:19 2018 +0100 @@ -17,8 +17,26 @@ #define MBED_CIRCULARBUFFER_H #include "platform/mbed_critical.h" +#include "platform/mbed_assert.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 +47,22 @@ /** 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 +169,3 @@ } #endif -