OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide 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-2009 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     Foam::PtrList
00027 
00028 Description
00029     A templated 1D list of pointers to objects of type <T>, where the
00030     size of the array is known and used for subscript bounds checking, etc.
00031 
00032     The element operator [] returns a reference to the object
00033     rather than to the 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 friend functions and operators
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 by transferring the parameter contents
00133         PtrList(const Xfer<PtrList<T> >&);
00134 
00135         //- Construct as copy or re-use as specified.
00136         PtrList(PtrList<T>&, bool reUse);
00137 
00138         //- Construct as copy of SLPtrList<T>
00139         PtrList(const SLPtrList<T>&);
00140 
00141         //- Construct from Istream using given Istream constructor class
00142         template<class INew>
00143         PtrList(Istream&, const INew&);
00144 
00145         //- Construct from Istream using default Istream constructor class
00146         PtrList(Istream&);
00147 
00148 
00149     // Destructor
00150 
00151         ~PtrList();
00152 
00153 
00154     // Member functions
00155 
00156         // Access
00157 
00158             //- Return the number of elements in the PtrList
00159             inline label size() const;
00160 
00161             //- Return true if the PtrList is empty (ie, size() is zero).
00162             inline bool empty() const;
00163 
00164 
00165         // Edit
00166 
00167             //- Reset size of PtrList.  This can only be used to set the size
00168             //  of an empty PtrList, extend a PtrList, remove entries from
00169             //  the end of a PtrList. If the entries are non-empty they are
00170             //  deleted.
00171             void setSize(const label);
00172 
00173             //- Reset size of PtrList.  This can only be used to set the size
00174             //  of an empty PtrList, extend a PtrList, remove entries from
00175             //  the end of a PtrList. If the entries are non-empty they are
00176             //  deleted.
00177             inline void resize(const label);
00178 
00179             //- Clear the PtrList, i.e. set size to zero deleting all the
00180             //  allocated entries.
00181             void clear();
00182 
00183             //- Transfer the contents of the argument PtrList into this PtrList
00184             //  and annull the argument list.
00185             void transfer(PtrList<T>&);
00186 
00187             //- Transfer contents to the Xfer container
00188             inline Xfer<PtrList<T> > xfer();
00189 
00190             //- Is element set
00191             inline bool set(const label) const;
00192 
00193             //- Set element. Return old element (can be NULL).
00194             //  No checks on new element.
00195             inline autoPtr<T> set(const label, T*);
00196             inline autoPtr<T> set(const label, const autoPtr<T>&);
00197             inline autoPtr<T> set(const label, const tmp<T>&);
00198 
00199             //- Reorders elements. Ordering does not have to be done in
00200             //  ascending or descending order. Reordering has to be unique.
00201             //  (is shuffle)
00202             void reorder(const UList<label>&);
00203 
00204 
00205     // Member operators
00206 
00207         //- Return element const reference.
00208         inline const T& operator[](const label) const;
00209 
00210         //- Return element reference.
00211         inline T& operator[](const label);
00212 
00213         //- Return element const pointer.
00214         inline const T* operator()(const label) const;
00215 
00216 
00217         //- Assignment.
00218         PtrList<T>& operator=(const PtrList<T>&);
00219 
00220 
00221     // STL type definitions
00222 
00223         //- Type of values the PtrList contains.
00224         typedef T value_type;
00225 
00226         //- Type that can be used for storing into PtrList::value_type objects.
00227         typedef T& reference;
00228 
00229         //- Type that can be used for storing into constant PtrList::value_type
00230         //  objects.
00231         typedef const T& const_reference;
00232 
00233 
00234     // STL iterator
00235     // Random access iterator for traversing PtrList.
00236 
00237         class iterator;
00238         friend class iterator;
00239 
00240         //- An STL-conforming iterator
00241         class iterator
00242         {
00243             T** ptr_;
00244 
00245         public:
00246 
00247             //- Construct for a given PtrList entry
00248             inline iterator(T**);
00249 
00250             // Member operators
00251 
00252                 inline bool operator==(const iterator&) const;
00253                 inline bool operator!=(const iterator&) const;
00254 
00255                 typedef T& Tref;
00256                 inline Tref operator*();
00257                 inline Tref operator()();
00258 
00259                 inline iterator operator++();
00260                 inline iterator operator++(int);
00261 
00262                 inline iterator operator--();
00263                 inline iterator operator--(int);
00264 
00265                 inline iterator operator+=(label);
00266 
00267                 friend iterator operator+ <T>(const iterator&, label);
00268                 friend iterator operator+ <T>(label, const iterator&);
00269 
00270                 inline iterator operator-=(label);
00271 
00272                 friend iterator operator- <T>(const iterator&, label);
00273 
00274                 friend label operator- <T>
00275                 (
00276                     const iterator&,
00277                     const iterator&
00278                 );
00279 
00280                 inline T& operator[](label);
00281 
00282                 inline bool operator<(const iterator&) const;
00283                 inline bool operator>(const iterator&) const;
00284 
00285                 inline bool operator<=(const iterator&) const;
00286                 inline bool operator>=(const iterator&) const;
00287         };
00288 
00289         //- Return an iterator to begin traversing the PtrList.
00290         inline iterator begin();
00291 
00292         //- Return an iterator to end traversing the PtrList.
00293         inline iterator end();
00294 
00295 
00296     // IOstream operator
00297 
00298         //- Read List from Istream, discarding contents of existing List.
00299         friend Istream& operator>> <T>(Istream&, PtrList<T>&);
00300 
00301         // Write List to Ostream.
00302         friend Ostream& operator<< <T>(Ostream&, const PtrList<T>&);
00303 };
00304 
00305 
00306 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00307 
00308 } // End namespace Foam
00309 
00310 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00311 
00312 #   include "PtrListI.H"
00313 
00314 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00315 
00316 #ifdef NoRepository
00317 #   include "PtrList.C"
00318 #endif
00319 
00320 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00321 
00322 #endif
00323 
00324 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd