123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- #pragma once
- #include <stdint.h>
- template <class T, uint8_t SIZE>
- class AP_Buffer {
- public:
-
-
-
- AP_Buffer();
-
-
- void clear();
-
-
-
- void push_back( const T &item );
-
-
-
-
- bool pop_front(T &ret);
-
-
-
-
-
- const T& peek(uint8_t position) const;
- T& peek_mutable(uint8_t position);
-
-
-
- const T& front() const { return this->peek(0); }
-
-
- uint8_t size() const { return _num_items; }
-
-
- bool is_full() const { return _num_items >= SIZE; }
-
-
- bool is_empty() const { return _num_items == 0; }
- private:
- uint8_t _num_items;
- uint8_t _head;
- T _buff[SIZE];
- };
- typedef AP_Buffer<float,5> AP_BufferFloat_Size5;
- typedef AP_Buffer<float,15> AP_BufferFloat_Size15;
- template <class T, uint8_t SIZE>
- AP_Buffer<T,SIZE>::AP_Buffer() :
- _num_items(0), _head(0)
- {
- }
- template <class T, uint8_t SIZE>
- void AP_Buffer<T,SIZE>::clear() {
-
- _num_items = 0;
- _head = 0;
- }
- template <class T, uint8_t SIZE>
- void AP_Buffer<T,SIZE>::push_back( const T &item )
- {
-
- uint8_t tail = _head + _num_items;
- if( tail >= SIZE ) {
- tail -= SIZE;
- }
-
- _buff[tail] = item;
-
- if( _num_items < SIZE ) {
- _num_items++;
- }else{
-
- _head++;
- if( _head >= SIZE ) {
- _head = 0;
- }
- }
- }
- template <class T, uint8_t SIZE>
- bool AP_Buffer<T,SIZE>::pop_front(T &ret)
- {
- if(_num_items == 0) {
-
- return false;
- }
-
- ret = _buff[_head];
-
- _head++;
- if( _head >= SIZE )
- _head = 0;
-
- _num_items--;
-
- return true;
- }
- template <class T, uint8_t SIZE>
- const T& AP_Buffer<T,SIZE>::peek(uint8_t position) const
- {
- uint8_t j = _head + position;
-
- if( j >= SIZE )
- j -= SIZE;
-
- return _buff[j];
- }
- template <class T, uint8_t SIZE>
- T& AP_Buffer<T,SIZE>::peek_mutable(uint8_t position)
- {
- uint8_t j = _head + position;
-
- if( j >= SIZE )
- j -= SIZE;
-
- return _buff[j];
- }
|