Embedded Template Library  1.0
 All Classes Files Functions Variables Typedefs Friends Modules Pages
vector_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_IVECTOR_H__
30 #error This header is a private element of etl::vector & etl::ivector
31 #endif
32 
33 #ifndef __ETL_VECTOR_BASE__
34 #define __ETL_VECTOR_BASE__
35 
36 #include <stddef.h>
37 
38 #include "exception.h"
39 
40 #ifndef ETL_THROW_EXCEPTIONS
41 #include "error_handler.h"
42 #endif
43 
44 namespace etl
45 {
46  //***************************************************************************
49  //***************************************************************************
50  class vector_exception : public exception
51  {
52  public:
53 
54  vector_exception(const char* what)
55  : exception(what)
56  {
57  }
58  };
59 
60  //***************************************************************************
63  //***************************************************************************
65  {
66  public:
67 
68  vector_full()
69  : vector_exception("vector: full")
70  {
71  }
72  };
73 
74  //***************************************************************************
77  //***************************************************************************
79  {
80  public:
81 
83  : vector_exception("vector: out of bounds")
84  {
85  }
86  };
87 
88  //***************************************************************************
91  //***************************************************************************
93  {
94  public:
95 
97  : vector_exception("vector: iterator error")
98  {
99  }
100  };
101 
102  //***************************************************************************
105  //***************************************************************************
107  {
108  public:
109 
110  typedef size_t size_type;
111 
112  //*************************************************************************
115  //*************************************************************************
116  size_type size() const
117  {
118  return current_size;
119  }
120 
121  //*************************************************************************
124  //*************************************************************************
125  bool empty() const
126  {
127  return (current_size == 0);
128  }
129 
130  //*************************************************************************
133  //*************************************************************************
134  bool full() const
135  {
136  return current_size == MAX_SIZE;
137  }
138 
139  //*************************************************************************
142  //*************************************************************************
143  size_type capacity() const
144  {
145  return MAX_SIZE;
146  }
147 
148  //*************************************************************************
151  //*************************************************************************
152  size_type max_size() const
153  {
154  return MAX_SIZE;
155  }
156 
157  //*************************************************************************
160  //*************************************************************************
161  size_t available() const
162  {
163  return max_size() - size();
164  }
165 
166  //*************************************************************************
168  //*************************************************************************
169  void clear()
170  {
171  current_size = 0;
172  }
173 
174  //*************************************************************************
177  //*************************************************************************
178  void push_back()
179  {
180 #ifdef ETL_THROW_EXCEPTIONS
181  if (current_size == MAX_SIZE)
182  {
183  throw vector_full();
184  }
185 #else
186  {
187  error_handler::
188  }
189 #endif
190 
191  ++current_size;
192  }
193 
194  //*************************************************************************
197  //*************************************************************************
198  void pop_back()
199  {
200  if (current_size > 0)
201  {
202  --current_size;
203  }
204  }
205 
206  protected:
207 
208  //*************************************************************************
210  //*************************************************************************
212  : current_size(0),
213  MAX_SIZE(max_size)
214  {
215  }
216 
217  size_type current_size;
218  const size_type MAX_SIZE;
219  };
220 }
221 
222 #endif
size_type max_size() const
Definition: vector_base.h:152
void push_back()
Definition: vector_base.h:178
const size_type MAX_SIZE
The maximum number of elements in the vector.
Definition: vector_base.h:218
bool empty() const
Definition: vector_base.h:125
Definition: error_handler.h:46
void clear()
Clears the vector.
Definition: vector_base.h:169
vector_base(size_t max_size)
Constructor.
Definition: vector_base.h:211
exception(value_type reason)
Constructor.
Definition: exception.h:51
Definition: algorithm.h:43
Definition: exception.h:42
size_type capacity() const
Definition: vector_base.h:143
value_type what() const
Definition: exception.h:60
bool full() const
Definition: vector_base.h:134
Definition: vector_base.h:106
void pop_back()
Definition: vector_base.h:198
Definition: vector_base.h:64
size_t available() const
Definition: vector_base.h:161
size_type current_size
The current number of elements in the vector.
Definition: vector_base.h:217
Definition: vector_base.h:50
Definition: vector_base.h:92
Definition: vector_base.h:78
size_type size() const
Definition: vector_base.h:116