initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / containers / Lists / PtrList / PtrList.H
blobb5167e5ef8977f02334642c9d31f32ed91ac16e3
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Class
26     Foam::PtrList
28 Description
29     A templated 1D list of pointers to objects of type \<T\>, where the
30     size of the array is known and used for subscript bounds checking, etc.
32     The element operator [] returns a reference to the object
33     rather than to the pointer.
35 SourceFiles
36     PtrList.C
37     PtrListIO.C
39 \*---------------------------------------------------------------------------*/
41 #ifndef PtrList_H
42 #define PtrList_H
44 #include "List.H"
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 namespace Foam
51 // Forward declaration of friend functions and operators
53 template<class T> class PtrList;
54 template<class T> class SLPtrList;
56 template<class T>
57 inline typename PtrList<T>::iterator operator+
59     const typename PtrList<T>::iterator&,
60     label
63 template<class T>
64 inline typename PtrList<T>::iterator operator+
66     label,
67     const typename PtrList<T>::iterator&
70 template<class T>
71 inline typename PtrList<T>::iterator operator-
73     const typename PtrList<T>::iterator&,
74     label
77 template<class T>
78 inline label operator-
80     const typename PtrList<T>::iterator&,
81     const typename PtrList<T>::iterator&
84 template<class T>
85 Istream& operator>>(Istream&, PtrList<T>&);
87 template<class T>
88 Ostream& operator<<(Ostream&, const PtrList<T>&);
90 template<class T> class autoPtr;
91 template<class T> class tmp;
94 /*---------------------------------------------------------------------------*\
95                            Class PtrList Declaration
96 \*---------------------------------------------------------------------------*/
98 template<class T>
99 class PtrList
101     // Private data
103         List<T*> ptrs_;
106 protected:
108     // Protected member functions
110         //- Read from Istream using given Istream constructor class
111         template<class INew>
112         void read(Istream&, const INew& inewt);
115 public:
117     // Constructors
119         //- Null Constructor.
120         PtrList();
122         //- Construct with length specified.
123         explicit PtrList(const label);
125         //- Copy constructor.
126         PtrList(const PtrList<T>&);
128         //- Copy constructor with additional argument for clone
129         template<class CloneArg>
130         PtrList(const PtrList<T>&, const CloneArg&);
132         //- Construct by transferring the parameter contents
133         PtrList(const Xfer<PtrList<T> >&);
135         //- Construct as copy or re-use as specified.
136         PtrList(PtrList<T>&, bool reUse);
138         //- Construct as copy of SLPtrList<T>
139         PtrList(const SLPtrList<T>&);
141         //- Construct from Istream using given Istream constructor class
142         template<class INew>
143         PtrList(Istream&, const INew&);
145         //- Construct from Istream using default Istream constructor class
146         PtrList(Istream&);
149     // Destructor
151         ~PtrList();
154     // Member functions
156         // Access
158             //- Return the number of elements in the PtrList
159             inline label size() const;
161             //- Return true if the PtrList is empty (ie, size() is zero).
162             inline bool empty() const;
165         // Edit
167             //- Reset size of PtrList.  This can only be used to set the size
168             //  of an empty PtrList, extend a PtrList, remove entries from
169             //  the end of a PtrList. If the entries are non-empty they are
170             //  deleted.
171             void setSize(const label);
173             //- Reset size of PtrList.  This can only be used to set the size
174             //  of an empty PtrList, extend a PtrList, remove entries from
175             //  the end of a PtrList. If the entries are non-empty they are
176             //  deleted.
177             inline void resize(const label);
179             //- Clear the PtrList, i.e. set size to zero deleting all the
180             //  allocated entries.
181             void clear();
183             //- Transfer the contents of the argument PtrList into this PtrList
184             //  and annull the argument list.
185             void transfer(PtrList<T>&);
187             //- Transfer contents to the Xfer container
188             inline Xfer<PtrList<T> > xfer();
190             //- Is element set
191             inline bool set(const label) const;
193             //- Set element. Return old element (can be NULL).
194             //  No checks on new element.
195             inline autoPtr<T> set(const label, T*);
196             inline autoPtr<T> set(const label, const autoPtr<T>&);
197             inline autoPtr<T> set(const label, const tmp<T>&);
199             //- Reorders elements. Ordering does not have to be done in
200             //  ascending or descending order. Reordering has to be unique.
201             //  (is shuffle)
202             void reorder(const UList<label>&);
205     // Member operators
207         //- Return element const reference.
208         inline const T& operator[](const label) const;
210         //- Return element reference.
211         inline T& operator[](const label);
213         //- Return element const pointer.
214         inline const T* operator()(const label) const;
217         //- Assignment.
218         PtrList<T>& operator=(const PtrList<T>&);
221     // STL type definitions
223         //- Type of values the PtrList contains.
224         typedef T value_type;
226         //- Type that can be used for storing into PtrList::value_type objects.
227         typedef T& reference;
229         //- Type that can be used for storing into constant PtrList::value_type
230         //  objects.
231         typedef const T& const_reference;
234     // STL iterator
235     // Random access iterator for traversing PtrList.
237         class iterator;
238         friend class iterator;
240         //- An STL-conforming iterator
241         class iterator
242         {
243             T** ptr_;
245         public:
247             //- Construct for a given PtrList entry
248             inline iterator(T**);
250             // Member operators
252                 inline bool operator==(const iterator&) const;
253                 inline bool operator!=(const iterator&) const;
255                 typedef T& Tref;
256                 inline Tref operator*();
257                 inline Tref operator()();
259                 inline iterator operator++();
260                 inline iterator operator++(int);
262                 inline iterator operator--();
263                 inline iterator operator--(int);
265                 inline iterator operator+=(label);
267                 friend iterator operator+ <T>(const iterator&, label);
268                 friend iterator operator+ <T>(label, const iterator&);
270                 inline iterator operator-=(label);
272                 friend iterator operator- <T>(const iterator&, label);
274                 friend label operator- <T>
275                 (
276                     const iterator&,
277                     const iterator&
278                 );
280                 inline T& operator[](label);
282                 inline bool operator<(const iterator&) const;
283                 inline bool operator>(const iterator&) const;
285                 inline bool operator<=(const iterator&) const;
286                 inline bool operator>=(const iterator&) const;
287         };
289         //- Return an iterator to begin traversing the PtrList.
290         inline iterator begin();
292         //- Return an iterator to end traversing the PtrList.
293         inline iterator end();
296     // IOstream operator
298         //- Read List from Istream, discarding contents of existing List.
299         friend Istream& operator>> <T>(Istream&, PtrList<T>&);
301         // Write List to Ostream.
302         friend Ostream& operator<< <T>(Ostream&, const PtrList<T>&);
306 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
308 } // End namespace Foam
310 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
312 #   include "PtrListI.H"
314 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
316 #ifdef NoRepository
317 #   include "PtrList.C"
318 #endif
320 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
322 #endif
324 // ************************************************************************* //