Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
CircularBuffer.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018 DIVIDE-Studio
3 Copyright (c) 2009 Ionut Cava
4
5 This file is part of DIVIDE Framework.
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software
9 and associated documentation files (the "Software"), to deal in the Software
10 without restriction,
11 including without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so,
15 subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED,
22 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23 PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY,
26 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27 IN CONNECTION WITH THE SOFTWARE
28 OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 */
31
32#pragma once
33#ifndef DVD_CIRCULAR_BUFFER_H_
34#define DVD_CIRCULAR_BUFFER_H_
35
36//ref: https://github.com/embeddedartistry/embedded-resources/blob/master/examples/cpp/circular_buffer.cpp
37namespace Divide {
38
39template <class T, size_t N, bool threadSafe = false>
41 struct Lock
42 {
44 {
45 if constexpr ( threadSafe )
46 {
47 _lock.lock();
48 }
49 }
51 {
52 if constexpr ( threadSafe )
53 {
54 _lock.unlock();
55 }
56 }
57 mutable Mutex _lock;
58 };
59
60public:
61 void reset()
62 {
63 Lock();
64
65 _head = _tail;
66 _isFull = false;
67 }
68
69 void put(const T& item)
70 {
71 Lock();
72
73 _buffer[_head] = item;
74
75 if ( fullLocked() )
76 {
77 _tail = (_tail + 1) % N;
78 }
79
80 _head = (_head + 1) % N;
81 _isFull = _head == _tail;
82 }
83
84 [[nodiscard]] const T& get(const size_t idx) const
85 {
86 Lock();
87 return _buffer[idx];
88 }
89
90 [[nodiscard]] T get()
91 {
92 Lock();
93
94 if (!empty())
95 {
96 const T val = _buffer[_tail];
97 _isFull = false;
98 _tail = (_tail + 1) % N;
99
100 return val;
101 }
102
103 return T();
104 }
105
106 [[nodiscard]] inline size_t size() const noexcept
107 {
108 Lock();
109
110 if ( fullLocked() )
111 {
112 return N;
113 }
114
115 return (_head >= _tail)
116 ? _head - _tail
117 : N + _head - _tail;
118 }
119
120 [[nodiscard]] inline bool empty() const noexcept
121 {
122 Lock();
123 return (!fullLocked() && (_head == _tail));
124 }
125
126 [[nodiscard]] inline bool full() const noexcept
127 {
128 Lock();
129 return fullLocked();
130 }
131
132 [[nodiscard]] inline static size_t capacity() noexcept
133 {
134 return N;
135 }
136
137
138private:
139 [[nodiscard]] inline bool fullLocked() const noexcept
140 {
141 return _isFull;
142 }
143
144private:
146 size_t _head{0u};
147 size_t _tail{0u};
148 bool _isFull{false};
149};
150
151}; //namespace Divide
152
153#endif //DVD_CIRCULAR_BUFFER_H_
size_t size() const noexcept
bool full() const noexcept
void put(const T &item)
bool empty() const noexcept
const T & get(const size_t idx) const
bool fullLocked() const noexcept
static size_t capacity() noexcept
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
std::mutex Mutex
Definition: SharedMutex.h:40