FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 Class
00025     Foam::PtrList
00026 
00027 Description
00028     A templated 1D list of pointers to objects of type <T>, where the
00029     size of the array is known and used for subscript bounds checking, etc.
00030 
00031     The element operator [] returns a reference to the object
00032     rather than to the pointer.
00033 
00034 SourceFiles
00035     PtrList.C
00036     PtrListIO.C
00037 
00038 \*---------------------------------------------------------------------------*/
00039 
00040 #ifndef PtrList_H
00041 #define PtrList_H
00042 
00043 #include <OpenFOAM/List.H>
00044 
00045 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00046 
00047 namespace Foam
00048 {
00049 
00050 // Forward declaration of friend functions and operators
00051 
00052 template<class T> class PtrList;
00053 template<class T> class SLPtrList;
00054 
00055 template<class T>
00056 inline typename PtrList<T>::iterator operator+
00057 (
00058     const typename PtrList<T>::iterator&,
00059     label
00060 );
00061 
00062 template<class T>
00063 inline typename PtrList<T>::iterator operator+
00064 (
00065     label,
00066     const typename PtrList<T>::iterator&
00067 );
00068 
00069 template<class T>
00070 inline typename PtrList<T>::iterator operator-
00071 (
00072     const typename PtrList<T>::iterator&,
00073     label
00074 );
00075 
00076 template<class T>
00077 inline label operator-
00078 (
00079     const typename PtrList<T>::iterator&,
00080     const typename PtrList<T>::iterator&
00081 );
00082 
00083 template<class T>
00084 Istream& operator>>(Istream&, PtrList<T>&);
00085 
00086 template<class T>
00087 Ostream& operator<<(Ostream&, const PtrList<T>&);
00088 
00089 template<class T> class autoPtr;
00090 template<class T> class tmp;
00091 
00092 
00093 /*---------------------------------------------------------------------------*\
00094                            Class PtrList Declaration
00095 \*---------------------------------------------------------------------------*/
00096 
00097 template<class T>
00098 class PtrList
00099 {
00100     // Private data
00101 
00102         List<T*> ptrs_;
00103 
00104 
00105 protected:
00106 
00107     // Protected member functions
00108 
00109         //- Read from Istream using given Istream constructor class
00110         template<class INew>
00111         void read(Istream&, const INew& inewt);
00112 
00113 
00114 public:
00115 
00116     // Constructors
00117 
00118         //- Null Constructor.
00119         PtrList();
00120 
00121         //- Construct with length specified.
00122         explicit PtrList(const label);
00123 
00124         //- Copy constructor.
00125         PtrList(const PtrList<T>&);
00126 
00127         //- Copy constructor with additional argument for clone
00128         template<class CloneArg>
00129         PtrList(const PtrList<T>&, const CloneArg&);
00130 
00131         //- Construct by transferring the parameter contents
00132         PtrList(const Xfer<PtrList<T> >&);
00133 
00134         //- Construct as copy or re-use as specified.
00135         PtrList(PtrList<T>&, bool reUse);
00136 
00137         //- Construct as copy of SLPtrList<T>
00138         PtrList(const SLPtrList<T>&);
00139 
00140         //- Construct from Istream using given Istream constructor class
00141         template<class INew>
00142         PtrList(Istream&, const INew&);
00143 
00144         //- Construct from Istream using default Istream constructor class
00145         PtrList(Istream&);
00146 
00147 
00148     // Destructor
00149 
00150         ~PtrList();
00151 
00152 
00153     // Member functions
00154 
00155         // Access
00156 
00157             //- Return the number of elements in the PtrList
00158             inline label size() const;
00159 
00160             //- Return true if the PtrList is empty (ie, size() is zero).
00161             inline bool empty() const;
00162 
00163 
00164         // Edit
00165 
00166             //- Reset size of PtrList.  This can only be used to set the size
00167             //  of an empty PtrList, extend a PtrList, remove entries from
00168             //  the end of a PtrList. If the entries are non-empty they are
00169             //  deleted.
00170             void setSize(const label);
00171 
00172             //- Reset size of PtrList.  This can only be used to set the size
00173             //  of an empty PtrList, extend a PtrList, remove entries from
00174             //  the end of a PtrList. If the entries are non-empty they are
00175             //  deleted.
00176             inline void resize(const label);
00177 
00178             //- Clear the PtrList, i.e. set size to zero deleting all the
00179             //  allocated entries.
00180             void clear();
00181 
00182             //- Transfer the contents of the argument PtrList into this PtrList
00183             //  and annull the argument list.
00184             void transfer(PtrList<T>&);
00185 
00186             //- Transfer contents to the Xfer container
00187             inline Xfer<PtrList<T> > xfer();
00188 
00189             //- Is element set
00190             inline bool set(const label) const;
00191 
00192             //- Set element. Return old element (can be NULL).
00193             //  No checks on new element.
00194             inline autoPtr<T> set(const label, T*);
00195             inline autoPtr<T> set(const label, const autoPtr<T>&);
00196             inline autoPtr<T> set(const label, const tmp<T>&);
00197 
00198             //- Reorders elements. Ordering does not have to be done in
00199             //  ascending or descending order. Reordering has to be unique.
00200             //  (is shuffle)
00201             void reorder(const UList<label>&);
00202 
00203 
00204     // Member operators
00205 
00206         //- Return element const reference.
00207         inline const T& operator[](const label) const;
00208 
00209         //- Return element reference.
00210         inline T& operator[](const label);
00211 
00212         //- Return element const pointer.
00213         inline const T* operator()(const label) const;
00214 
00215 
00216         //- Assignment.
00217         PtrList<T>& operator=(const PtrList<T>&);
00218 
00219 
00220     // STL type definitions
00221 
00222         //- Type of values the PtrList contains.
00223         typedef T value_type;
00224 
00225         //- Type that can be used for storing into PtrList::value_type objects.
00226         typedef T& reference;
00227 
00228         //- Type that can be used for storing into constant PtrList::value_type
00229         //  objects.
00230         typedef const T& const_reference;
00231 
00232 
00233     // STL iterator
00234     // Random access iterator for traversing PtrList.
00235 
00236         class iterator;
00237         friend class iterator;
00238 
00239         //- An STL-conforming iterator
00240         class iterator
00241         {
00242             T** ptr_;
00243 
00244         public:
00245 
00246             //- Construct for a given PtrList entry
00247             inline iterator(T**);
00248 
00249             // Member operators
00250 
00251                 inline bool operator==(const iterator&) const;
00252                 inline bool operator!=(const iterator&) const;
00253 
00254                 typedef T& Tref;
00255                 inline Tref operator*();
00256                 inline Tref operator()();
00257 
00258                 inline iterator operator++();
00259                 inline iterator operator++(int);
00260 
00261                 inline iterator operator--();
00262                 inline iterator operator--(int);
00263 
00264                 inline iterator operator+=(label);
00265 
00266                 friend iterator operator+ <T>(const iterator&, label);
00267                 friend iterator operator+ <T>(label, const iterator&);
00268 
00269                 inline iterator operator-=(label);
00270 
00271                 friend iterator operator- <T>(const iterator&, label);
00272 
00273                 friend label operator- <T>
00274                 (
00275                     const iterator&,
00276                     const iterator&
00277                 );
00278 
00279                 inline T& operator[](label);
00280 
00281                 inline bool operator<(const iterator&) const;
00282                 inline bool operator>(const iterator&) const;
00283 
00284                 inline bool operator<=(const iterator&) const;
00285                 inline bool operator>=(const iterator&) const;
00286         };
00287 
00288         //- Return an iterator to begin traversing the PtrList.
00289         inline iterator begin();
00290 
00291         //- Return an iterator to end traversing the PtrList.
00292         inline iterator end();
00293 
00294 
00295     // IOstream operator
00296 
00297         //- Read List from Istream, discarding contents of existing List.
00298         friend Istream& operator>> <T>(Istream&, PtrList<T>&);
00299 
00300         // Write List to Ostream.
00301         friend Ostream& operator<< <T>(Ostream&, const PtrList<T>&);
00302 };
00303 
00304 
00305 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00306 
00307 } // End namespace Foam
00308 
00309 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00310 
00311 #   include <OpenFOAM/PtrListI.H>
00312 
00313 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00314 
00315 #ifdef NoRepository
00316 #   include <OpenFOAM/PtrList.C>
00317 #endif
00318 
00319 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00320 
00321 #endif
00322 
00323 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines