Embedded Template Library  1.0
 All Classes Files Functions Variables Typedefs Friends Modules Pages
queue_base.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_IN_IQUEUE_H__
30 #error This header is a private element of etl::queue & etl::iqueue
31 #endif
32 
33 #ifndef __ETL_QUEUE_BASE__
34 #define __ETL_QUEUE_BASE__
35 
36 #include <stddef.h>
37 
38 #include "exception.h"
39 
40 namespace etl
41 {
42  //***************************************************************************
45  //***************************************************************************
46  class queue_exception : public exception
47  {
48  public:
49 
50  queue_exception(const char* what)
51  : exception(what)
52  {
53  }
54  };
55 
56  //***************************************************************************
59  //***************************************************************************
60  class queue_full : public queue_exception
61  {
62  public:
63 
64  queue_full()
65  : queue_exception("queue: full")
66  {
67  }
68  };
69 
70  //***************************************************************************
73  //***************************************************************************
74  class queue_base
75  {
76  public:
77 
78  typedef size_t size_type;
79 
80  //*************************************************************************
82  //*************************************************************************
83  size_type size() const
84  {
85  return current_size;
86  }
87 
88  //*************************************************************************
90  //*************************************************************************
91  size_type capacity() const
92  {
93  return MAX_SIZE;
94  }
95 
96  //*************************************************************************
98  //*************************************************************************
99  size_type max_size() const
100  {
101  return MAX_SIZE;
102  }
103 
104  //*************************************************************************
107  //*************************************************************************
108  bool empty() const
109  {
110  return current_size == 0;
111  }
112 
113  //*************************************************************************
116  //*************************************************************************
117  bool full() const
118  {
119  return current_size == MAX_SIZE;
120  }
121 
122  //*************************************************************************
125  //*************************************************************************
126  size_t available() const
127  {
128  return max_size() - size();
129  }
130 
131  //*************************************************************************
133  //*************************************************************************
134  void clear()
135  {
136  in = 0;
137  out = 0;
138  current_size = 0;
139  }
140 
141  //*************************************************************************
144  //*************************************************************************
145  void pop()
146  {
147  if (!empty())
148  {
149  out = (out == (MAX_SIZE - 1)) ? 0 : out + 1;
150  --current_size;
151  }
152  }
153 
154  protected:
155 
156  //*************************************************************************
158  //*************************************************************************
159  queue_base(size_type max_size)
160  : MAX_SIZE(max_size)
161  {
162  clear();
163  }
164 
165  size_type in;
166  size_type out;
167  size_type current_size;
168  const size_type MAX_SIZE;
169  };
170 }
171 
172 #endif
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
queue_base(size_type max_size)
The constructor that is called from derived classes.
Definition: queue_base.h:159
const size_type MAX_SIZE
The maximum number of items in the queue.
Definition: queue_base.h:168
size_type current_size
The number of items in the queue.
Definition: queue_base.h:167
exception(value_type reason)
Constructor.
Definition: exception.h:51
size_type capacity() const
Returns the maximum number of items that can be queued.
Definition: queue_base.h:91
size_type in
Where to input new data.
Definition: queue_base.h:165
Definition: queue_base.h:46
Definition: algorithm.h:43
Definition: queue_base.h:60
size_type size() const
Returns the current number of items in the queue.
Definition: queue_base.h:83
void clear()
Clears the queue to the empty state.
Definition: queue_base.h:134
void pop()
Definition: queue_base.h:145
Definition: exception.h:42
value_type what() const
Definition: exception.h:60
size_type max_size() const
Returns the maximum number of items that can be queued.
Definition: queue_base.h:99
size_t available() const
Definition: queue_base.h:126
bool full() const
Definition: queue_base.h:117
bool empty() const
Definition: queue_base.h:108
Definition: queue_base.h:74