30
30
* Data is contained in a vector dynamically allocated.
31
31
*/
32
32
template <typename T>
33
- class RingBuffer {
33
+ class RingBuffer
34
+ {
34
35
public:
35
36
/* *
36
37
* @brief RingBuffer
37
38
* @param max maximum number of elements that can be contained in the ring buffer
38
39
*/
39
- RingBuffer ( std::size_t max = 0 ): mData ( max), mRead ( 0 ), mWrite ( 0 ), mFull ( false ) { }
40
+ RingBuffer ( std::size_t max = 0 ) : mData ( max ), mRead ( 0 ), mWrite ( 0 ), mFull ( false ) {}
40
41
41
42
/* *
42
43
* @brief Resets the ring_buffer
43
44
* @param max maximum number of elements that can be contained in the ring buffer.
44
45
*/
45
- void reset (std::size_t max = 0 ) {
46
- mData = std::vector<T>(max);
47
- mRead = 0 ;
46
+ void reset ( std::size_t max = 0 )
47
+ {
48
+ mData = std::vector<T> ( max );
49
+ mRead = 0 ;
48
50
mWrite = 0 ;
49
- mFull = false ;
51
+ mFull = false ;
50
52
}
51
53
52
54
/* *
53
55
* @brief Current number of elements contained in the ring buffer
54
56
* @return
55
57
*/
56
- std::size_t size () const {
58
+ std::size_t size () const
59
+ {
57
60
std::size_t size = capacity ();
58
- if (!mFull ) {
59
- if (mWrite >= mRead ) {
61
+ if ( !mFull )
62
+ {
63
+ if ( mWrite >= mRead )
64
+ {
60
65
size = mWrite - mRead ;
61
- } else {
66
+ }
67
+ else
68
+ {
62
69
size = capacity () + mWrite - mRead ;
63
70
}
64
71
}
@@ -75,7 +82,7 @@ class RingBuffer {
75
82
* @brief whether the ring buffer is empty.
76
83
* @return
77
84
*/
78
- bool isEmpty () const { return !isFull () && (mRead == mWrite ); }
85
+ bool isEmpty () const { return !isFull () && ( mRead == mWrite ); }
79
86
80
87
/* *
81
88
* @brief Maximum number of elements in the ring buffer
@@ -87,7 +94,8 @@ class RingBuffer {
87
94
* @brief Adds a single value
88
95
* @param v the value to add
89
96
*/
90
- void put (const T &v) {
97
+ void put ( const T& v )
98
+ {
91
99
mData [mWrite ] = v;
92
100
forward ();
93
101
}
@@ -97,12 +105,16 @@ class RingBuffer {
97
105
* @param v the value read
98
106
* @return true if the value was read
99
107
*/
100
- bool get (T&v) {
101
- if (!isEmpty ()) {
108
+ bool get ( T& v )
109
+ {
110
+ if ( !isEmpty () )
111
+ {
102
112
v = mData [mRead ];
103
113
backward ();
104
114
return true ;
105
- } else {
115
+ }
116
+ else
117
+ {
106
118
return false ;
107
119
}
108
120
}
@@ -112,13 +124,15 @@ class RingBuffer {
112
124
* @param v pointer to the consecutive values
113
125
* @param count number of consecutive values.
114
126
*/
115
- void put (const T *v, std::size_t count) {
116
- std::size_t avail = mWrite - capacity ();
117
- std::size_t to_copy = std::min (count,avail);
118
- memcpy (mData .data () + mWrite ,v, to_copy*sizeof (T));
119
- forward (to_copy);
120
- if (to_copy < count) {
121
- put (v+to_copy,count - to_copy);
127
+ void put ( const T* v, std::size_t count )
128
+ {
129
+ std::size_t avail = mWrite - capacity ();
130
+ std::size_t to_copy = std::min ( count, avail );
131
+ memcpy ( mData .data () + mWrite , v, to_copy * sizeof ( T ) );
132
+ forward ( to_copy );
133
+ if ( to_copy < count )
134
+ {
135
+ put ( v + to_copy, count - to_copy );
122
136
}
123
137
}
124
138
@@ -128,45 +142,57 @@ class RingBuffer {
128
142
* @param count Maximum available size in the memory area
129
143
* @return actual number of elements read.
130
144
*/
131
- std::size_t get (T *v, std::size_t count) {
145
+ std::size_t get ( T* v, std::size_t count )
146
+ {
132
147
std::size_t avail = 0 ;
133
- if (mRead < mWrite ) {
148
+ if ( mRead < mWrite )
149
+ {
134
150
avail = mWrite - mRead ;
135
- } else {
151
+ }
152
+ else
153
+ {
136
154
avail = mRead - capacity ();
137
155
}
138
- std::size_t to_copy = std::min (count, avail);
139
- memcpy (v, mData .data () + mRead , to_copy * sizeof (T));
140
- backward (to_copy);
141
- if ((size ()>0 )&&(count > to_copy)) {
142
- return to_copy + get (v + to_copy, count - to_copy);
143
- } else {
156
+ std::size_t to_copy = std::min ( count, avail );
157
+ memcpy ( v, mData .data () + mRead , to_copy * sizeof ( T ) );
158
+ backward ( to_copy );
159
+ if ( ( size () > 0 ) && ( count > to_copy ) )
160
+ {
161
+ return to_copy + get ( v + to_copy, count - to_copy );
162
+ }
163
+ else
164
+ {
144
165
return to_copy;
145
166
}
146
167
}
168
+
147
169
private:
148
- void forward () {
149
- if (isFull ()) {
150
- mRead = (mRead + 1 ) % capacity ();
170
+ void forward ()
171
+ {
172
+ if ( isFull () )
173
+ {
174
+ mRead = ( mRead + 1 ) % capacity ();
151
175
}
152
- mWrite = (mWrite + 1 ) % capacity ();
153
- mFull = (mRead == mWrite );
176
+ mWrite = ( mWrite + 1 ) % capacity ();
177
+ mFull = ( mRead == mWrite );
154
178
}
155
179
156
- void forward (std::size_t count) {
157
- for (std::size_t i=0 ; i<count;i++) {
180
+ void forward ( std::size_t count )
181
+ {
182
+ for ( std::size_t i = 0 ; i < count; i++ )
183
+ {
158
184
forward ();
159
185
}
160
186
}
161
187
162
- void backward (std::size_t count) {
188
+ void backward ( std::size_t count )
189
+ {
163
190
mFull = false ;
164
- mRead = (mRead + count) % capacity ();
191
+ mRead = ( mRead + count ) % capacity ();
165
192
}
166
193
167
194
std::vector<T> mData ;
168
- std::size_t mRead ; /* * offset to reading point */
169
- std::size_t mWrite ; /* * offset to writing point */
170
- bool mFull ;
195
+ std::size_t mRead ; /* * offset to reading point */
196
+ std::size_t mWrite ; /* * offset to writing point */
197
+ bool mFull ;
171
198
};
172
-
0 commit comments