areaDetector  3-5-0
EPICS areaDetector framework
CircularBuffer.h
Go to the documentation of this file.
1 #ifndef CIRCULAR_BUFFER_H_
2 #define CIRCULAR_BUFFER_H_
3 
4 #include <algorithm>
5 #include <vector>
6 
7 template <class T>
9 public:
10  CircularBuffer(size_t max_length)
11  : max_length_(max_length),
12  cpos_(0),
13  size_(0),
14  buffer_(max_length)
15  {}
16 
17  size_t max_size() const {
18  return max_length_;
19  }
20 
21  size_t size() const {
22  return size_;
23  }
24 
25  const T& first() const {
26  return buffer_[cpos_];
27  }
28 
29  const T& last() const {
30  return buffer_[(cpos_ + max_length_ - 1) % max_length_];
31  }
32 
33  void push_back(const T& el) {
34  buffer_[cpos_] = el;
35 
36  cpos_ = (cpos_ + 1) % max_length_;
37 
38  if (size_ < max_length_) {
39  size_++;
40  }
41  }
42 
43  const T& operator[](size_t i) const {
44  return buffer_[(cpos_ + i) % size_];
45  }
46 
47  void clear() {
48  cpos_ = 0;
49  size_ = 0;
50  }
51 
52  const size_t copy_to_array(T * const buffer, size_t buffer_size) const {
53  size_t size = std::min(size_, buffer_size);
54  if (size_ < max_length_) {
55  std::copy(buffer_.begin(), buffer_.begin() + size, buffer);
56  return size;
57  } else {
58  size_t len_to_end = max_length_ - cpos_;
59  if (size < len_to_end) {
60  std::copy(buffer_.begin() + cpos_,
61  buffer_.begin() + cpos_ + size, buffer);
62  return size;
63  } else {
64  std::copy(buffer_.begin() + cpos_,
65  buffer_.begin() + cpos_ + len_to_end,
66  buffer);
67  std::copy(buffer_.begin(),
68  buffer_.begin() + (size - len_to_end),
69  buffer + len_to_end);
70  return size;
71  }
72  }
73  }
74 
75 private:
76  size_t max_length_;
77  size_t cpos_;
78  size_t size_;
79  std::vector<T> buffer_;
80 };
81 
82 #endif // CIRCULAR_BUFFER_H_
int i
Definition: makeAdl.py:479
size_t max_size() const
Definition: CircularBuffer.h:17
const T & first() const
Definition: CircularBuffer.h:25
const T & last() const
Definition: CircularBuffer.h:29
#define min(x, y)
Definition: mar3xx_pck.c:33
void clear()
Definition: CircularBuffer.h:47
size_t size() const
Definition: CircularBuffer.h:21
void push_back(const T &el)
Definition: CircularBuffer.h:33
Definition: CircularBuffer.h:8
CircularBuffer(size_t max_length)
Definition: CircularBuffer.h:10
const size_t copy_to_array(T *const buffer, size_t buffer_size) const
Definition: CircularBuffer.h:52
const T & operator[](size_t i) const
Definition: CircularBuffer.h:43