From 0b181dd995c58bde48e2e4d8d090de9387a5379d Mon Sep 17 00:00:00 2001 From: Tobias Date: Tue, 30 Jul 2013 17:56:33 +0200 Subject: [PATCH] AP_Buffer: remove header/source separation to allow arbitrary template parameters without explicit instantiation in the cpp file. http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file --- libraries/AP_Buffer/AP_Buffer.cpp | 95 ------------------------------- libraries/AP_Buffer/AP_Buffer.h | 82 +++++++++++++++++++++++++- 2 files changed, 81 insertions(+), 96 deletions(-) delete mode 100644 libraries/AP_Buffer/AP_Buffer.cpp diff --git a/libraries/AP_Buffer/AP_Buffer.cpp b/libraries/AP_Buffer/AP_Buffer.cpp deleted file mode 100644 index 7e65197654..0000000000 --- a/libraries/AP_Buffer/AP_Buffer.cpp +++ /dev/null @@ -1,95 +0,0 @@ -// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- - -/// @file AP_Buffer.h -/// @brief fifo buffer template class - -#include -#include - -// Constructor -template -AP_Buffer::AP_Buffer() : - _num_items(0), _head(0) -{ -} - -// clear - removes all points from the curve -template -void AP_Buffer::clear() { - // clear the curve - _num_items = 0; - _head = 0; -} - -template -void AP_Buffer::push_back( const T &item ) -{ - // determine position of new item - uint8_t tail = _head + _num_items; - if( tail >= SIZE ) { - tail -= SIZE; - } - - // add item to buffer - _buff[tail] = item; - - // increment number of items - if( _num_items < SIZE ) { - _num_items++; - }else{ - // no room for new items so drop oldest item - _head++; - if( _head >= SIZE ) { - _head = 0; - } - } -} - -template -T AP_Buffer::pop_front() -{ - T result; - - // return zero if buffer is empty - if( _num_items == 0 ) { - return 0; - } - - // get next value in buffer - result = _buff[_head]; - - // increment to next point - _head++; - if( _head >= SIZE ) - _head = 0; - - // reduce number of items - _num_items--; - - // return item - return result; -} - -template -const T& AP_Buffer::peek(uint8_t position) const -{ - uint8_t j = _head + position; - - // return zero if position is out of range - if( position >= _num_items ) { - const static T r = 0; - return r; - } - - // wrap around if necessary - if( j >= SIZE ) - j -= SIZE; - - // return desired value - return _buff[j]; -} - -// explicitly instantiate all needed template instances -// (this is needed to allow the separation of declaration and definition into header and source files) -template class AP_Buffer; -template class AP_Buffer; diff --git a/libraries/AP_Buffer/AP_Buffer.h b/libraries/AP_Buffer/AP_Buffer.h index 393ba9f646..1cc52cc0ff 100644 --- a/libraries/AP_Buffer/AP_Buffer.h +++ b/libraries/AP_Buffer/AP_Buffer.h @@ -62,8 +62,88 @@ private: }; // Typedef for convenience - add more as needed -// (The used template instances have also to be instantiated explicitly in the source file.) typedef AP_Buffer AP_BufferFloat_Size5; typedef AP_Buffer AP_BufferFloat_Size15; +template +AP_Buffer::AP_Buffer() : + _num_items(0), _head(0) +{ +} + +template +void AP_Buffer::clear() { + // clear the curve + _num_items = 0; + _head = 0; +} + +template +void AP_Buffer::push_back( const T &item ) +{ + // determine position of new item + uint8_t tail = _head + _num_items; + if( tail >= SIZE ) { + tail -= SIZE; + } + + // add item to buffer + _buff[tail] = item; + + // increment number of items + if( _num_items < SIZE ) { + _num_items++; + }else{ + // no room for new items so drop oldest item + _head++; + if( _head >= SIZE ) { + _head = 0; + } + } +} + +template +T AP_Buffer::pop_front() +{ + T result; + + // return zero if buffer is empty + if( _num_items == 0 ) { + return 0; + } + + // get next value in buffer + result = _buff[_head]; + + // increment to next point + _head++; + if( _head >= SIZE ) + _head = 0; + + // reduce number of items + _num_items--; + + // return item + return result; +} + +template +const T& AP_Buffer::peek(uint8_t position) const +{ + uint8_t j = _head + position; + + // return zero if position is out of range + if( position >= _num_items ) { + const static T r = 0; + return r; + } + + // wrap around if necessary + if( j >= SIZE ) + j -= SIZE; + + // return desired value + return _buff[j]; +} + #endif // __AP_BUFFER_H__