initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / containers / LinkedLists / accessTypes / LList / LList.H
blob0905f5543cbd7f272845f62d5d7d8afa0633b9ae
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::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"
41 #include "uLabel.H"
43 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 namespace Foam
48 class Istream;
49 class Ostream;
51 // Forward declaration of friend functions and operators
53 template<class LListBase, class T> class LList;
55 template<class LListBase, class T>
56 Istream& operator>>
58     Istream&,
59     LList<LListBase, T>&
62 template<class LListBase, class T>
63 Ostream& operator<<
65     Ostream&,
66     const LList<LListBase, T>&
70 /*---------------------------------------------------------------------------*\
71                             Class LList Declaration
72 \*---------------------------------------------------------------------------*/
74 template<class LListBase, class T>
75 class LList
77     public LListBase
80 public:
82     // Forward declaration of STL iterators
84         class iterator;
85         friend class iterator;
87         class const_iterator;
88         friend class const_iterator;
91     //- Link structure
92     struct link
93     :
94         public LListBase::link
95     {
96         //- Stored object
97         T obj_;
99         //- Construct given object
100         link(T a)
101         :
102             obj_(a)
103         {}
104     };
107     // Constructors
109         //- Null construct
110         LList()
111         {}
113         //- Construct given initial T
114         LList(T a)
115         :
116             LListBase(new link(a))
117         {}
119         //- Construct from Istream
120         LList(Istream&);
122         //- Construct as copy
123         LList(const LList<LListBase, T>&);
126     // Destructor
128         ~LList();
131     // Member Functions
133         // Access
135             //- Return the first entry added
136             T& first()
137             {
138                 return static_cast<link*>(LListBase::first())->obj_;
139             }
141             //- Return const access to the first entry added
142             const T& first() const
143             {
144                 return static_cast<const link*>(LListBase::first())->obj_;
145             }
147             //- Return the last entry added
148             T& last()
149             {
150                 return static_cast<link*>(LListBase::last())->obj_;
151             }
153             //- Return const access to the last entry added
154             const T& last() const
155             {
156                 return static_cast<const link*>(LListBase::last())->obj_;
157             }
160         // Edit
162             //- Add at head of list
163             void insert(const T& a)
164             {
165                 LListBase::insert(new link(a));
166             }
168             //- Add at tail of list
169             void append(const T& a)
170             {
171                 LListBase::append(new link(a));
172             }
174             //- Remove and return head
175             T removeHead()
176             {
177                 link* elmtPtr = static_cast<link*>(LListBase::removeHead());
178                 T data = elmtPtr->obj_;
179                 delete elmtPtr;
180                 return data;
181             }
183             //- Remove and return element
184             T remove(link* l)
185             {
186                 link* elmtPtr = static_cast<link*>(LListBase::remove(l));
187                 T data = elmtPtr->obj_;
188                 delete elmtPtr;
189                 return data;
190             }
192             //- Remove and return element specified by iterator
193             T remove(iterator& it)
194             {
195                 link* elmtPtr = static_cast<link*>(LListBase::remove(it));
196                 T data = elmtPtr->obj_;
197                 delete elmtPtr;
198                 return data;
199             }
201             //- Delete contents of list
202             void clear();
204             //- Transfer the contents of the argument into this List
205             //  and annull the argument list.
206             void transfer(LList<LListBase, T>&);
208     // Member operators
210         void operator=(const LList<LListBase, T>&);
213     // STL type definitions
215         //- Type of values the LList contains.
216         typedef T value_type;
218         //- Type that can be used for storing into value_type
219         //  objects.
220         typedef T& reference;
222         //- Type that can be used for storing into constant
223         //  LList::value_type objects.
224         typedef const T& const_reference;
226         //- The type that can represent the size of a LList.
227         typedef label size_type;
230     // STL iterator
232         typedef typename LListBase::iterator LListBase_iterator;
234         //- An STL-conforming iterator
235         class iterator
236         :
237             public LListBase_iterator
238         {
240         public:
242             //- Construct from base iterator
243             iterator
244             (
245                 LListBase_iterator baseIter
246             )
247             :
248                 LListBase_iterator(baseIter)
249             {}
252             // Member operators
254                 T& operator*()
255                 {
256                     return
257                         static_cast<link&>
258                         (LListBase_iterator::operator*()).obj_;
259                 }
261                 T& operator()()
262                 {
263                     return operator*();
264                 }
266                 iterator& operator++()
267                 {
268                     LListBase_iterator::operator++();
269                     return *this;
270                 }
271         };
274     // STL const_iterator
276         typedef typename LListBase::const_iterator LListBase_const_iterator;
278         //- An STL-conforming const_iterator
279         class const_iterator
280         :
281             public LListBase_const_iterator
282         {
284         public:
286             //- Construct from base const_iterator
287             const_iterator
288             (
289                 LListBase_const_iterator baseIter
290             )
291             :
292                 LListBase_const_iterator(baseIter)
293             {}
296             //- Construct from base iterator
297             const_iterator
298             (
299                 LListBase_iterator baseIter
300             )
301             :
302                 LListBase_const_iterator(baseIter)
303             {}
306             // Member operators
308                 const T& operator*()
309                 {
310                     return
311                         static_cast<const link&>
312                         (LListBase_const_iterator::operator*()).obj_;
313                 }
315                 const T& operator()()
316                 {
317                     return operator*();
318                 }
320                 const_iterator& operator++()
321                 {
322                     LListBase_const_iterator::operator++();
323                     return *this;
324                 }
325         };
328     // IOstream operators
330         friend Istream& operator>> <LListBase, T>
331         (
332             Istream&,
333             LList<LListBase, T>&
334         );
336         friend Ostream& operator<< <LListBase, T>
337         (
338             Ostream&,
339             const LList<LListBase, T>&
340         );
344 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
346 } // End namespace Foam
348 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
350 #ifdef NoRepository
351 #   include "LList.C"
352 #endif
354 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
356 #endif
358 // ************************************************************************* //