3030 * Data is contained in a vector dynamically allocated.
3131 */
3232template <typename T>
33- class RingBuffer {
33+ class RingBuffer
34+ {
3435public:
3536 /* *
3637 * @brief RingBuffer
3738 * @param max maximum number of elements that can be contained in the ring buffer
3839 */
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 ) {}
4041
4142 /* *
4243 * @brief Resets the ring_buffer
4344 * @param max maximum number of elements that can be contained in the ring buffer.
4445 */
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 ;
4850 mWrite = 0 ;
49- mFull = false ;
51+ mFull = false ;
5052 }
5153
5254 /* *
5355 * @brief Current number of elements contained in the ring buffer
5456 * @return
5557 */
56- std::size_t size () const {
58+ std::size_t size () const
59+ {
5760 std::size_t size = capacity ();
58- if (!mFull ) {
59- if (mWrite >= mRead ) {
61+ if ( !mFull )
62+ {
63+ if ( mWrite >= mRead )
64+ {
6065 size = mWrite - mRead ;
61- } else {
66+ }
67+ else
68+ {
6269 size = capacity () + mWrite - mRead ;
6370 }
6471 }
@@ -75,7 +82,7 @@ class RingBuffer {
7582 * @brief whether the ring buffer is empty.
7683 * @return
7784 */
78- bool isEmpty () const { return !isFull () && (mRead == mWrite ); }
85+ bool isEmpty () const { return !isFull () && ( mRead == mWrite ); }
7986
8087 /* *
8188 * @brief Maximum number of elements in the ring buffer
@@ -87,7 +94,8 @@ class RingBuffer {
8794 * @brief Adds a single value
8895 * @param v the value to add
8996 */
90- void put (const T &v) {
97+ void put ( const T& v )
98+ {
9199 mData [mWrite ] = v;
92100 forward ();
93101 }
@@ -97,12 +105,16 @@ class RingBuffer {
97105 * @param v the value read
98106 * @return true if the value was read
99107 */
100- bool get (T&v) {
101- if (!isEmpty ()) {
108+ bool get ( T& v )
109+ {
110+ if ( !isEmpty () )
111+ {
102112 v = mData [mRead ];
103113 backward ();
104114 return true ;
105- } else {
115+ }
116+ else
117+ {
106118 return false ;
107119 }
108120 }
@@ -112,13 +124,15 @@ class RingBuffer {
112124 * @param v pointer to the consecutive values
113125 * @param count number of consecutive values.
114126 */
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 );
122136 }
123137 }
124138
@@ -128,45 +142,57 @@ class RingBuffer {
128142 * @param count Maximum available size in the memory area
129143 * @return actual number of elements read.
130144 */
131- std::size_t get (T *v, std::size_t count) {
145+ std::size_t get ( T* v, std::size_t count )
146+ {
132147 std::size_t avail = 0 ;
133- if (mRead < mWrite ) {
148+ if ( mRead < mWrite )
149+ {
134150 avail = mWrite - mRead ;
135- } else {
151+ }
152+ else
153+ {
136154 avail = mRead - capacity ();
137155 }
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+ {
144165 return to_copy;
145166 }
146167 }
168+
147169private:
148- void forward () {
149- if (isFull ()) {
150- mRead = (mRead + 1 ) % capacity ();
170+ void forward ()
171+ {
172+ if ( isFull () )
173+ {
174+ mRead = ( mRead + 1 ) % capacity ();
151175 }
152- mWrite = (mWrite + 1 ) % capacity ();
153- mFull = (mRead == mWrite );
176+ mWrite = ( mWrite + 1 ) % capacity ();
177+ mFull = ( mRead == mWrite );
154178 }
155179
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+ {
158184 forward ();
159185 }
160186 }
161187
162- void backward (std::size_t count) {
188+ void backward ( std::size_t count )
189+ {
163190 mFull = false ;
164- mRead = (mRead + count) % capacity ();
191+ mRead = ( mRead + count ) % capacity ();
165192 }
166193
167194 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 ;
171198};
172-
0 commit comments