Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

checkedvector.h

Go to the documentation of this file.
00001 /*-*-c++-*-*****************************************************************
00002                           checkedvector.h  -  description
00003                              -------------------
00004     begin                : Fri Feb 20 2004
00005     copyright            : (C) 2004 by Christian Stimming
00006     email                : stimming@tuhh.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU Lesser General Public            *
00013  *   License as published by the Free Software Foundation; either          *
00014  *   version 2.1 of the License, or (at your option) any later version.    *
00015  *                                                                         *
00016  *   This library is distributed in the hope that it will be useful,       *
00017  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00018  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00019  *   Lesser General Public License for more details.                       *
00020  *                                                                         *
00021  *   You should have received a copy of the GNU Lesser General Public      *
00022  *   License along with this library; if not, write to the Free Software   *
00023  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston,                 *
00024  *   MA  02111-1307  USA                                                   *
00025  *                                                                         *
00026  ***************************************************************************/
00027 
00040 #ifndef _CHECKEDVECTOR_H
00041 #define _CHECKEDVECTOR_H
00042 
00043 
00044 #include <vector>
00045 #include <iostream>
00046 
00047 #include <simthetic/misc.h>
00048 #include <simthetic/exceptions.h>
00049 
00050 namespace simth
00051 {
00052 
00062 
00063 
00071 template<class T>
00072 class checkedVector : public std::vector<T>
00073 {
00074   public:
00077     typedef typename std::vector<T>::iterator iterator;
00080     typedef typename std::vector<T>::const_iterator const_iterator;
00081 
00084     typedef typename std::vector<T>::reference reference;
00087     typedef typename std::vector<T>::const_reference const_reference;
00088 
00089   public:
00092     checkedVector() : std::vector<T>() {}
00096     explicit checkedVector(unsigned int size, const T &val = T()) : std::vector<T>(size, val) {}
00100     const_reference operator[](unsigned int pos) const;
00104     reference operator[](unsigned int pos);
00108     void assign(unsigned int size, const T &val);
00113     checkedVector<T>& operator=(const checkedVector<T>& from);
00114     checkedVector<T>& operator=(const std::vector<T>& from);
00115 };
00116 
00117 
00118 template<class T>
00119 inline typename checkedVector<T>::const_reference checkedVector<T>::operator[](unsigned int pos) const
00120 {
00121 #if DEBUG==1
00122   if(pos >= std::vector<T>::size()) {
00123     throw std::out_of_range("checkedVector<T>::operator[] out of range");
00124   }
00125 #endif
00126   return std::vector<T>::operator[](pos);
00127 }
00128 
00129 
00130 
00131 //Fuer Anwender dieser Funktion verhaelt sich der Rueckgabewert wie eine
00132 //Referenz auf ein T (T&)
00133 template<class T>
00134 inline typename checkedVector<T>::reference checkedVector<T>::operator[](unsigned int pos)
00135 {
00136 #if DEBUG==1
00137   if(pos >= std::vector<T>::size()) {
00138     throw std::out_of_range("checkedVector<T>::operator[] out of range");
00139   }
00140 #endif
00141   return std::vector<T>::operator[](pos);
00142 }
00143 
00144 
00145 template<class T>
00146 inline checkedVector<T>& checkedVector<T>::operator=(const std::vector<T>& from)
00147 {
00148   unsigned int newSize = from.size();
00149   if(newSize != this->size()) {
00150     this->resize(newSize);
00151   }
00152 
00153   iterator thisIter = this->begin();
00154   for(const_iterator fromIter = from.begin();fromIter!=from.end();++fromIter) {
00155     (*thisIter) = (*fromIter);
00156     ++thisIter;
00157   }
00158   return *this;
00159 }
00160 template<class T>
00161 inline checkedVector<T>& checkedVector<T>::operator=(const checkedVector<T>& from)
00162 {
00163   return operator=(static_cast<const std::vector<T>&>(from));
00164 }
00165 
00166 template<class T>
00167 inline void checkedVector<T>::assign(unsigned int newSize, const T &val)
00168 {
00169   this->resize(newSize);
00170   for(unsigned int pos=0; pos<newSize; pos++) {
00171     (*this)[pos]=val;
00172   }
00173 }
00174 
00175 
00176 
00177 template<class T>
00178 std::ostream& operator<<(std::ostream &os, const checkedVector<T> &seq)
00179 {
00180   //save old format settings:
00181   std::ios::fmtflags oldSettings = os.flags();
00182   //formatting
00183   os.precision(2);
00184   unsigned int len = seq.size();
00185   for(int i = len - 1; i >= 0; i--) {
00186     os<<" "<<seq[i]<<" ";
00187   }
00188   os<<" <- start";
00189   //rewrite old settings:
00190   os.flags(oldSettings);
00191   return os;
00192 }
00193 
00194 std::ostream& operator<<(std::ostream &os, const checkedVector<Complex>& compSeq);
00195 
00197 
00198 } // namespace
00199 
00200 #endif // _CONTAINERS_H

Generated on Mon Apr 24 21:19:19 2006 for simthetic by  doxygen 1.4.1