library of assembled shared sources

http://lass.cocamware.com

fixed_array.h

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 
00044 
00045 /** @struct lass::util::FixedArray
00046  *  @brief compile time fixed array that also supports zero length
00047  *  @author Bram de Greve [Bramz]
00048  *  @date 2002-2003
00049  *
00050  *  FixedArray is an array of compile-time-fixed length.  The unique property of this implementation
00051  *  is that this length can also be zero.  As a surplus, this class is given an STL container
00052  *  interface with iterators etc.
00053  */
00054 
00055 
00056 
00057 #ifndef LASS_GUARDIAN_OF_INCLUSION_UTIL_FIXED_ARRAY_H
00058 #define LASS_GUARDIAN_OF_INCLUSION_UTIL_FIXED_ARRAY_H
00059 
00060 // --- OLD INTERFACES ----------------------------------------------------------
00061 
00062 #include "util_common.h"
00063 #include <cstddef>
00064 #include <iterator>
00065 
00066 
00067 
00068 // --- NEW INTERFACES ----------------------------------------------------------
00069 
00070 namespace lass
00071 {
00072 namespace util
00073 {
00074 
00075 template <typename T, size_t size_>
00076 class FixedArray
00077 {
00078 public:
00079 
00080     // type definitions
00081 
00082     typedef T value_type;
00083     typedef value_type& reference;
00084     typedef const value_type& const_reference;
00085     typedef value_type* pointer;
00086     typedef const value_type* const_pointer;
00087     typedef size_t size_type;
00088     typedef ptrdiff_t difference_type;
00089     typedef value_type* iterator;
00090     typedef const value_type* const_iterator;
00091     typedef std::reverse_iterator<iterator> reverse_iterator;
00092     typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00093 
00094 
00095     // iterator support
00096 
00097     iterator begin() { return array_; }
00098     iterator end() { return array_ + size_; }
00099 
00100     const_iterator begin() const { return array_; }
00101     const_iterator end() const { return array_ + size_; }
00102 
00103     reverse_iterator rbegin() { return reverse_iterator(end()); }
00104     reverse_iterator rend() { return reverse_iterator(begin()); }
00105 
00106     const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
00107     const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
00108 
00109 
00110     // direct element access
00111 
00112     reference operator[](size_type iIndex)
00113     {
00114         LASS_ASSERT(iIndex < size_);
00115         return array_[iIndex];
00116     }
00117 
00118     const_reference operator[](size_type iIndex) const
00119     {
00120         LASS_ASSERT(iIndex < size_);
00121         return array_[iIndex];
00122     }
00123 
00124     reference at(size_type iIndex) 
00125     {
00126         if (iIndex >= size_)
00127         {
00128             throw std::out_of_range("index out of range in lass::util::FixedArray::at");
00129         }
00130         return array_[iIndex];
00131     }
00132 
00133     const_reference at(size_type iIndex) const
00134     {
00135         if (iIndex >= size_)
00136         {
00137             throw std::out_of_range("index out of range in lass::util::FixedArray::at");
00138         }
00139         return array_[iIndex];
00140     }
00141 
00142     reference front() { return array_[0]; }
00143     reference back() { return array_[size_ - 1]; }
00144 
00145     const_reference front() const { return array_[0]; }
00146     const_reference back() const { return array_[size_ - 1]; }
00147 
00148 
00149     // general
00150 
00151     size_type size() const { return size_; }
00152     size_type max_size() const { return size_; }
00153     bool empty() const { return false; }
00154 
00155 private:
00156 
00157     value_type array_[size_];
00158 };
00159 
00160 
00161 
00162 template <typename T>
00163   class FixedArray<T, 0>
00164 {
00165 public:
00166 
00167     // type definitions
00168 
00169     typedef T value_type;
00170     typedef value_type& reference;
00171     typedef const value_type& const_reference;
00172     typedef value_type* pointer;
00173     typedef const value_type* const_pointer;
00174     typedef size_t size_type;
00175     typedef ptrdiff_t difference_type;
00176     typedef value_type* iterator;
00177     typedef const value_type* const_iterator;
00178     typedef std::reverse_iterator<iterator> reverse_iterator;
00179     typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00180 
00181 
00182     // iterator support
00183 
00184     iterator begin() { return 0; }
00185     iterator end() { return 0; }
00186 
00187     const_iterator begin() const { return 0; }
00188     const_iterator end() const { return 0; }
00189 
00190     reverse_iterator rbegin() { return reverse_iterator(end()); }
00191     reverse_iterator rend() { return reverse_iterator(begin()); }
00192 
00193     const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
00194     const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
00195 
00196 
00197     // direct element access
00198 
00199     reference operator[](size_type iIndex)
00200     {
00201         LASS_ASSERT(false);
00202         return *begin();
00203     }
00204 
00205     const_reference operator[](size_type iIndex) const
00206     {
00207         LASS_ASSERT(false);
00208         return *begin();
00209     }
00210 
00211     reference at(size_type iIndex) 
00212     {
00213         throw std::out_of_range("index out of range in lass::util::FixedArray::at");
00214         return *begin();
00215     }
00216 
00217     const_reference at(size_type iIndex) const
00218     {
00219         throw std::out_of_range("index out of range in lass::util::FixedArray::at");
00220         return *begin();
00221     }
00222 
00223     reference front() { return *begin(); }
00224     reference back() { return *begin(); }
00225 
00226     const_reference front() const { return *begin(); }
00227     const_reference back() const { return *begin(); }
00228 
00229 
00230     // general
00231 
00232     size_type size() const { return 0; }
00233     size_type max_size() const { return 0; }
00234     bool empty() const { return true; }
00235 };
00236 
00237 
00238 }
00239 
00240 }
00241 
00242 #endif
00243 
00244 // --- END OF FILE -------------------------------------------------------------

Generated on Mon Nov 10 14:20:04 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo