µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
os::posix::circular_buffer< T > Class Template Reference

Circular buffer class template.<cmsis-plus/posix-driver/circular-buffer.h> More...

#include <circular-buffer.h>

Public Types

using value_type = T
 Standard type definition.
 

Public Member Functions

Constructors & Destructor
 circular_buffer (const value_type *buf, std::size_t size, std::size_t high_water_mark, std::size_t low_water_mark=0)
 
 circular_buffer (const value_type *buf, std::size_t size)
 
 ~circular_buffer ()
 
Public Member Functions
void clear (void)
 
const value_typeoperator[] (std::size_t idx) const
 
std::size_t push_back (value_type v)
 
std::size_t push_back (const value_type *buf, std::size_t count)
 
std::size_t advance_back (std::size_t count)
 
void retreat_back (void)
 
std::size_t pop_front (value_type *buf)
 
std::size_t pop_front (value_type *buf, std::size_t size)
 
std::size_t advance_front (std::size_t count)
 
std::size_t front_contiguous_buffer (value_type **ppbuf)
 
std::size_t back_contiguous_buffer (value_type **ppbuf)
 
bool empty (void) const
 
bool full (void) const
 
bool above_high_water_mark (void) const
 
bool below_high_water_mark (void) const
 
bool above_low_water_mark (void) const
 
bool below_low_water_mark (void) const
 
std::size_t length (void) const
 
std::size_t size (void) const
 
void dump (void)
 

Detailed Description

template<typename T>
class os::posix::circular_buffer< T >

Definition at line 37 of file circular-buffer.h.

Member Typedef Documentation

◆ value_type

template<typename T >
using os::posix::circular_buffer< T >::value_type = T

Definition at line 45 of file circular-buffer.h.

Constructor & Destructor Documentation

◆ circular_buffer() [1/2]

template<typename T >
os::posix::circular_buffer< T >::circular_buffer ( const value_type buf,
std::size_t  size,
std::size_t  high_water_mark,
std::size_t  low_water_mark = 0 
)

Definition at line 209 of file circular-buffer.h.

213 : buf_ (buf), //
214 size_ (siz), //
215 high_water_mark_ (high_water_mark <= size_ ? high_water_mark
216 : siz), //
217 low_water_mark_ (low_water_mark)
218 {
219 assert (low_water_mark_ <= high_water_mark_);
220
221 clear ();
222 }

References os::posix::circular_buffer< T >::clear().

◆ circular_buffer() [2/2]

template<typename T >
os::posix::circular_buffer< T >::circular_buffer ( const value_type buf,
std::size_t  size 
)

Definition at line 225 of file circular-buffer.h.

227 : circular_buffer{ buf, siz, siz, 0 }
228 {
229 trace::printf ("%s(%p,%u) %p\n", __func__, buf, siz, this);
230 }
circular_buffer(const value_type *buf, std::size_t size, std::size_t high_water_mark, std::size_t low_water_mark=0)
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:59

References os::trace::printf().

◆ ~circular_buffer()

template<typename T >
os::posix::circular_buffer< T >::~circular_buffer

Definition at line 233 of file circular-buffer.h.

234 {
235 trace::printf ("%s() %p\n", __func__, this);
236 }

References os::trace::printf().

Member Function Documentation

◆ above_high_water_mark()

template<typename T >
bool os::posix::circular_buffer< T >::above_high_water_mark ( void  ) const
inline

Definition at line 275 of file circular-buffer.h.

276 {
277 // Allow for water mark to be size.
278 return (len_ >= high_water_mark_);
279 }

◆ above_low_water_mark()

template<typename T >
bool os::posix::circular_buffer< T >::above_low_water_mark ( void  ) const
inline

Definition at line 298 of file circular-buffer.h.

299 {
300 return !below_low_water_mark ();
301 }
bool below_low_water_mark(void) const

◆ advance_back()

template<typename T >
std::size_t os::posix::circular_buffer< T >::advance_back ( std::size_t  count)

Definition at line 381 of file circular-buffer.h.

382 {
383 std::size_t adjust = count;
384 if (count > (size_ - len_))
385 {
386 adjust = size_ - len_;
387 }
388
389 if (adjust == 0)
390 {
391 return 0;
392 }
393
394 back_ += adjust;
395 if (back_ >= (buf_ + size_))
396 {
397 // Wrap.
398 back_ -= size_;
399 }
400 len_ += adjust;
401
402 return adjust;
403 }

◆ advance_front()

template<typename T >
std::size_t os::posix::circular_buffer< T >::advance_front ( std::size_t  count)

Definition at line 481 of file circular-buffer.h.

482 {
483 if (count == 0)
484 {
485 return 0;
486 }
487
488 std::size_t adjust = count;
489 if (adjust > len_)
490 {
491 adjust = len_;
492 }
493
494 front_ += adjust;
495 if (front_ >= (buf_ + size_))
496 {
497 // Wrap.
498 front_ -= size_;
499 }
500 len_ -= adjust;
501
502 return adjust;
503 }

◆ back_contiguous_buffer()

template<typename T >
std::size_t os::posix::circular_buffer< T >::back_contiguous_buffer ( value_type **  ppbuf)

Definition at line 525 of file circular-buffer.h.

526 {
527 assert (ppbuf != nullptr);
528
529 *ppbuf = back_;
530
531 std::size_t sizeToEnd = size_ - static_cast<std::size_t> (back_ - buf_);
532 std::size_t len = sizeToEnd;
533 if (len > (size_ - len_))
534 {
535 len = size_ - len_;
536 }
537
538 return len;
539 }

◆ below_high_water_mark()

template<typename T >
bool os::posix::circular_buffer< T >::below_high_water_mark ( void  ) const
inline

Definition at line 291 of file circular-buffer.h.

292 {
293 return !above_high_water_mark ();
294 }
bool above_high_water_mark(void) const

◆ below_low_water_mark()

template<typename T >
bool os::posix::circular_buffer< T >::below_low_water_mark ( void  ) const
inline

Definition at line 283 of file circular-buffer.h.

284 {
285 // Allow for water mark to be 0.
286 return (len_ <= low_water_mark_);
287 }

◆ clear()

template<typename T >
void os::posix::circular_buffer< T >::clear ( void  )

Definition at line 242 of file circular-buffer.h.

243 {
244 back_ = front_ = const_cast<value_type* volatile> (buf_);
245 len_ = 0;
246#if defined(DEBUG)
247 std::memset (static_cast<void*> (const_cast<value_type*> (buf_)), '?',
248 size_ * sizeof (value_type));
249#endif
250 }
T value_type
Standard type definition.

Referenced by os::posix::circular_buffer< T >::circular_buffer().

◆ dump()

template<typename T >
void os::posix::circular_buffer< T >::dump ( void  )

Definition at line 543 of file circular-buffer.h.

544 {
545 os::trace::printf ("%s @%p {buf=%p, size=%d, len=%d, hwm=%d, lwn=%d}\n",
546 __PRETTY_FUNCTION__, buf_, size_, len_,
547 high_water_mark_, low_water_mark_);
548 }

References os::trace::printf().

◆ empty()

template<typename T >
bool os::posix::circular_buffer< T >::empty ( void  ) const
inline

Definition at line 261 of file circular-buffer.h.

262 {
263 return (len_ == 0);
264 }

◆ front_contiguous_buffer()

template<typename T >
std::size_t os::posix::circular_buffer< T >::front_contiguous_buffer ( value_type **  ppbuf)

Definition at line 507 of file circular-buffer.h.

508 {
509 assert (ppbuf != nullptr);
510
511 *ppbuf = front_;
512
513 std::size_t sizeToEnd = size_ - static_cast<std::size_t> (front_ - buf_);
514 std::size_t len = sizeToEnd;
515 if (len > len_)
516 {
517 len = len_;
518 }
519
520 return len;
521 }

◆ full()

template<typename T >
bool os::posix::circular_buffer< T >::full ( void  ) const
inline

Definition at line 268 of file circular-buffer.h.

269 {
270 return (len_ >= size_);
271 }

◆ length()

template<typename T >
std::size_t os::posix::circular_buffer< T >::length ( void  ) const
inline

Definition at line 305 of file circular-buffer.h.

306 {
307 return len_;
308 }

◆ operator[]()

template<typename T >
const circular_buffer< T >::value_type & os::posix::circular_buffer< T >::operator[] ( std::size_t  idx) const
inline

Definition at line 254 of file circular-buffer.h.

255 {
256 return buf_[idx];
257 }

◆ pop_front() [1/2]

template<typename T >
std::size_t os::posix::circular_buffer< T >::pop_front ( value_type buf)

Definition at line 423 of file circular-buffer.h.

424 {
425 assert (buf != nullptr);
426
427 value_type c;
428 if (len_ == 0)
429 {
430 return 0;
431 }
432 else
433 {
434 c = *front_++;
435 if (static_cast<std::size_t> (front_ - buf_) >= size_)
436 {
437 front_ = const_cast<value_type* volatile> (buf_);
438 }
439 len_--;
440 *buf = c;
441 return 1;
442 }
443 }

◆ pop_front() [2/2]

template<typename T >
std::size_t os::posix::circular_buffer< T >::pop_front ( value_type buf,
std::size_t  size 
)

Definition at line 447 of file circular-buffer.h.

448 {
449 assert (buf != nullptr);
450
451 std::size_t len = siz;
452 if (len > len_)
453 {
454 len = len_;
455 }
456
457 std::size_t sizeToEnd = size_ - static_cast<std::size_t> (front_ - buf_);
458 if (len <= sizeToEnd)
459 {
460 std::memcpy (buf, front_, len);
461 front_ += len;
462 if (static_cast<std::size_t> (front_ - buf_) >= size_)
463 {
464 front_ = const_cast<value_type* volatile> (buf_);
465 }
466 len_ -= len;
467 }
468 else
469 {
470 std::memcpy (buf, front_, sizeToEnd);
471 front_ = const_cast<value_type* volatile> (buf_);
472 std::memcpy (buf + sizeToEnd, front_, len - sizeToEnd);
473 front_ += (len - sizeToEnd);
474 len_ -= len;
475 }
476 return len;
477 }

◆ push_back() [1/2]

template<typename T >
std::size_t os::posix::circular_buffer< T >::push_back ( const value_type buf,
std::size_t  count 
)

Definition at line 340 of file circular-buffer.h.

341 {
342 assert (buf != nullptr);
343
344 std::size_t len = count;
345 if (count > (size_ - len_))
346 {
347 len = size_ - len_;
348 }
349
350 if (len == 0)
351 {
352 return 0;
353 }
354
355 std::size_t sizeToEnd = static_cast<std::size_t> (
356 size_ - static_cast<std::size_t> (back_ - buf_));
357 if (len <= sizeToEnd)
358 {
359 std::memcpy (back_, buf, len);
360 back_ += len;
361 if (static_cast<std::size_t> (back_ - buf_) >= size_)
362 {
363 // Wrap.
364 back_ = const_cast<value_type* volatile> (buf_);
365 }
366 len_ += len;
367 }
368 else
369 {
370 std::memcpy (back_, buf, sizeToEnd);
371 back_ = const_cast<value_type* volatile> (buf_);
372 std::memcpy (back_, buf + sizeToEnd, len - sizeToEnd);
373 back_ += (len - sizeToEnd);
374 len_ += len;
375 }
376 return len;
377 }

◆ push_back() [2/2]

template<typename T >
std::size_t os::posix::circular_buffer< T >::push_back ( value_type  v)

Definition at line 319 of file circular-buffer.h.

320 {
321 if (full ())
322 {
323 return 0;
324 }
325
326 // Add to back.
327 *back_++ = v;
328 if (static_cast<std::size_t> (back_ - buf_) >= size_)
329 {
330 // Wrap.
331 back_ = const_cast<value_type* volatile> (buf_);
332 }
333 len_++;
334 return 1;
335 }

◆ retreat_back()

template<typename T >
void os::posix::circular_buffer< T >::retreat_back ( void  )

Definition at line 407 of file circular-buffer.h.

408 {
409 if (back_ == buf_)
410 {
411 back_ = static_cast<value_type*> (const_cast<value_type*> (buf_)
412 + size_ - 1);
413 }
414 else
415 {
416 back_ -= 1;
417 }
418 len_--;
419 }

◆ size()

template<typename T >
std::size_t os::posix::circular_buffer< T >::size ( void  ) const
inline

Definition at line 312 of file circular-buffer.h.

313 {
314 return size_;
315 }

The documentation for this class was generated from the following file: