/* The following code declares class array, * an STL container (as wrapper) for arrays of constant size. * * See * http://www.boost.org/libs/array/ * for documentation. * * The original author site is at: http://www.josuttis.com/ * * (C) Copyright Nicolai M. Josuttis 2001. * * Distributed under the Boost Software License, Version 1.0. (See * accompanying file LICENSE_1_0.txt or copy at * http://www.boost.org/LICENSE_1_0.txt) * * 29 Jan 2004 - c_array() added, BOOST_NO_PRIVATE_IN_AGGREGATE removed (Nico Josuttis) * 23 Aug 2002 - fix for Non-MSVC compilers combined with MSVC libraries. * 05 Aug 2001 - minor update (Nico Josuttis) * 20 Jan 2001 - STLport fix (Beman Dawes) * 29 Sep 2000 - Initial Revision (Nico Josuttis) * * Jan 29, 2004 */ #ifndef BOOST_ARRAY_HPP #define BOOST_ARRAY_HPP #include #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400) # pragma warning(push) # pragma warning(disable:4996) // 'std::equal': Function call with parameters that may be unsafe #endif #include #include #include #include // Handles broken standard libraries better than #include #include #include // FIXES for broken compilers #include #ifdef BOOST_ARRAY_INHERIT_ABSTRACT_BASE_CLASS #include #endif #ifdef BOOST_ARRAY_INHERIT_ABSTRACT_BASE_CLASS #define BOOST_ARRAY_VIRTUAL virtual #define BOOST_ARRAY_STATIC #else #define BOOST_ARRAY_VIRTUAL #define BOOST_ARRAY_STATIC static #endif namespace boost { #ifdef BOOST_ARRAY_INHERIT_ABSTRACT_BASE_CLASS template class array : public array_base { #else template class array { #endif public: T elems[N]; // fixed-size array of elements of type T public: // type definitions typedef T value_type; typedef T* iterator; typedef const T* const_iterator; typedef T& reference; typedef const T& const_reference; typedef std::size_t size_type; typedef std::ptrdiff_t difference_type; // iterator support BOOST_ARRAY_VIRTUAL iterator begin() { return elems; } BOOST_ARRAY_VIRTUAL const_iterator begin() const { return elems; } BOOST_ARRAY_VIRTUAL iterator end() { return elems+N; } BOOST_ARRAY_VIRTUAL const_iterator end() const { return elems+N; } // reverse iterator support #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_MSVC_STD_ITERATOR) && !defined(BOOST_NO_STD_ITERATOR_TRAITS) typedef std::reverse_iterator reverse_iterator; typedef std::reverse_iterator const_reverse_iterator; #elif defined(_MSC_VER) && (_MSC_VER == 1300) && defined(BOOST_DINKUMWARE_STDLIB) && (BOOST_DINKUMWARE_STDLIB == 310) // workaround for broken reverse_iterator in VC7 typedef std::reverse_iterator > reverse_iterator; typedef std::reverse_iterator > const_reverse_iterator; #else // workaround for broken reverse_iterator implementations typedef std::reverse_iterator reverse_iterator; typedef std::reverse_iterator const_reverse_iterator; #endif BOOST_ARRAY_VIRTUAL reverse_iterator rbegin() { return reverse_iterator(end()); } BOOST_ARRAY_VIRTUAL const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } BOOST_ARRAY_VIRTUAL reverse_iterator rend() { return reverse_iterator(begin()); } BOOST_ARRAY_VIRTUAL const_reverse_iterator rend() const { return const_reverse_iterator(begin()); } // operator[] BOOST_ARRAY_VIRTUAL reference operator[](size_type i) { BOOST_ASSERT( i < N && "out of range" ); return elems[i]; } BOOST_ARRAY_VIRTUAL const_reference operator[](size_type i) const { BOOST_ASSERT( i < N && "out of range" ); return elems[i]; } // at() with range check BOOST_ARRAY_VIRTUAL reference at(size_type i) { rangecheck(i); return elems[i]; } BOOST_ARRAY_VIRTUAL const_reference at(size_type i) const { rangecheck(i); return elems[i]; } // front() and back() BOOST_ARRAY_VIRTUAL reference front() { return elems[0]; } BOOST_ARRAY_VIRTUAL const_reference front() const { return elems[0]; } BOOST_ARRAY_VIRTUAL reference back() { return elems[N-1]; } BOOST_ARRAY_VIRTUAL const_reference back() const { return elems[N-1]; } #ifdef BOOST_ARRAY_INHERIT_ABSTRACT_BASE_CLASS BOOST_ARRAY_VIRTUAL size_type size() const { return N; } BOOST_ARRAY_VIRTUAL bool empty() const { return false; } BOOST_ARRAY_VIRTUAL size_type max_size() const { return N; } #else // size is constant static size_type size() { return N; } static bool empty() { return false; } static size_type max_size() { return N; } #endif enum { static_size = N }; // swap (note: linear complexity) BOOST_ARRAY_VIRTUAL void swap (array& y) { for (size_type i = 0; i < N; ++i) boost::swap(elems[i],y.elems[i]); } // direct access to data (read-only) BOOST_ARRAY_VIRTUAL const T* data() const { return elems; } BOOST_ARRAY_VIRTUAL T* data() { return elems; } // use array as C array (direct read/write access to data) BOOST_ARRAY_VIRTUAL T* c_array() { return elems; } // assignment with type conversion template array& operator= (const array& rhs) { std::copy(rhs.begin(),rhs.end(), begin()); return *this; } // assign one value to all elements BOOST_ARRAY_VIRTUAL void assign (const T& value) { std::fill_n(begin(),size(),value); } // check range (may be private because it is static) #ifdef BOOST_ARRAY_INHERIT_ABSTRACT_BASE_CLASS virtual void rangecheck (size_type i) const { #else static void rangecheck (size_type i) { #endif if (i >= size()) { std::out_of_range e("array<>: index out of range"); boost::throw_exception(e); } } }; #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) #ifdef BOOST_ARRAY_INHERIT_ABSTRACT_BASE_CLASS template< class T > class array< T, 0 > : public array_base { #else template< class T > class array< T, 0 > { #endif public: // type definitions typedef T value_type; typedef T* iterator; typedef const T* const_iterator; typedef T& reference; typedef const T& const_reference; typedef std::size_t size_type; typedef std::ptrdiff_t difference_type; // iterator support BOOST_ARRAY_VIRTUAL iterator begin() { return iterator( reinterpret_cast< T * >( this ) ); } BOOST_ARRAY_VIRTUAL const_iterator begin() const { return const_iterator( reinterpret_cast< const T * >( this ) ); } BOOST_ARRAY_VIRTUAL iterator end() { return begin(); } BOOST_ARRAY_VIRTUAL const_iterator end() const { return begin(); } // reverse iterator support #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_MSVC_STD_ITERATOR) && !defined(BOOST_NO_STD_ITERATOR_TRAITS) typedef std::reverse_iterator reverse_iterator; typedef std::reverse_iterator const_reverse_iterator; #elif defined(_MSC_VER) && (_MSC_VER == 1300) && defined(BOOST_DINKUMWARE_STDLIB) && (BOOST_DINKUMWARE_STDLIB == 310) // workaround for broken reverse_iterator in VC7 typedef std::reverse_iterator > reverse_iterator; typedef std::reverse_iterator > const_reverse_iterator; #else // workaround for broken reverse_iterator implementations typedef std::reverse_iterator reverse_iterator; typedef std::reverse_iterator const_reverse_iterator; #endif BOOST_ARRAY_VIRTUAL reverse_iterator rbegin() { return reverse_iterator(end()); } BOOST_ARRAY_VIRTUAL const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } BOOST_ARRAY_VIRTUAL reverse_iterator rend() { return reverse_iterator(begin()); } BOOST_ARRAY_VIRTUAL const_reverse_iterator rend() const { return const_reverse_iterator(begin()); } // operator[] BOOST_ARRAY_VIRTUAL reference operator[](size_type /*i*/) { return failed_rangecheck(); } BOOST_ARRAY_VIRTUAL const_reference operator[](size_type /*i*/) const { return failed_rangecheck(); } // at() with range check BOOST_ARRAY_VIRTUAL reference at(size_type /*i*/) { return failed_rangecheck(); } BOOST_ARRAY_VIRTUAL const_reference at(size_type /*i*/) const { return failed_rangecheck(); } // front() and back() BOOST_ARRAY_VIRTUAL reference front() { return failed_rangecheck(); } BOOST_ARRAY_VIRTUAL const_reference front() const { return failed_rangecheck(); } BOOST_ARRAY_VIRTUAL reference back() { return failed_rangecheck(); } BOOST_ARRAY_VIRTUAL const_reference back() const { return failed_rangecheck(); } // size is constant BOOST_ARRAY_VIRTUAL BOOST_ARRAY_STATIC size_type size() { return 0; } BOOST_ARRAY_VIRTUAL BOOST_ARRAY_STATIC bool empty() { return true; } BOOST_ARRAY_VIRTUAL BOOST_ARRAY_STATIC size_type max_size() { return 0; } enum { static_size = 0 }; BOOST_ARRAY_VIRTUAL void swap (array& /*y*/) { } // direct access to data (read-only) BOOST_ARRAY_VIRTUAL const T* data() const { return 0; } BOOST_ARRAY_VIRTUAL T* data() { return 0; } // use array as C array (direct read/write access to data) BOOST_ARRAY_VIRTUAL T* c_array() { return 0; } // assignment with type conversion template array& operator= (const array& ) { return *this; } // assign one value to all elements BOOST_ARRAY_VIRTUAL void assign (const T& ) { } // check range (may be private because it is static) #ifdef BOOST_ARRAY_INHERIT_ABSTRACT_BASE_CLASS virtual reference failed_rangecheck() const { #else static reference failed_rangecheck() { #endif std::out_of_range e("attempt to access element of an empty array"); boost::throw_exception(e); // // We need to return something here to keep // some compilers happy: however we will never // actually get here.... // static T placeholder; return placeholder; } }; // comparisons template bool operator== (const array& x, const array& y) { return std::equal(x.begin(), x.end(), y.begin()); } template bool operator< (const array& x, const array& y) { return std::lexicographical_compare(x.begin(),x.end(),y.begin(),y.end()); } template bool operator!= (const array& x, const array& y) { return !(x==y); } template bool operator> (const array& x, const array& y) { return y bool operator<= (const array& x, const array& y) { return !(y bool operator>= (const array& x, const array& y) { return !(x inline void swap (array& x, array& y) { x.swap(y); } #endif } /* namespace boost */ #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400) # pragma warning(pop) #endif #endif /*BOOST_ARRAY_HPP*/