initial commit for version 1.5.x patch release
[OpenFOAM-1.5.x.git] / src / OpenFOAM / containers / LinkedLists / accessTypes / LList / LList.H
bloba9bc0bccae46bfdef4d1523beacf08fffc4e4dfd
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 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::LList
28 Description
29     Template class for non-intrusive linked lists.
31 SourceFiles
32     LList.C
33     LListIO.C
35 \*---------------------------------------------------------------------------*/
37 #ifndef LList_H
38 #define LList_H
40 #include "label.H"
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 namespace Foam
47 class Istream;
48 class Ostream;
50 // Forward declaration of friend functions and operators
52 template<class LListBase, class T> class LList;
54 template<class LListBase, class T>
55 Istream& operator>>
57     Istream&,
58     LList<LListBase, T>&
61 template<class LListBase, class T>
62 Ostream& operator<<
64     Ostream&,
65     const LList<LListBase, T>&
69 /*---------------------------------------------------------------------------*\
70                            Class LList Declaration
71 \*---------------------------------------------------------------------------*/
73 template<class LListBase, class T>
74 class LList
76     public LListBase
79 public:
81     // Forward declaration of STL iterators
83         class iterator;
84         friend class iterator;
86         class const_iterator;
87         friend class const_iterator;
90     //- Link structure
91     struct link
92     :
93         public LListBase::link
94     {
95         //- Stored object
96         T obj_;
98         //- Construct given object
99         link(T a)
100         :
101             obj_(a)
102         {}
103     };
106     // Constructors
108         //- Null construct
109         LList()
110         {}
112         //- Construct given initial T
113         LList(T a)
114         :
115             LListBase(new link(a))
116         {}
118         //- Construct from Istream
119         LList(Istream&);
121         //- Construct as copy
122         LList(const LList<LListBase, T>&);
125     // Destructor
127         ~LList();
130     // Member Functions
132         // Access
134             //- Return the first entry added
135             T& first()
136             {
137                 return static_cast<link*>(LListBase::first())->obj_;
138             }
140             //- Return const access to the first entry added
141             const T& first() const
142             {
143                 return static_cast<const link*>(LListBase::first())->obj_;
144             }
146             //- Return the last entry added
147             T& last()
148             {
149                 return static_cast<link*>(LListBase::last())->obj_;
150             }
152             //- Return const access to the last entry added
153             const T& last() const
154             {
155                 return static_cast<const link*>(LListBase::last())->obj_;
156             }
159         // Edit
161             //- Add at head of list
162             void insert(const T& a)
163             {
164                 LListBase::insert(new link(a));
165             }
167             //- Add at tail of list
168             void append(const T& a)
169             {
170                 LListBase::append(new link(a));
171             }
173             //- Remove and return head
174             T removeHead()
175             {
176                 link* elmtPtr = static_cast<link*>(LListBase::removeHead());
177                 T data = elmtPtr->obj_;
178                 delete elmtPtr;
179                 return data;
180             }
182             //- Remove and return element
183             T remove(link* l)
184             {
185                 link* elmtPtr = static_cast<link*>(LListBase::remove(l));
186                 T data = elmtPtr->obj_;
187                 delete elmtPtr;
188                 return data;
189             }
191             //- Remove and return element specified by iterator
192             T remove(iterator& it)
193             {
194                 link* elmtPtr = static_cast<link*>(LListBase::remove(it));
195                 T data = elmtPtr->obj_;
196                 delete elmtPtr;
197                 return data;
198             }
200             //- Delete contents of list
201             void clear();
204     // Member operators
206         void operator=(const LList<LListBase, T>&);
209     // STL type definitions
211         //- Type of values the LList contains.
212         typedef T value_type;
214         //- Type that can be used for storing into value_type
215         //  objects.
216         typedef T& reference;
218         //- Type that can be used for storing into constant
219         //  LList::value_type objects.
220         typedef const T& const_reference;
222         //- The type that can represent the size of a LList.
223         typedef label size_type;
226     // STL iterator
228         typedef typename LListBase::iterator LListBase_iterator;
230         //- An STL-conforming iterator
231         class iterator
232         :
233             public LListBase_iterator
234         {
236         public:
238             //- Construct from base iterator
239             iterator
240             (
241                 LListBase_iterator baseIter
242             )
243             :
244                 LListBase_iterator(baseIter)
245             {}
248             // Member operators
250                 T& operator*()
251                 {
252                     return
253                         static_cast<link&>
254                         (LListBase_iterator::operator*()).obj_;
255                 }
257                 T& operator()()
258                 {
259                     return operator*();
260                 }
262                 iterator& operator++()
263                 {
264                     LListBase_iterator::operator++();
265                     return *this;
266                 }
267         };
270     // STL const_iterator
272         typedef typename LListBase::const_iterator LListBase_const_iterator;
274         //- An STL-conforming const_iterator
275         class const_iterator
276         :
277             public LListBase_const_iterator
278         {
280         public:
282             //- Construct from base const_iterator
283             const_iterator
284             (
285                 LListBase_const_iterator baseIter
286             )
287             :
288                 LListBase_const_iterator(baseIter)
289             {}
292             //- Construct from base iterator
293             const_iterator
294             (
295                 LListBase_iterator baseIter
296             )
297             :
298                 LListBase_const_iterator(baseIter)
299             {}
302             // Member operators
304                 const T& operator*()
305                 {
306                     return
307                         static_cast<const link&>
308                         (LListBase_const_iterator::operator*()).obj_;
309                 }
311                 const T& operator()()
312                 {
313                     return operator*();
314                 }
316                 const_iterator& operator++()
317                 {
318                     LListBase_const_iterator::operator++();
319                     return *this;
320                 }
321         };
324     // IOstream operators
326         friend Istream& operator>>
327         #ifndef __CINT__
328         <LListBase, T>
329         #endif
330         (
331             Istream&,
332             LList<LListBase, T>&
333         );
335         friend Ostream& operator<<
336         #ifndef __CINT__
337         <LListBase, T>
338         #endif
339         (
340             Ostream&,
341             const LList<LListBase, T>&
342         );
346 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
348 } // End namespace Foam
350 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
352 #ifdef NoRepository
353 #   include "LList.C"
354 #endif
356 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
358 #endif
360 // ************************************************************************* //