Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
Loading...
Searching...
No Matches
ring_buffer.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cassert>
4
5namespace slick::sim::utils {
6
7template<typename T>
8class RingBuffer {
9public:
10 explicit RingBuffer(size_t capacity)
11 : capacity_(capacity)
12 , buffer_(new T[capacity])
13 , head_(0)
14 , tail_(0)
15 , mask_(capacity_ - 1)
16 {
17 assert((capacity & (capacity - 1)) == 0 && "Capacity must be a power of two");
18 }
19
21 delete[] buffer_;
22 buffer_ = nullptr;
23 }
24
25 RingBuffer(const RingBuffer&) = delete;
26 RingBuffer& operator=(const RingBuffer&) = delete;
27
28 RingBuffer(RingBuffer&& other) noexcept
29 : capacity_(other.capacity_)
30 , buffer_(other.buffer_)
31 , head_(other.head_)
32 , tail_(other.tail_)
33 , mask_(other.mask_)
34 {
35 other.buffer_ = nullptr;
36 }
37
38 RingBuffer& operator=(RingBuffer&& other) noexcept {
39 if (this != &other) {
40 delete[] buffer_;
41 capacity_ = other.capacity_;
42 buffer_ = other.buffer_;
43 head_ = other.head_;
44 tail_ = other.tail_;
45 mask_ = other.mask_;
46 other.buffer_ = nullptr;
47 }
48 return *this;
49 }
50
51 void push(const T& item) {
52 buffer_[head_++ & mask_] = item;
53 if ((head_ & mask_) == (tail_ & mask_)) {
54 // Overwrite oldest item
55 ++tail_;
56 }
57 }
58
59 void emplace(T&& item) {
60 buffer_[head_++ & mask_] = std::move(item);
61 if ((head_ & mask_) == (tail_ & mask_)) {
62 // Overwrite oldest item
63 ++tail_;
64 }
65 }
66
67 bool try_push(const T& item) {
68 if (size() == capacity_) {
69 return false; // Buffer is full
70 }
71 buffer_[head_++ & mask_] = item;
72 return true;
73 }
74
75 bool try_emplace(T&& item) {
76 if (size() == capacity_) {
77 return false; // Buffer is full
78 }
79 buffer_[head_++ & mask_] = std::move(item);
80 return true;
81 }
82
83 bool pop_front(T& item) {
84 if (isEmpty()) {
85 return false; // Buffer is empty
86 }
87 item = buffer_[tail_++ & mask_];
88 return true;
89 }
90
91 bool pop_back(T& item) {
92 if (isEmpty()) {
93 return false; // Buffer is empty
94 }
95 item = buffer_[--head_ & mask_];
96 return true;
97 }
98
99 T& front() {
100 return buffer_[tail_ & mask_];
101 }
102
103 T& back() {
104 return buffer_[(head_ - 1) & mask_];
105 }
106
107 size_t size() const {
108 return head_ - tail_;
109 }
110
111 bool isEmpty() const {
112 return head_ == tail_;
113 }
114
115 bool isFull() const {
116 return size() == capacity_;
117 }
118
119 T* operator[](size_t index) {
120 if (index >= size()) {
121 return nullptr;
122 }
123 return &buffer_[(tail_ + index) & mask_];
124 }
125
126private:
127 size_t capacity_;
128 T* buffer_;
129 size_t head_;
130 size_t tail_;
131 size_t mask_;
132};
133
134}
RingBuffer & operator=(RingBuffer &&other) noexcept
bool try_push(const T &item)
RingBuffer(const RingBuffer &)=delete
RingBuffer & operator=(const RingBuffer &)=delete
RingBuffer(RingBuffer &&other) noexcept
Conversions between wire decimals with a constant exponent of -9 (CME PRICE9, ICE Decimal9) and the s...
Definition decimal.hpp:9