Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
RingBuffer.h
Go to the documentation of this file.
1/*
2Copyright (c) 2018 DIVIDE-Studio
3Copyright (c) 2009 Ionut Cava
4
5This file is part of DIVIDE Framework.
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software
9and associated documentation files (the "Software"), to deal in the Software
10without restriction,
11including without limitation the rights to use, copy, modify, merge, publish,
12distribute, sublicense,
13and/or sell copies of the Software, and to permit persons to whom the
14Software is furnished to do so,
15subject to the following conditions:
16
17The above copyright notice and this permission notice shall be included in
18all copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED,
22INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23PARTICULAR PURPOSE AND NONINFRINGEMENT.
24IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25DAMAGES OR OTHER LIABILITY,
26WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27IN CONNECTION WITH THE SOFTWARE
28OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30*/
31
32#pragma once
33#ifndef DVD_CORE_RING_BUFFER_H_
34#define DVD_CORE_RING_BUFFER_H_
35
36namespace Divide {
37
39public:
40 // If separateReadWrite is true, this behaves exactly like a RingBuffer
41 explicit RingBufferSeparateWrite(U16 queueLength, bool separateReadWrite, bool writeAhead = true) noexcept;
42 virtual ~RingBufferSeparateWrite() = default;
43
44 virtual void resize(U16 queueLength);
45
46 [[nodiscard]] U16 queueLength() const noexcept
47 {
48 return _queueLength;
49 }
50
51 [[nodiscard]] I32 queueWriteIndex() const noexcept
52 {
53 const I32 ret = _queueIndex.load();
54
56 {
57 return (ret + (_writeAhead ? 1 : to_I32(_queueLength) - 1)) % to_I32(_queueLength);
58 }
59
60 return ret;
61 }
62
63 [[nodiscard]] I32 queueReadIndex() const noexcept
64 {
65 return _queueIndex;
66 }
67
68 I32 incQueue() noexcept
69 {
70 if (queueLength() > 1)
71 {
73 }
74
75 return queueWriteIndex();
76 }
77
78 I32 decQueue() noexcept
79 {
80 if (queueLength() > 1)
81 {
82 if (_queueIndex == 0)
83 {
85 }
86
88 }
89
90 return queueWriteIndex();
91 }
92
93private:
95 const bool _writeAhead = false;
96 const bool _separateReadWrite = false;
97 std::atomic_int _queueIndex = 0;
98};
99
101{
102public:
103 explicit RingBuffer(U16 queueLength) noexcept;
104 virtual ~RingBuffer() = default;
105
106 virtual void resize(U16 queueLength) noexcept;
107
108 [[nodiscard]] U16 queueLength() const noexcept
109 {
110 return _queueLength;
111 }
112
113 [[nodiscard]] I32 queueIndex() const noexcept
114 {
115 return _queueIndex;
116 }
117
118 void incQueue() noexcept
119 {
120 if (queueLength() > 1)
121 {
123 }
124 }
125
126 void decQueue() noexcept
127 {
128 if (queueLength() > 1)
129 {
131 }
132 }
133
134private:
136 std::atomic_int _queueIndex;
137};
138
139} //namespace Divide
140
141#endif //DVD_CORE_RING_BUFFER_H_
std::atomic_int _queueIndex
Definition: RingBuffer.h:136
virtual ~RingBuffer()=default
U16 queueLength() const noexcept
Definition: RingBuffer.h:108
virtual void resize(U16 queueLength) noexcept
Definition: RingBuffer.cpp:27
void decQueue() noexcept
Definition: RingBuffer.h:126
void incQueue() noexcept
Definition: RingBuffer.h:118
I32 queueIndex() const noexcept
Definition: RingBuffer.h:113
I32 queueReadIndex() const noexcept
Definition: RingBuffer.h:63
virtual void resize(U16 queueLength)
Definition: RingBuffer.cpp:14
virtual ~RingBufferSeparateWrite()=default
U16 queueLength() const noexcept
Definition: RingBuffer.h:46
I32 queueWriteIndex() const noexcept
Definition: RingBuffer.h:51
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
int32_t I32
uint16_t U16
constexpr I32 to_I32(const T value)