OpenFOAM logo
The Open Source CFD Toolbox
  Programmer’s C++ Doxygen 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-2007 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             //- Remove and return 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             //- Remove and return element
00183             T remove(link* l)
00184             {
00185                 link* elmtPtr = static_cast<link*>(LListBase::remove(l));
00186                 T data = elmtPtr->obj_;
00187                 delete elmtPtr;
00188                 return data;
00189             }
00190 
00191             //- Remove and return element specified by iterator
00192             T remove(iterator& it)
00193             {
00194                 link* elmtPtr = static_cast<link*>(LListBase::remove(it));
00195                 T data = elmtPtr->obj_;
00196                 delete elmtPtr;
00197                 return data;
00198             }
00199 
00200             //- Delete contents of list
00201             void clear();
00202 
00203 
00204     // Member operators
00205 
00206         void operator=(const LList<LListBase, T>&);
00207 
00208 
00209     // STL type definitions
00210 
00211         //- Type of values the LList contains.
00212         typedef T value_type;
00213 
00214         //- Type that can be used for storing into value_type
00215         //  objects.
00216         typedef T& reference;
00217 
00218         //- Type that can be used for storing into constant
00219         //  LList::value_type objects.
00220         typedef const T& const_reference;
00221 
00222         //- The type that can represent the size of a LList.
00223         typedef label size_type;
00224 
00225 
00226     // STL iterator
00227 
00228         typedef typename LListBase::iterator LListBase_iterator;
00229 
00230         class iterator
00231         :
00232             public LListBase_iterator
00233         {
00234 
00235         public:
00236 
00237             //- Construct from base iterator
00238             iterator
00239             (
00240                 LListBase_iterator baseIter
00241             )
00242             :
00243                 LListBase_iterator(baseIter)
00244             {}
00245 
00246 
00247             // Member operators
00248 
00249                 T& operator*()
00250                 {
00251                     return
00252                         static_cast<link&>
00253                         (LListBase_iterator::operator*()).obj_;
00254                 }
00255 
00256                 T& operator()()
00257                 {
00258                     return operator*();
00259                 }
00260 
00261                 iterator& operator++()
00262                 {
00263                     LListBase_iterator::operator++();
00264                     return *this;
00265                 }
00266         };
00267 
00268 
00269     // STL const_iterator
00270 
00271         typedef typename LListBase::const_iterator LListBase_const_iterator;
00272 
00273         class const_iterator
00274         :
00275             public LListBase_const_iterator
00276         {
00277 
00278         public:
00279 
00280             //- Construct from base const_iterator
00281             const_iterator
00282             (
00283                 LListBase_const_iterator baseIter
00284             )
00285             :
00286                 LListBase_const_iterator(baseIter)
00287             {}
00288 
00289 
00290             //- Construct from base iterator
00291             const_iterator
00292             (
00293                 LListBase_iterator baseIter
00294             )
00295             :
00296                 LListBase_const_iterator(baseIter)
00297             {}
00298 
00299 
00300             // Member operators
00301 
00302                 const T& operator*()
00303                 {
00304                     return
00305                         static_cast<const link&>
00306                         (LListBase_const_iterator::operator*()).obj_;
00307                 }
00308 
00309                 const T& operator()()
00310                 {
00311                     return operator*();
00312                 }
00313 
00314                 const_iterator& operator++()
00315                 {
00316                     LListBase_const_iterator::operator++();
00317                     return *this;
00318                 }
00319         };
00320 
00321 
00322     // IOstream operators
00323 
00324         friend Istream& operator>> <LListBase, T>
00325         (
00326             Istream&,
00327             LList<LListBase, T>&
00328         );
00329 
00330         friend Ostream& operator<< <LListBase, T>
00331         (
00332             Ostream&,
00333             const LList<LListBase, T>&
00334         );
00335 };
00336 
00337 
00338 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00339 
00340 } // End namespace Foam
00341 
00342 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00343 
00344 #ifdef NoRepository
00345 #   include "LList.C"
00346 #endif
00347 
00348 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00349 
00350 #endif
00351 
00352 // ************************************************************************* //
Copyright © 2000-2007 OpenCFD Ltd