OpenFOAM logo
The Open Source CFD Toolbox
  Programmer’s C++ Doxygen OpenCFD Solutions Contact OpenFOAM

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-2007 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 
00106 protected:
00107 
00108     // Protected member functions
00109 
00110         //- Read from Istream using given Istream constructor class
00111         template<class INew>
00112         void read(Istream&, const INew& inewt);
00113 
00114 
00115 public:
00116 
00117     // Constructors
00118 
00119         //- Null Constructor.
00120         PtrList();
00121 
00122         //- Construct with length specified.
00123         explicit PtrList(const label);
00124 
00125         //- Copy constructor.
00126         PtrList(const PtrList<T>&);
00127 
00128         //- Copy constructor with additional argument for clone
00129         template<class CloneArg>
00130         PtrList(const PtrList<T>&, const CloneArg&);
00131 
00132         //- Construct as copy or re-use as specified.
00133         PtrList(PtrList<T>&, bool reUse);
00134 
00135         //- Construct as copy of SLPtrList<T>
00136         PtrList(const SLPtrList<T>&);
00137 
00138         //- Construct from Istream using given Istream constructor class
00139         template<class INew>
00140         PtrList(Istream&, const INew&);
00141 
00142         //- Construct from Istream using default Istream constructor class
00143         PtrList(Istream&);
00144 
00145 
00146     // Destructor
00147 
00148         ~PtrList();
00149 
00150 
00151     // Member functions
00152 
00153         // Access
00154 
00155             //- Return the number of elements in the PtrList
00156             inline label size() const;
00157 
00158 
00159         // Edit
00160 
00161             //- Reset size of PtrList.  This can only be used to set the size
00162             //  of an empty PtrList, extend a PtrList, remove entries from
00163             //  the end of a PtrList. If the entries are non-empty they are
00164             //  deleted.
00165             void setSize(const label);
00166 
00167             //- Clear the PtrList, i.e. set size to zero deleting all the
00168             //  allocated entries.
00169             void clear();
00170 
00171             //- Transfer the contents of the argument PtrList into this PtrList
00172             //  and annull the argument list.
00173             void transfer(PtrList<T>&);
00174 
00175             //- Set element. Return old element (can be NULL).
00176             //  No checks on new element.
00177             inline autoPtr<T> set(const label, T*);
00178             inline autoPtr<T> set(const label, const autoPtr<T>&);
00179             inline autoPtr<T> set(const label, const tmp<T>&);
00180 
00181             //- Reorders elements. Ordering does not have to be done in
00182             //  ascending or descending order. Reordering has to be unique.
00183             //  (is shuffle)
00184             void reorder(const UList<label>&);
00185 
00186 
00187     // Member operators
00188 
00189         //- Return element const reference.
00190         inline const T& operator[](const label) const;
00191 
00192         //- Return element reference.
00193         inline T& operator[](const label);
00194 
00195         //- Return element const pointer.
00196         inline const T* operator()(const label) const;
00197 
00198 
00199         //- Assignment.
00200         PtrList<T>& operator=(const PtrList<T>&);
00201 
00202 
00203     // STL type definitions
00204 
00205         //- Type of values the PtrList contains.
00206         typedef T value_type;
00207 
00208         //- Type that can be used for storing into PtrList::value_type objects.
00209         typedef T& reference;
00210 
00211         //- Type that can be used for storing into constant PtrList::value_type
00212         //  objects.
00213         typedef const T& const_reference;
00214 
00215 
00216     // STL iterator
00217     // Random access iterator for traversing PtrList.
00218 
00219         class iterator;
00220         friend class iterator;
00221 
00222         class iterator
00223         {
00224             T** ptr_;
00225 
00226         public:
00227 
00228             //- Construct for a given PtrList entry
00229             inline iterator(T**);
00230 
00231             // Member operators
00232 
00233                 inline bool operator==(const iterator&) const;
00234                 inline bool operator!=(const iterator&) const;
00235 
00236                 inline T& operator*();
00237                 inline T& operator()();
00238 
00239                 inline iterator operator++();
00240                 inline iterator operator++(int);
00241 
00242                 inline iterator operator--();
00243                 inline iterator operator--(int);
00244 
00245                 inline iterator operator+=(label);
00246 
00247                 friend iterator operator+ <T>(const iterator&, label);
00248                 friend iterator operator+ <T>(label, const iterator&);
00249 
00250                 inline iterator operator-=(label);
00251 
00252                 friend iterator operator- <T>(const iterator&, label);
00253 
00254                 friend label operator- <T>
00255                 (
00256                     const iterator&,
00257                     const iterator&
00258                 );
00259 
00260                 inline T& operator[](label);
00261 
00262                 inline bool operator<(const iterator&) const;
00263                 inline bool operator>(const iterator&) const;
00264 
00265                 inline bool operator<=(const iterator&) const;
00266                 inline bool operator>=(const iterator&) const;
00267         };
00268 
00269         //- Return an iterator to begin traversing the PtrList.
00270         inline iterator begin();
00271 
00272         //- Return an iterator to end traversing the PtrList.
00273         inline iterator end();
00274 
00275 
00276     // IOstream operator
00277 
00278         //- Read List from Istream, discarding contents of existing List.
00279         friend Istream& operator>> <T>(Istream&, PtrList<T>&);
00280 
00281         // Write List to Ostream.
00282         friend Ostream& operator<< <T>(Ostream&, const PtrList<T>&);
00283 };
00284 
00285 
00286 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00287 
00288 } // End namespace Foam
00289 
00290 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00291 
00292 #   include "PtrListI.H"
00293 
00294 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00295 
00296 #ifdef NoRepository
00297 #   include "PtrList.C"
00298 #endif
00299 
00300 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00301 
00302 #endif
00303 
00304 // ************************************************************************* //
Copyright © 2000-2007 OpenCFD Ltd