OpenFOAM logo
Open Source CFD Toolkit

PtrList.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2005 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Class
00026     PtrList
00027 
00028 Description
00029     A PtrList<class T> is a 1D array of pointers to objects
00030     of T 'T', where the size of the array is known and used for
00031     subscript bounds checking, etc.
00032     The element operator [] returns a reference to the object
00033     rather than a pointer.
00034 
00035 SourceFiles
00036     PtrList.C
00037     PtrListIO.C
00038 
00039 \*---------------------------------------------------------------------------*/
00040 
00041 #ifndef PtrList_H
00042 #define PtrList_H
00043 
00044 #include "List.H"
00045 
00046 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00047 
00048 namespace Foam
00049 {
00050 
00051 // * * * * * * Forward declaration of template friend fuctions * * * * * * * //
00052 
00053 template<class T> class PtrList;
00054 template<class T> class SLPtrList;
00055 
00056 template<class T>
00057 inline typename PtrList<T>::iterator operator+
00058 (
00059     const typename PtrList<T>::iterator&,
00060     label
00061 );
00062 
00063 template<class T>
00064 inline typename PtrList<T>::iterator operator+
00065 (
00066     label,
00067     const typename PtrList<T>::iterator&
00068 );
00069 
00070 template<class T>
00071 inline typename PtrList<T>::iterator operator-
00072 (
00073     const typename PtrList<T>::iterator&,
00074     label
00075 );
00076 
00077 template<class T>
00078 inline label operator-
00079 (
00080     const typename PtrList<T>::iterator&,
00081     const typename PtrList<T>::iterator&
00082 );
00083 
00084 template<class T>
00085 Istream& operator>>(Istream&, PtrList<T>&);
00086 
00087 template<class T>
00088 Ostream& operator<<(Ostream&, const PtrList<T>&);
00089 
00090 template<class T> class autoPtr;
00091 template<class T> class tmp;
00092 
00093 
00094 /*---------------------------------------------------------------------------*\
00095                            Class PtrList Declaration
00096 \*---------------------------------------------------------------------------*/
00097 
00098 template<class T>
00099 class PtrList
00100 {
00101     // Private data
00102 
00103         List<T*> ptrs_;
00104 
00105         //- Number of the next free hook.
00106         label nextFree_;
00107 
00108 
00109     // Private member functions
00110 
00111         //- Construct with length and single value specified (not implemented).
00112         //  Hides the equivalent constructor in List<T*>.
00113         PtrList(const label, const T&);
00114 
00115 
00116         //- Read from Istream using given Istream constructor class
00117         template<class INew>
00118         void read(Istream&, const INew& inewt);
00119 
00120 
00121 protected:
00122 
00123         //- Return address of next free element
00124         label nextFree() const
00125         {
00126             return nextFree_;
00127         }
00128 
00129 
00130 public:
00131 
00132     // Constructors
00133 
00134         //- Null Constructor.
00135         PtrList();
00136 
00137         //- Construct with length specified.
00138         explicit PtrList(const label);
00139 
00140         //- Copy constructor.
00141         PtrList(const PtrList<T>&);
00142 
00143         //- Copy constructor with additional argument for clone
00144         template<class CloneArg>
00145         PtrList(const PtrList<T>&, const CloneArg&);
00146 
00147         //- Construct as copy or re-use as specified.
00148         PtrList(PtrList<T>&, bool reUse);
00149 
00150         //- Construct as copy of SLPtrList<T>
00151         PtrList(const SLPtrList<T>&);
00152 
00153         //- Construct from Istream using given Istream constructor class
00154         template<class INew>
00155         PtrList(Istream&, const INew&);
00156 
00157         //- Construct from Istream using default Istream constructor class
00158         PtrList(Istream&);
00159 
00160 
00161     // Destructor
00162 
00163         ~PtrList();
00164 
00165 
00166     // Member functions
00167 
00168         // Access
00169 
00170             //- Return the number of elements in the PtrList
00171             inline label size() const;
00172 
00173 
00174         // Edit
00175 
00176             //- Reset size of PtrList.  This can only be used to set the size of
00177             //  an empty PtrList, extend a PtrList, remove entries from
00178             //  the end of a PtrList. If the entries are non-empty they are
00179             //  deleted.
00180             void setSize(const label);
00181 
00182             //- Clear the PtrList, i.e. set size to zero deleting all the
00183             //  allocated entries.
00184             void clear();
00185 
00186             //- Transfer the contents of the argument PtrList into this PtrList
00187             //  and annull the argument list.
00188             void transfer(PtrList<T>&);
00189 
00190             //- Return an element pointer to be set.  The pointer is checked
00191             //  if already set and the index range checked.
00192             typedef T* Tptr;
00193             Tptr& set(const label);
00194 
00195             //- Hook an an element created by new onto the list.
00196             void hook(T*);
00197 
00198             //- Hook an an element created by new onto the list.
00199             inline void hook(const autoPtr<T>&);
00200 
00201             //- Hook an an element created by new onto the list.
00202             inline void hook(const tmp<T>&);
00203 
00204             //- Reorders elements. Ordering does not have to be done in
00205             //  ascending or descending order. Reordering has to be unique.
00206             //  (is shuffle)
00207             void reorder(const UList<label>&);
00208 
00209 
00210     // Member operators
00211 
00212         //- Return element const reference.
00213         inline const T& operator[](const label) const;
00214 
00215         //- Return element reference.
00216         inline T& operator[](const label);
00217 
00218         //- Return element const pointer.
00219         inline const T* operator()(const label) const;
00220 
00221 
00222         //- Assignment.
00223         PtrList<T>& operator=(const PtrList<T>&);
00224 
00225 
00226     // STL type definitions
00227 
00228         //- Type of values the PtrList contains.
00229         typedef T value_type;
00230 
00231         //- Type that can be used for storing into PtrList::value_type objects.
00232         typedef T& reference;
00233 
00234         //- Type that can be used for storing into constant PtrList::value_type
00235         //  objects.
00236         typedef const T& const_reference;
00237 
00238 
00239     // STL iterator
00240     // Random access iterator for traversing PtrList.
00241 
00242         class iterator;
00243         friend class iterator;
00244 
00245         class iterator
00246         {
00247             T** ptr_;
00248 
00249         public:
00250 
00251             //- Construct for a given PtrList entry
00252             inline iterator(T**);
00253 
00254             // Member operators
00255 
00256                 inline bool operator==(const iterator&) const;
00257                 inline bool operator!=(const iterator&) const;
00258 
00259                 inline T& operator*();
00260                 inline T& operator()();
00261 
00262                 inline iterator operator++();
00263                 inline iterator operator++(int);
00264 
00265                 inline iterator operator--();
00266                 inline iterator operator--(int);
00267 
00268                 inline iterator operator+=(label);
00269 
00270                 friend iterator operator+ <T>(const iterator&, label);
00271                 friend iterator operator+ <T>(label, const iterator&);
00272 
00273                 inline iterator operator-=(label);
00274 
00275                 friend iterator operator- <T>(const iterator&, label);
00276 
00277                 friend label operator- <T>
00278                 (
00279                     const iterator&,
00280                     const iterator&
00281                 );
00282 
00283                 inline T& operator[](label);
00284 
00285                 inline bool operator<(const iterator&) const;
00286                 inline bool operator>(const iterator&) const;
00287 
00288                 inline bool operator<=(const iterator&) const;
00289                 inline bool operator>=(const iterator&) const;
00290         };
00291 
00292         //- Return an iterator to begin traversing the PtrList.
00293         inline iterator begin();
00294 
00295         //- Return an iterator to end traversing the PtrList.
00296         inline iterator end();
00297 
00298 
00299     // IOstream operator
00300 
00301         //- Read List from Istream, discarding contents of existing List.
00302         friend Istream& operator>> <T>(Istream&, PtrList<T>&);
00303 
00304         // Write List to Ostream.
00305         friend Ostream& operator<< <T>(Ostream&, const PtrList<T>&);
00306 };
00307 
00308 
00309 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00310 
00311 } // End namespace Foam
00312 
00313 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00314 
00315 #   include "PtrListI.H"
00316 
00317 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00318 
00319 #ifdef NoRepository
00320 #   include "PtrList.C"
00321 #endif
00322 
00323 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00324 
00325 #endif
00326 
00327 // ************************************************************************* //
For further information go to www.openfoam.org