OpenFOAM logo
Open Source CFD Toolkit

LList.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-2005 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     LList
00027 
00028 Description
00029     Template class for non-intrusive linked lists.
00030 
00031 SourceFiles
00032     LList.C
00033     LListIO.C
00034 
00035 \*---------------------------------------------------------------------------*/
00036 
00037 #ifndef LList_H
00038 #define LList_H
00039 
00040 #include "label.H"
00041 
00042 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00043 
00044 namespace Foam
00045 {
00046 
00047 class Istream;
00048 class Ostream;
00049 
00050 // * * * * * * Forward declaration of template friend fuctions * * * * * * * //
00051 
00052 template<class LListBase, class T> class LList;
00053 
00054 template<class LListBase, class T>
00055 Istream& operator>>
00056 (
00057     Istream&,
00058     LList<LListBase, T>&
00059 );
00060 
00061 template<class LListBase, class T>
00062 Ostream& operator<<
00063 (
00064     Ostream&,
00065     const LList<LListBase, T>&
00066 );
00067 
00068 
00069 /*---------------------------------------------------------------------------*\
00070                            Class LList Declaration
00071 \*---------------------------------------------------------------------------*/
00072 
00073 template<class LListBase, class T>
00074 class LList
00075 :
00076     public LListBase
00077 {
00078 
00079 public:
00080 
00081     // Forward declaration of STL iterators
00082 
00083         class iterator;
00084         friend class iterator;
00085 
00086         class const_iterator;
00087         friend class const_iterator;
00088 
00089 
00090     //- Link structure
00091     struct link
00092     :
00093         public LListBase::link
00094     {
00095         //- Stored object
00096         T obj_;
00097 
00098         //- Construct given object
00099         link(T a)
00100         :
00101             obj_(a)
00102         {}
00103     };
00104 
00105 
00106     // Constructors
00107 
00108         //- Null construct
00109         LList()
00110         {}
00111 
00112         //- Construct given initial T
00113         LList(T a)
00114         :
00115             LListBase(new link(a))
00116         {}
00117 
00118         //- Construct from Istream
00119         LList(Istream&);
00120 
00121         //- Construct as copy
00122         LList(const LList<LListBase, T>&);
00123 
00124 
00125     // Destructor
00126 
00127         ~LList();
00128 
00129 
00130     // Member Functions
00131 
00132         // Access
00133 
00134             //- Return the first entry added
00135             T& first()
00136             {
00137                 return static_cast<link*>(LListBase::first())->obj_;
00138             }
00139 
00140             //- Return const access to the first entry added
00141             const T& first() const
00142             {
00143                 return static_cast<const link*>(LListBase::first())->obj_;
00144             }
00145 
00146             //- Return the last entry added
00147             T& last()
00148             {
00149                 return static_cast<link*>(LListBase::last())->obj_;
00150             }
00151 
00152             //- Return const access to the last entry added
00153             const T& last() const
00154             {
00155                 return static_cast<const link*>(LListBase::last())->obj_;
00156             }
00157 
00158 
00159         // Edit
00160 
00161             //- Add at head of list
00162             void insert(const T& a)
00163             {
00164                 LListBase::insert(new link(a));
00165             }
00166 
00167             //- Add at tail of list
00168             void append(const T& a)
00169             {
00170                 LListBase::append(new link(a));
00171             }
00172 
00173             //- Return and remove head
00174             T removeHead()
00175             {
00176                 link* elmtPtr = static_cast<link*>(LListBase::removeHead());
00177                 T data = elmtPtr->obj_;
00178                 delete elmtPtr;
00179                 return data;
00180             }
00181 
00182             //- Delete contents of list
00183             void clear();
00184 
00185 
00186     // Member operators
00187 
00188         void operator=(const LList<LListBase, T>&);
00189 
00190 
00191     // STL type definitions
00192 
00193         //- Type of values the LList contains.
00194         typedef T value_type;
00195 
00196         //- Type that can be used for storing into value_type
00197         //  objects.
00198         typedef T& reference;
00199 
00200         //- Type that can be used for storing into constant
00201         //  LList::value_type objects.
00202         typedef const T& const_reference;
00203 
00204         //- The type that can represent the size of a LList.
00205         typedef label size_type;
00206 
00207 
00208     // STL iterator
00209 
00210         typedef typename LListBase::iterator LListBase_iterator;
00211 
00212         class iterator
00213         :
00214             public LListBase_iterator
00215         {
00216 
00217         public:
00218 
00219             //- Construct from base iterator
00220             iterator
00221             (
00222                 LListBase_iterator baseIter
00223             )
00224             :
00225                 LListBase_iterator(baseIter)
00226             {}
00227 
00228 
00229             // Member operators
00230 
00231                 T& operator*()
00232                 {
00233                     return
00234                         static_cast<link&>
00235                         (LListBase_iterator::operator*()).obj_;
00236                 }
00237 
00238                 T& operator()()
00239                 {
00240                     return operator*();
00241                 }
00242 
00243                 iterator& operator++()
00244                 {
00245                     LListBase_iterator::operator++();
00246                     return *this;
00247                 }
00248         };
00249 
00250 
00251     // STL const_iterator
00252 
00253         typedef typename LListBase::const_iterator LListBase_const_iterator;
00254 
00255         class const_iterator
00256         :
00257             public LListBase_const_iterator
00258         {
00259 
00260         public:
00261 
00262             //- Construct from base const_iterator
00263             const_iterator
00264             (
00265                 LListBase_const_iterator baseIter
00266             )
00267             :
00268                 LListBase_const_iterator(baseIter)
00269             {}
00270 
00271 
00272             //- Construct from base iterator
00273             const_iterator
00274             (
00275                 LListBase_iterator baseIter
00276             )
00277             :
00278                 LListBase_const_iterator(baseIter)
00279             {}
00280 
00281 
00282             // Member operators
00283 
00284                 const T& operator*()
00285                 {
00286                     return
00287                         static_cast<const link&>
00288                         (LListBase_const_iterator::operator*()).obj_;
00289                 }
00290 
00291                 const T& operator()()
00292                 {
00293                     return operator*();
00294                 }
00295 
00296                 const_iterator& operator++()
00297                 {
00298                     LListBase_const_iterator::operator++();
00299                     return *this;
00300                 }
00301         };
00302 
00303 
00304     // IOstream operators
00305 
00306         friend Istream& operator>> <LListBase, T>
00307         (
00308             Istream&,
00309             LList<LListBase, T>&
00310         );
00311 
00312         friend Ostream& operator<< <LListBase, T>
00313         (
00314             Ostream&,
00315             const LList<LListBase, T>&
00316         );
00317 };
00318 
00319 
00320 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00321 
00322 } // End namespace Foam
00323 
00324 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00325 
00326 #ifdef NoRepository
00327 #   include "LList.C"
00328 #endif
00329 
00330 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00331 
00332 #endif
00333 
00334 // ************************************************************************* //
For further information go to www.openfoam.org