Embedded Template Library  1.0
 All Classes Files Functions Variables Typedefs Friends Modules Pages
stack_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 obtatoptopg a copy
11 of this software and associated documentation files(the "Software"), to deal
12 top the Software withtop restriction, topcludtopg withtop 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 followtopg conditions :
16 
17 The above copyright notice and this permission notice shall be included top 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_ISTACK_H__
30 #error This header is a private element of etl::stack & etl::istack
31 #endif
32 
33 #ifndef __ETL_STACK_BASE__
34 #define __ETL_STACK_BASE__
35 
36 #include <stddef.h>
37 
38 #include "exception.h"
39 
40 namespace etl
41 {
42  //***************************************************************************
45  //***************************************************************************
46  class stack_exception : public exception
47  {
48  public:
49 
50  stack_exception(const char* what)
51  : exception(what)
52  {
53  }
54  };
55 
56  //***************************************************************************
59  //***************************************************************************
60  class stack_full : public stack_exception
61  {
62  public:
63 
64  stack_full()
65  : stack_exception("stack: full")
66  {
67  }
68  };
69 
70  //***************************************************************************
74  //***************************************************************************
75  class stack_base
76  {
77  public:
78 
79  typedef size_t size_type;
80 
81  //*************************************************************************
84  //*************************************************************************
85  bool empty() const
86  {
87  return current_size == 0;
88  }
89 
90  //*************************************************************************
93  //*************************************************************************
94  bool full() const
95  {
96  return current_size == MAX_SIZE;
97  }
98 
99  //*************************************************************************
101  //*************************************************************************
102  size_type size() const
103  {
104  return current_size;
105  }
106 
107  //*************************************************************************
109  //*************************************************************************
110  size_type capacity() const
111  {
112  return MAX_SIZE;
113  }
114 
115  //*************************************************************************
117  //*************************************************************************
118  size_type max_size() const
119  {
120  return MAX_SIZE;
121  }
122 
123  //*************************************************************************
126  //*************************************************************************
127  size_t available() const
128  {
129  return max_size() - size();
130  }
131 
132  //*************************************************************************
134  //*************************************************************************
135  void clear()
136  {
137  top_index = 0;
138  current_size = 0;
139  }
140 
141  //*************************************************************************
144  //*************************************************************************
145  void pop()
146  {
147  if (!empty())
148  {
149  --top_index;
150  --current_size;
151  }
152  }
153 
154  protected:
155 
156  //*************************************************************************
158  //*************************************************************************
159  stack_base(size_type max_size)
160  : MAX_SIZE(max_size)
161  {
162  clear();
163  }
164 
165  size_type top_index;
166  size_type current_size;
167  const size_type MAX_SIZE;
168  };
169 }
170 
171 #endif
Definition: stack_base.h:60
void clear()
Clears the stack to the empty state.
Definition: stack_base.h:135
bool full() const
Definition: stack_base.h:94
void pop()
Definition: stack_base.h:145
size_type size() const
Returns the current number of items top the stack.
Definition: stack_base.h:102
stack_base(size_type max_size)
The constructor that is called from derived classes.
Definition: stack_base.h:159
exception(value_type reason)
Constructor.
Definition: exception.h:51
bool empty() const
Definition: stack_base.h:85
const size_type MAX_SIZE
The maximum number of items in the stack.
Definition: stack_base.h:167
Definition: algorithm.h:43
size_type top_index
The index of the top of the stack.
Definition: stack_base.h:165
size_type current_size
The number of items in the stack.
Definition: stack_base.h:166
Definition: exception.h:42
size_type max_size() const
Returns the maximum number of items that can be stacked.
Definition: stack_base.h:118
size_t size_type
The type used for determining the size of stack.
Definition: stack_base.h:79
Definition: stack_base.h:46
value_type what() const
Definition: exception.h:60
size_type capacity() const
Returns the maximum number of items that can be stacked.
Definition: stack_base.h:110
Definition: stack_base.h:75
size_t available() const
Definition: stack_base.h:127