From dc5ab85e286447c91216e2f6dc5c60d8210fe2b9 Mon Sep 17 00:00:00 2001 From: jwellbelove Date: Mon, 27 Oct 2014 11:30:10 +0000 Subject: [PATCH] Added back() & #error for base class --- iqueue.h | 49 ++++++++++++++++++++++++++++++++++++++++++++----- queue.h | 2 +- queue_base.h | 24 +++++++++++++----------- 3 files changed, 58 insertions(+), 17 deletions(-) diff --git a/iqueue.h b/iqueue.h index a7f1948b..da769c1d 100644 --- a/iqueue.h +++ b/iqueue.h @@ -28,6 +28,7 @@ SOFTWARE. #ifndef __etl_iqueue__ #define __etl_iqueue__ +#define __etl_in_iqueue_h__ #include @@ -51,7 +52,7 @@ namespace etl { public: - typedef queue_base::size_type size_type; ///< The type used for determining the size of queue. + typedef queue_base::size_type size_type; ///< The type used for determining the size of the queue. typedef T value_type; ///< The type stored in the queue. typedef T& reference; ///< A reference to the type used in the queue. typedef const T& const_reference; ///< A const reference to the type used in the queue. @@ -68,9 +69,9 @@ namespace etl { if (!full()) { - buffer[in++] = item; + buffer[in] = item; in = (in == (MAX_SIZE - 1)) ? 0 : ++in; - ++size; + ++current_size; } #ifdef ETL_USE_EXCEPTIONS else @@ -95,7 +96,7 @@ namespace etl if (!full()) { in = (in == (MAX_SIZE - 1)) ? 0 : ++in; - ++size; + ++current_size; } #ifdef ETL_USE_EXCEPTIONS else @@ -143,12 +144,48 @@ namespace etl return buffer[out]; } + //************************************************************************* + /// Gets a reference to the item at the back of the queue.
+ /// If ETL_USE_EXCEPTIONS is defined, throws an etl::queue_empty_exception if the queue is empty.
+ /// If ETL_USE_EXCEPTIONS is not defined and the queue is empty, the return value is undefined. + /// \return A reference to the item at the back of the queue. + //************************************************************************* + reference back() + { +#ifdef ETL_USE_EXCEPTIONS + if (empty()) + { + throw queue_empty_exception(); + } +#endif + + return buffer[in == 0 ? MAX_SIZE - 1 : in - 1]; + } + + //************************************************************************* + /// Gets a const reference to the item at the back of the queue.
+ /// If ETL_USE_EXCEPTIONS is defined, throws an etl::queue_empty_exception if the queue is empty.
+ /// If ETL_USE_EXCEPTIONS is not defined and the queue is empty, the return value is undefined. + /// \return A const reference to the item at the back of the queue. + //************************************************************************* + const_reference back() const + { +#ifdef ETL_USE_EXCEPTIONS + if (empty()) + { + throw queue_empty_exception(); + } +#endif + + return buffer[in == 0 ? MAX_SIZE - 1 : in - 1]; + } + protected: //************************************************************************* /// The constructor that is called from derived classes. //************************************************************************* - queue(T* buffer, size_type max_size) + iqueue(T* buffer, size_type max_size) : queue_base(max_size), buffer(buffer) { @@ -160,3 +197,5 @@ namespace etl }; } +#undef __etl_in_iqueue_h__ +#endif diff --git a/queue.h b/queue.h index d26744a4..e5525d4e 100644 --- a/queue.h +++ b/queue.h @@ -31,7 +31,7 @@ SOFTWARE. #include -#include "IQueue.h" +#include "iqueue.h" //***************************************************************************** ///\defgroup queue queue diff --git a/queue_base.h b/queue_base.h index 69835263..4218b5e7 100644 --- a/queue_base.h +++ b/queue_base.h @@ -26,6 +26,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ******************************************************************************/ +#ifndef __etl_in_iqueue_h__ +#error This header is a private element of etl::queue & etl::iqueue +#endif + #ifndef __etl_queue_base__ #define __etl_queue_base__ @@ -33,9 +37,6 @@ SOFTWARE. #include "exception.h" -///\defgroup queue Queue -///\ingroup containers - namespace etl { #ifdef ETL_USE_EXCEPTIONS @@ -98,7 +99,7 @@ namespace etl //************************************************************************* size_type size() const { - return size; + return current_size; } //************************************************************************* @@ -122,9 +123,9 @@ namespace etl //************************************************************************* void clear() { - in = 0; - out = 0; - size = 0; + in = 0; + out = 0; + current_size = 0; } //************************************************************************* @@ -133,7 +134,7 @@ namespace etl //************************************************************************* bool empty() const { - return size == 0; + return current_size == 0; } //************************************************************************* @@ -142,7 +143,7 @@ namespace etl //************************************************************************* bool full() const { - return size == MAX_SIZE; + return current_size == MAX_SIZE; } //************************************************************************* @@ -154,7 +155,7 @@ namespace etl if (!empty()) { out = (out == (MAX_SIZE - 1)) ? 0 : ++out; - --size; + --current_size; } } @@ -171,8 +172,9 @@ namespace etl size_type in; ///< Where to input new data. size_type out; ///< Where to get the oldest data. - size_type size; ///< The number of items in the queue. + size_type current_size; ///< The number of items in the queue. const size_type MAX_SIZE; ///< The maximum number of items in the queue. }; } +#endif