Embedded Template Library  1.0
 All Classes Files Functions Variables Typedefs Friends Modules Pages
iqueue.h
Go to the documentation of this file.
1 
3 /******************************************************************************
4 The MIT License(MIT)
5 
6 Embedded Template Library.
7 
8 Copyright(c) 2014 jwellbelove
9 
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files(the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions :
16 
17 The above copyright notice and this permission notice shall be included in all
18 copies or substantial portions of the Software.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 SOFTWARE.
27 ******************************************************************************/
28 
29 #ifndef __ETL_IQUEUE__
30 #define __ETL_IQUEUE__
31 #define __ETL_IN_IQUEUE_H__
32 
33 #include <stddef.h>
34 
35 #include "queue_base.h"
36 #include "type_traits.h"
37 #include "parameter_type.h"
38 
39 #ifndef ETL_THROW_EXCEPTIONS
40 #include "error_handler.h"
41 #endif
42 
43 namespace etl
44 {
45  //***************************************************************************
55  //***************************************************************************
56  template <typename T>
57  class iqueue : public queue_base
58  {
59  public:
60 
61  typedef T value_type;
62  typedef T& reference;
63  typedef const T& const_reference;
64  typedef T* pointer;
65  typedef const T* const_pointer;
67 
68  private:
69 
70  typedef typename parameter_type<T>::type parameter_t;
71 
72  public:
73 
74  //*************************************************************************
77  //*************************************************************************
78  reference front()
79  {
80  return buffer[out];
81  }
82 
83  //*************************************************************************
86  //*************************************************************************
87  const_reference front() const
88  {
89  return buffer[out];
90  }
91 
92  //*************************************************************************
95  //*************************************************************************
96  reference back()
97  {
98  return buffer[in == 0 ? MAX_SIZE - 1 : in - 1];
99  }
100 
101  //*************************************************************************
104  //*************************************************************************
105  const_reference back() const
106  {
107  return buffer[in == 0 ? MAX_SIZE - 1 : in - 1];
108  }
109 
110  //*************************************************************************
115  //*************************************************************************
116  void push(parameter_t item)
117  {
118  if (!full())
119  {
120  buffer[in] = item;
121  in = (in == (MAX_SIZE - 1)) ? 0 : in + 1;
122  ++current_size;
123  }
124  else
125 #ifdef ETL_THROW_EXCEPTIONS
126  {
127  throw queue_full();
128  }
129 #else
130  {
132  }
133 #endif
134  }
135 
136  //*************************************************************************
143  //*************************************************************************
144  reference push()
145  {
146  const size_type next = in;
147 
148  if (!full())
149  {
150  in = (in == (MAX_SIZE - 1)) ? 0 : in + 1;
151  ++current_size;
152  }
153  else
154 #ifdef ETL_THROW_EXCEPTIONS
155  {
156  throw queue_full();
157  }
158 #else
159  {
161  }
162 #endif
163 
164  return buffer[next];
165  }
166 
167  protected:
168 
169  //*************************************************************************
171  //*************************************************************************
172  iqueue(T* buffer, size_type max_size)
173  : queue_base(max_size),
174  buffer(buffer)
175  {
176  }
177 
178  private:
179 
180  T* buffer;
181  };
182 }
183 
184 #undef __etl_in_iqueue_h__
185 #endif
const T & const_reference
A const reference to the type used in the queue.
Definition: iqueue.h:63
size_t size_type
The type used for determining the size of queue.
Definition: queue_base.h:78
size_type out
Where to get the oldest data.
Definition: queue_base.h:166
reference push()
Definition: iqueue.h:144
iqueue(T *buffer, size_type max_size)
The constructor that is called from derived classes.
Definition: iqueue.h:172
queue_base::size_type size_type
The type used for determining the size of the queue.
Definition: iqueue.h:66
const_reference back() const
Definition: iqueue.h:105
const size_type MAX_SIZE
The maximum number of items in the queue.
Definition: queue_base.h:168
Determine how to pass parameters.
Definition: parameter_type.h:40
size_type current_size
The number of items in the queue.
Definition: queue_base.h:167
void push(parameter_t item)
Definition: iqueue.h:116
size_type in
Where to input new data.
Definition: queue_base.h:165
Definition: algorithm.h:43
reference front()
Definition: iqueue.h:78
Definition: queue_base.h:60
reference back()
Definition: iqueue.h:96
T & reference
A reference to the type used in the queue.
Definition: iqueue.h:62
TIterator next(TIterator iterator, ptrdiff_t n=1)
Definition: container.h:245
This is the base for all queues that contain a particular type.
Definition: iqueue.h:57
static void error(const exception &e)
Definition: error_handler.cpp:50
size_type max_size() const
Returns the maximum number of items that can be queued.
Definition: queue_base.h:99
T value_type
The type stored in the queue.
Definition: iqueue.h:61
const_reference front() const
Definition: iqueue.h:87
const T * const_pointer
A const pointer to the type used in the qu.
Definition: iqueue.h:65
bool full() const
Definition: queue_base.h:117
T * pointer
A pointer to the type used in the queue.
Definition: iqueue.h:64
Definition: queue_base.h:74