OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

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