FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 Class
00025     Foam::LList
00026 
00027 Description
00028     Template class for non-intrusive linked lists.
00029 
00030 SourceFiles
00031     LList.C
00032     LListIO.C
00033 
00034 \*---------------------------------------------------------------------------*/
00035 
00036 #ifndef LList_H
00037 #define LList_H
00038 
00039 #include <OpenFOAM/label.H>
00040 #include <OpenFOAM/uLabel.H>
00041 
00042 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00043 
00044 namespace Foam
00045 {
00046 
00047 class Istream;
00048 class Ostream;
00049 
00050 // Forward declaration of friend functions and operators
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             //- Transfer the contents of the argument into this List
00204             //  and annull the argument list.
00205             void transfer(LList<LListBase, T>&);
00206 
00207     // Member operators
00208 
00209         void operator=(const LList<LListBase, T>&);
00210 
00211 
00212     // STL type definitions
00213 
00214         //- Type of values the LList contains.
00215         typedef T value_type;
00216 
00217         //- Type that can be used for storing into value_type
00218         //  objects.
00219         typedef T& reference;
00220 
00221         //- Type that can be used for storing into constant
00222         //  LList::value_type objects.
00223         typedef const T& const_reference;
00224 
00225         //- The type that can represent the size of a LList.
00226         typedef label size_type;
00227 
00228 
00229     // STL iterator
00230 
00231         typedef typename LListBase::iterator LListBase_iterator;
00232 
00233         //- An STL-conforming iterator
00234         class iterator
00235         :
00236             public LListBase_iterator
00237         {
00238 
00239         public:
00240 
00241             //- Construct from base iterator
00242             iterator
00243             (
00244                 LListBase_iterator baseIter
00245             )
00246             :
00247                 LListBase_iterator(baseIter)
00248             {}
00249 
00250 
00251             // Member operators
00252 
00253                 T& operator*()
00254                 {
00255                     return
00256                         static_cast<link&>
00257                         (LListBase_iterator::operator*()).obj_;
00258                 }
00259 
00260                 T& operator()()
00261                 {
00262                     return operator*();
00263                 }
00264 
00265                 iterator& operator++()
00266                 {
00267                     LListBase_iterator::operator++();
00268                     return *this;
00269                 }
00270         };
00271 
00272 
00273     // STL const_iterator
00274 
00275         typedef typename LListBase::const_iterator LListBase_const_iterator;
00276 
00277         //- An STL-conforming const_iterator
00278         class const_iterator
00279         :
00280             public LListBase_const_iterator
00281         {
00282 
00283         public:
00284 
00285             //- Construct from base const_iterator
00286             const_iterator
00287             (
00288                 LListBase_const_iterator baseIter
00289             )
00290             :
00291                 LListBase_const_iterator(baseIter)
00292             {}
00293 
00294 
00295             //- Construct from base iterator
00296             const_iterator
00297             (
00298                 LListBase_iterator baseIter
00299             )
00300             :
00301                 LListBase_const_iterator(baseIter)
00302             {}
00303 
00304 
00305             // Member operators
00306 
00307                 const T& operator*()
00308                 {
00309                     return
00310                         static_cast<const link&>
00311                         (LListBase_const_iterator::operator*()).obj_;
00312                 }
00313 
00314                 const T& operator()()
00315                 {
00316                     return operator*();
00317                 }
00318 
00319                 const_iterator& operator++()
00320                 {
00321                     LListBase_const_iterator::operator++();
00322                     return *this;
00323                 }
00324         };
00325 
00326 
00327     // IOstream operators
00328 
00329         friend Istream& operator>> <LListBase, T>
00330         (
00331             Istream&,
00332             LList<LListBase, T>&
00333         );
00334 
00335         friend Ostream& operator<< <LListBase, T>
00336         (
00337             Ostream&,
00338             const LList<LListBase, T>&
00339         );
00340 };
00341 
00342 
00343 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00344 
00345 } // End namespace Foam
00346 
00347 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00348 
00349 #ifdef NoRepository
00350 #   include "LList.C"
00351 #endif
00352 
00353 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00354 
00355 #endif
00356 
00357 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines