1 /* The following code declares class array,
2 * an STL container (as wrapper) for arrays of constant size.
5 * http://www.boost.org/libs/array/
8 * The original author site is at: http://www.josuttis.com/
10 * (C) Copyright Nicolai M. Josuttis 2001.
12 * Distributed under the Boost Software License, Version 1.0. (See
13 * accompanying file LICENSE_1_0.txt or copy at
14 * http://www.boost.org/LICENSE_1_0.txt)
16 * 29 Jan 2004 - c_array() added, BOOST_NO_PRIVATE_IN_AGGREGATE removed (Nico Josuttis)
17 * 23 Aug 2002 - fix for Non-MSVC compilers combined with MSVC libraries.
18 * 05 Aug 2001 - minor update (Nico Josuttis)
19 * 20 Jan 2001 - STLport fix (Beman Dawes)
20 * 29 Sep 2000 - Initial Revision (Nico Josuttis)
24 #ifndef BOOST_ARRAY_HPP
25 #define BOOST_ARRAY_HPP
27 #include <boost/detail/workaround.hpp>
29 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
30 # pragma warning(push)
31 # pragma warning(disable:4996) // 'std::equal': Function call with parameters that may be unsafe
36 #include <boost/assert.hpp>
37 #include <boost/swap.hpp>
39 // Handles broken standard libraries better than <iterator>
40 #include <boost/detail/iterator.hpp>
41 #include <boost/throw_exception.hpp>
44 // FIXES for broken compilers
45 #include <boost/config.hpp>
50 template<class T, std::size_t N>
53 T elems[N]; // fixed-size array of elements of type T
59 typedef const T* const_iterator;
61 typedef const T& const_reference;
62 typedef std::size_t size_type;
63 typedef std::ptrdiff_t difference_type;
66 iterator begin() { return elems; }
67 const_iterator begin() const { return elems; }
68 iterator end() { return elems+N; }
69 const_iterator end() const { return elems+N; }
71 // reverse iterator support
72 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_MSVC_STD_ITERATOR) && !defined(BOOST_NO_STD_ITERATOR_TRAITS)
73 typedef std::reverse_iterator<iterator> reverse_iterator;
74 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
75 #elif defined(_MSC_VER) && (_MSC_VER == 1300) && defined(BOOST_DINKUMWARE_STDLIB) && (BOOST_DINKUMWARE_STDLIB == 310)
76 // workaround for broken reverse_iterator in VC7
77 typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, iterator,
78 reference, iterator, reference> > reverse_iterator;
79 typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, const_iterator,
80 const_reference, iterator, reference> > const_reverse_iterator;
82 // workaround for broken reverse_iterator implementations
83 typedef std::reverse_iterator<iterator,T> reverse_iterator;
84 typedef std::reverse_iterator<const_iterator,T> const_reverse_iterator;
87 reverse_iterator rbegin() { return reverse_iterator(end()); }
88 const_reverse_iterator rbegin() const {
89 return const_reverse_iterator(end());
91 reverse_iterator rend() { return reverse_iterator(begin()); }
92 const_reverse_iterator rend() const {
93 return const_reverse_iterator(begin());
97 reference operator[](size_type i)
99 BOOST_ASSERT( i < N && "out of range" );
103 const_reference operator[](size_type i) const
105 BOOST_ASSERT( i < N && "out of range" );
109 // at() with range check
110 reference at(size_type i) { rangecheck(i); return elems[i]; }
111 const_reference at(size_type i) const { rangecheck(i); return elems[i]; }
113 // front() and back()
119 const_reference front() const
129 const_reference back() const
135 static size_type size() { return N; }
136 static bool empty() { return false; }
137 static size_type max_size() { return N; }
138 enum { static_size = N };
140 // swap (note: linear complexity)
141 void swap (array<T,N>& y) {
142 for (size_type i = 0; i < N; ++i)
143 boost::swap(elems[i],y.elems[i]);
146 // direct access to data (read-only)
147 const T* data() const { return elems; }
148 T* data() { return elems; }
150 // use array as C array (direct read/write access to data)
151 T* c_array() { return elems; }
153 // assignment with type conversion
154 template <typename T2>
155 array<T,N>& operator= (const array<T2,N>& rhs) {
156 std::copy(rhs.begin(),rhs.end(), begin());
160 // assign one value to all elements
161 void assign (const T& value)
163 std::fill_n(begin(),size(),value);
166 // check range (may be private because it is static)
167 static void rangecheck (size_type i) {
169 throw std::out_of_range("array<>: index out of range");
175 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
177 class array< T, 0 > {
181 typedef T value_type;
183 typedef const T* const_iterator;
184 typedef T& reference;
185 typedef const T& const_reference;
186 typedef std::size_t size_type;
187 typedef std::ptrdiff_t difference_type;
190 iterator begin() { return iterator( reinterpret_cast< T * >( this ) ); }
191 const_iterator begin() const { return const_iterator( reinterpret_cast< const T * >( this ) ); }
192 iterator end() { return begin(); }
193 const_iterator end() const { return begin(); }
195 // reverse iterator support
196 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_MSVC_STD_ITERATOR) && !defined(BOOST_NO_STD_ITERATOR_TRAITS)
197 typedef std::reverse_iterator<iterator> reverse_iterator;
198 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
199 #elif defined(_MSC_VER) && (_MSC_VER == 1300) && defined(BOOST_DINKUMWARE_STDLIB) && (BOOST_DINKUMWARE_STDLIB == 310)
200 // workaround for broken reverse_iterator in VC7
201 typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, iterator,
202 reference, iterator, reference> > reverse_iterator;
203 typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, const_iterator,
204 const_reference, iterator, reference> > const_reverse_iterator;
206 // workaround for broken reverse_iterator implementations
207 typedef std::reverse_iterator<iterator,T> reverse_iterator;
208 typedef std::reverse_iterator<const_iterator,T> const_reverse_iterator;
211 reverse_iterator rbegin() { return reverse_iterator(end()); }
212 const_reverse_iterator rbegin() const {
213 return const_reverse_iterator(end());
215 reverse_iterator rend() { return reverse_iterator(begin()); }
216 const_reverse_iterator rend() const {
217 return const_reverse_iterator(begin());
221 reference operator[](size_type /*i*/)
223 return failed_rangecheck();
226 const_reference operator[](size_type /*i*/) const
228 return failed_rangecheck();
231 // at() with range check
232 reference at(size_type /*i*/) { return failed_rangecheck(); }
233 const_reference at(size_type /*i*/) const { return failed_rangecheck(); }
235 // front() and back()
238 return failed_rangecheck();
241 const_reference front() const
243 return failed_rangecheck();
248 return failed_rangecheck();
251 const_reference back() const
253 return failed_rangecheck();
257 static size_type size() { return 0; }
258 static bool empty() { return true; }
259 static size_type max_size() { return 0; }
260 enum { static_size = 0 };
262 void swap (array<T,0>& /*y*/) {
265 // direct access to data (read-only)
266 const T* data() const { return 0; }
267 T* data() { return 0; }
269 // use array as C array (direct read/write access to data)
270 T* c_array() { return 0; }
272 // assignment with type conversion
273 template <typename T2>
274 array<T,0>& operator= (const array<T2,0>& ) {
278 // assign one value to all elements
279 void assign (const T& ) { }
281 // check range (may be private because it is static)
282 static reference failed_rangecheck () {
283 std::out_of_range e("attempt to access element of an empty array");
284 boost::throw_exception(e);
286 // We need to return something here to keep
287 // some compilers happy: however we will never
288 // actually get here....
290 static T placeholder;
297 template<class T, std::size_t N>
298 bool operator== (const array<T,N>& x, const array<T,N>& y) {
299 return std::equal(x.begin(), x.end(), y.begin());
301 template<class T, std::size_t N>
302 bool operator< (const array<T,N>& x, const array<T,N>& y) {
303 return std::lexicographical_compare(x.begin(),x.end(),y.begin(),y.end());
305 template<class T, std::size_t N>
306 bool operator!= (const array<T,N>& x, const array<T,N>& y) {
309 template<class T, std::size_t N>
310 bool operator> (const array<T,N>& x, const array<T,N>& y) {
313 template<class T, std::size_t N>
314 bool operator<= (const array<T,N>& x, const array<T,N>& y) {
317 template<class T, std::size_t N>
318 bool operator>= (const array<T,N>& x, const array<T,N>& y) {
323 template<class T, std::size_t N>
324 inline void swap (array<T,N>& x, array<T,N>& y) {
328 } /* namespace boost */
331 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
332 # pragma warning(pop)
335 #endif /*BOOST_ARRAY_HPP*/