Library of Assembled Shared Sources
fixed_array.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2024 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44
45/** @struct lass::util::FixedArray
46 * @brief compile time fixed array that also supports zero length
47 * @author Bram de Greve [Bramz]
48 * @date 2002-2003
49 *
50 * FixedArray is an array of compile-time-fixed length. The unique property of this implementation
51 * is that this length can also be zero. As a surplus, this class is given an STL container
52 * interface with iterators etc.
53 */
54
55
56
57#ifndef LASS_GUARDIAN_OF_INCLUSION_UTIL_FIXED_ARRAY_H
58#define LASS_GUARDIAN_OF_INCLUSION_UTIL_FIXED_ARRAY_H
59
60// --- OLD INTERFACES ----------------------------------------------------------
61
62#include "util_common.h"
63#include <cstddef>
64#include <iterator>
65
66
67
68// --- NEW INTERFACES ----------------------------------------------------------
69
70namespace lass
71{
72namespace util
73{
74
75template <typename T, size_t size_>
77{
78public:
79
80 // type definitions
81
82 typedef T value_type;
83 typedef value_type& reference;
84 typedef const value_type& const_reference;
85 typedef value_type* pointer;
86 typedef const value_type* const_pointer;
87 typedef size_t size_type;
88 typedef std::ptrdiff_t difference_type;
89 typedef value_type* iterator;
90 typedef const value_type* const_iterator;
91 typedef std::reverse_iterator<iterator> reverse_iterator;
92 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
93
94
95 // iterator support
96
97 iterator begin() { return array_; }
98 iterator end() { return array_ + size_; }
99
100 const_iterator begin() const { return array_; }
101 const_iterator end() const { return array_ + size_; }
102
103 reverse_iterator rbegin() { return reverse_iterator(end()); }
104 reverse_iterator rend() { return reverse_iterator(begin()); }
105
106 const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
107 const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
108
109
110 // direct element access
111
112 reference operator[](size_type iIndex)
113 {
114 LASS_ASSERT(iIndex < size_);
115 return array_[iIndex];
116 }
117
118 const_reference operator[](size_type iIndex) const
119 {
120 LASS_ASSERT(iIndex < size_);
121 return array_[iIndex];
122 }
123
124 reference at(size_type iIndex)
125 {
126 if (iIndex >= size_)
127 {
128 throw std::out_of_range("index out of range in lass::util::FixedArray::at");
129 }
130 return array_[iIndex];
131 }
132
133 const_reference at(size_type iIndex) const
134 {
135 if (iIndex >= size_)
136 {
137 throw std::out_of_range("index out of range in lass::util::FixedArray::at");
138 }
139 return array_[iIndex];
140 }
141
142 reference front() { return array_[0]; }
143 reference back() { return array_[size_ - 1]; }
144
145 const_reference front() const { return array_[0]; }
146 const_reference back() const { return array_[size_ - 1]; }
147
148
149 // general
150
151 size_type size() const { return size_; }
152 size_type max_size() const { return size_; }
153 bool empty() const { return false; }
154
155private:
156
157 value_type array_[size_];
158};
159
160
161
162template <typename T>
163 class FixedArray<T, 0>
164{
165public:
166
167 // type definitions
168
169 typedef T value_type;
170 typedef value_type& reference;
171 typedef const value_type& const_reference;
172 typedef value_type* pointer;
173 typedef const value_type* const_pointer;
174 typedef size_t size_type;
175 typedef std::ptrdiff_t difference_type;
176 typedef value_type* iterator;
177 typedef const value_type* const_iterator;
178 typedef std::reverse_iterator<iterator> reverse_iterator;
179 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
180
181
182 // iterator support
183
184 iterator begin() { return 0; }
185 iterator end() { return 0; }
186
187 const_iterator begin() const { return 0; }
188 const_iterator end() const { return 0; }
189
190 reverse_iterator rbegin() { return reverse_iterator(end()); }
191 reverse_iterator rend() { return reverse_iterator(begin()); }
192
193 const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
194 const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
195
196
197 // direct element access
198
199 reference operator[](size_type)
200 {
201 LASS_ASSERT(false);
202 return *begin();
203 }
204
205 const_reference operator[](size_type) const
206 {
207 LASS_ASSERT(false);
208 return *begin();
209 }
210
211 reference at(size_type)
212 {
213 throw std::out_of_range("index out of range in lass::util::FixedArray::at");
214 }
215
216 const_reference at(size_type) const
217 {
218 throw std::out_of_range("index out of range in lass::util::FixedArray::at");
219 }
220
221 reference front() { return *begin(); }
222 reference back() { return *begin(); }
223
224 const_reference front() const { return *begin(); }
225 const_reference back() const { return *begin(); }
226
227
228 // general
229
230 size_type size() const { return 0; }
231 size_type max_size() const { return 0; }
232 bool empty() const { return true; }
233};
234
235
236}
237
238}
239
240#endif
241
242// --- END OF FILE -------------------------------------------------------------
compile time fixed array that also supports zero length
Definition fixed_array.h:77
general utility, debug facilities, ...
Library for Assembled Shared Sources.
Definition config.h:53