fastcat 0.13.15
C++ EtherCAT Device Command & Control Library
Loading...
Searching...
No Matches
ring_buffer.h
Go to the documentation of this file.
1#ifndef FASTCAT_RING_BUFFER_H_
2#define FASTCAT_RING_BUFFER_H_
3
4#include <iostream>
5#include <stdexcept>
6#include <vector>
7#include <mutex>
8
9namespace fastcat
10{
11template <typename T>
13{
14 std::vector<T> buffer_;
15 size_t index_ = 0;
16 size_t num_received_ = 0;
17 // std::mutex mutex_;
18
19 public:
20 RingBuffer(size_t buffer_size = 10) { buffer_.resize(buffer_size); }
21
22 T load(void) {
23 // const std::lock_guard<std::mutex> lock(mutex_);
24 return buffer_[index_ % buffer_.size()];
25 }
26
27 T load(size_t delay)
28 {
29 if (delay > buffer_.size()) {
30 throw std::runtime_error("Delay is greater than allocated buffer size");
31 }
32 // const std::lock_guard<std::mutex> lock(mutex_);
33 return buffer_[(index_ - delay) % buffer_.size()];
34 }
35
36 void store(const T& value) {
37 // const std::lock_guard<std::mutex> lock(mutex_);
38 buffer_[(++index_) % buffer_.size()] = value;
39 num_received_++;
40 }
41
42 void clear() {
43 // const std::lock_guard<std::mutex> lock(mutex_);
44 num_received_ = 0;
45 }
46
48 // const std::lock_guard<std::mutex> lock(mutex_);
49 return num_received_;
50 }
51
52 size_t size() {
53 return buffer_.size();
54 }
55};
56}
57
58#endif
Definition ring_buffer.h:13
size_t size()
Definition ring_buffer.h:52
T load(size_t delay)
Definition ring_buffer.h:27
size_t get_num_received()
Definition ring_buffer.h:47
T load(void)
Definition ring_buffer.h:22
RingBuffer(size_t buffer_size=10)
Definition ring_buffer.h:20
void store(const T &value)
Definition ring_buffer.h:36
void clear()
Definition ring_buffer.h:42
Definition device_base.h:18