The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Revision:
165:d1b4690b3f8b
Parent:
160:5571c4ff569f
Child:
169:a7c7b631e539
--- a/platform/CircularBuffer.h	Tue Mar 20 16:15:49 2018 +0000
+++ b/platform/CircularBuffer.h	Thu Apr 19 14:31:27 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
-