EEROS  0.4.1.0
API for the EEROS Real-Time Robotics Framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
RingBuffer.hpp
Go to the documentation of this file.
1 #ifndef ORG_EEROS_CORE_SIGNALBUFFER_HPP_
2 #define ORG_EEROS_CORE_SIGNALBUFFER_HPP_
3 
4 #include <stdint.h>
5 #include <array>
6 #include <mutex>
7 
8 namespace eeros {
9 
10  template<typename T, int N = 32>
11  class RingBuffer {
12  public:
13  RingBuffer() : tail(0), len(0) { }
14 
15  bool push(T v) {
16  std::lock_guard<std::mutex> lock(mtx);
17  if(len == N) return false;
18  items[(tail + len++) % N] = v;
19  return true;
20  }
21 
22  bool pop(T& v) {
23  std::lock_guard<std::mutex> lock(mtx);
24  if(len == 0) return false;
25  v = items[tail];
26  tail = (tail + 1) % N;
27  len--;
28  return true;
29  }
30 
31  unsigned int length() const { return len; }
32 
33  constexpr int size() const { return N; }
34 
35  private:
36  std::mutex mtx;
37  unsigned int tail; // points to the next readable item
38  unsigned int len;
39  T items[N];
40  };
41 };
42 
43 #endif // ORG_EEROS_CORE_SIGNALBUFFER_HPP_
bool pop(T &v)
Definition: RingBuffer.hpp:22
bool push(T v)
Definition: RingBuffer.hpp:15
int v
Definition: RingBufferTest.cpp:14
Definition: RingBuffer.hpp:11
unsigned int length() const
Definition: RingBuffer.hpp:31
RingBuffer()
Definition: RingBuffer.hpp:13
constexpr int size() const
Definition: RingBuffer.hpp:33