/home/neoflo/smb4k/SERVEUR/Millie/trunk/src/DataBuffer.hpp

Aller à la documentation de ce fichier.
00001 /******************************************************************************
00002  *       __    _    _   _       _       _   _____                             *
00003  *       | \  / |  | | | |     | |     | |  | ___|                            *
00004  *       |  \/  |  | | | |     | |     | |  | |_                              *
00005  *       |      |  | | | |     | |     | |  |  _|                             *
00006  *       | |\/| |  | | | |__   | |__   | |  | |__                             *
00007  *       |_|  |_|  |_| |____|  |____|  |_|  |____|                            *
00008  * __________________________________________________________________________ *
00009  *                 Multifunctional Library For Image Processing               *
00010  *                                                                            *
00011  *                                                                            *
00012  *                                                                            *
00013  *      (c) Copyright 2007 by Humbert Florent                                 *
00014  *                                                                            *
00015  *      This program is free software; you can redistribute it and/or modify  *
00016  *      it under the terms of the GNU General Public License as published by  *
00017  *      the Free Software Foundation; only version 2 of the License.          *
00018  *                                                                            *
00019  *      This program is distributed in the hope that it will be useful,       *
00020  *      but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00021  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00022  *      GNU General Public License for more details.                          *
00023  *                                                                            *
00024  *      You should have received a copy of the GNU General Public License     *
00025  *      along with this program; if not, write to the Free Software           *
00026  *      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA             *
00027  *      02111-1307, USA.                                                      *
00028  ******************************************************************************/
00029 
00032 #ifndef _DATABUFFER_HPP_
00033 #define _DATABUFFER_HPP_
00034 
00035 #include <iostream>
00036 #include "MillieExceptions.hpp"
00037 
00044 namespace Millie
00045 {
00046 
00050   template <typename T>
00051   class DataBuffer
00052   {
00053     protected:
00054       T* _tab;
00055       int    _size;
00056       int    _realsize;
00057 
00058     public:
00067       DataBuffer(int size = 0)
00068       {
00069         _tab = NULL;
00070 
00071         if(size<0)
00072           throw IllegalArgument("DataBuffer::Size");
00073 
00074         /*sinon, _tab vaut toujours NULL*/
00075         if(size!=0)
00076           _tab = new T[size];
00077 
00078         _size = size;
00079         _realsize = size;
00080       }
00081 
00082 
00083 
00088       DataBuffer(const DataBuffer<T>& d)
00089       {
00090         _tab = NULL;
00091 
00092         _size = d.getSize();
00093         _realsize = d.getSize();
00094 
00095         /*sinon _tab est NULL*/
00096         if(_size != 0)
00097           _tab = new T[_size];
00098 
00099         /* si _tab est NULL, c'est que la taille est nulle, on ne rentre pas dans
00100           la boucle
00101           */
00102         for(int i = 0; i< _size;i++)
00103           _tab[i] = d._tab[i];
00104       }
00105 
00109       virtual ~DataBuffer()
00110       {
00111         delete[] _tab;
00112         _tab = NULL;
00113       }
00114 
00123       void resize(int taille)
00124       {
00125         if(taille<0)
00126           throw IllegalArgument("DataBuffer Resize");
00127 
00128 
00129         _size = taille;
00130 
00131         if(taille> _realsize)
00132         {
00133           delete[] _tab;
00134           _tab = NULL;
00135           /*la taille ne peut pas être nulle à cet instant !*/
00136           _tab = new T[taille];
00137           _realsize = taille;
00138         }
00139 
00140       }
00141 
00148       DataBuffer& operator=(const DataBuffer<T>& d)
00149       {
00150         _size = d.getSize();
00151 
00152         /*optimisation*/
00153         if(_realsize < d.getSize())
00154         {
00155           _realsize = d.getSize();
00156 
00157           T * tempo = _tab;
00158           /*d.getSize() ne peut pas être nulle*/
00159           tempo = new T[d.getSize()];
00160 
00161           for(int i = 0; i< _size;i++)
00162             tempo[i] = d._tab[i];
00163 
00165           delete[] _tab;
00166 
00167           _tab = tempo;
00168         }
00169         else
00170         {
00171 
00172           /*si la taille est nulle, on ne rentre pas dans la boucle*/
00173           for(int i = 0; i< _size;i++)
00174             _tab[i] = d._tab[i];
00175         }
00176 
00177         return *this;
00178       }
00179 
00184       int  getSize() const
00185       {
00186         return _size;
00187       }
00188 
00192       T * getDataBuffer()
00193       {
00194         return _tab;
00195       }
00196 
00202       T operator[](int position) const
00203       {
00204 #ifndef _MILLIE_DANGEROUS_OPTIMIZATIONS_
00205         if(position<0 || position> getSize())
00206         {
00207           //*(int*) 0 = 1;
00208           throw OutOfRange("hors zone");
00209 
00210         }
00211 #endif
00212         return _tab[position];
00213       }
00214 
00219       T& operator[](int position)
00220       {
00221 #ifndef _MILLIE_DANGEROUS_OPTIMIZATIONS_
00222         if(position<0 || position> getSize())
00223         {
00224           //*(int*) 0 = 1;
00225           throw OutOfRange("hors zone");
00226         }
00227 #endif
00228         return _tab[position];
00229       }
00230 
00231   };
00232 
00233 
00234 }
00235 
00236 #endif

Généré le Fri May 18 23:24:33 2007 pour Millie par  doxygen 1.5.1