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

PtrListIO.C

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 \*---------------------------------------------------------------------------*/
00026 
00027 #include "PtrList.H"
00028 #include "SLList.H"
00029 #include "Istream.H"
00030 #include "Ostream.H"
00031 #include "INew.H"
00032 
00033 // * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * * //
00034 
00035 template<class T>
00036 template<class INew>
00037 void Foam::PtrList<T>::read(Istream& is, const INew& inewt)
00038 {
00039     is.fatalCheck("PtrList<T>::read(Istream&, const INew&)");
00040 
00041     token firstToken(is);
00042 
00043     is.fatalCheck
00044     (
00045         "PtrList<T>::read(Istream&, const INew&) : "
00046         "reading first token"
00047     );
00048 
00049     if (firstToken.isLabel())
00050     {
00051         // Read size of list
00052         label s = firstToken.labelToken();
00053 
00054         setSize(s);
00055 
00056         // Read beginning of contents
00057         char delimiter = is.readBeginList("PtrList");
00058 
00059         if (s)
00060         {
00061             if (delimiter == token::BEGIN_LIST)
00062             {
00063                 forAll(*this, i)
00064                 {
00065                     set(i, inewt(is));
00066 
00067                     is.fatalCheck
00068                     (
00069                         "PtrList<T>::read(Istream&, const INew&) : "
00070                         "reading entry"
00071                     );
00072                 }
00073             }
00074             else
00075             {
00076                 T* tPtr = inewt(is).ptr();
00077                 set(0, tPtr);
00078 
00079                 is.fatalCheck
00080                 (
00081                     "PtrList<T>::read(Istream&, const INew&) : "
00082                     "reading the single entry"
00083                 );
00084 
00085                 for (label i=1; i<s; i++)
00086                 {
00087                     set(i, tPtr->clone());
00088                 }
00089             }
00090         }
00091 
00092         // Read end of contents
00093         is.readEndList("PtrList");
00094     }
00095     else if (firstToken.isPunctuation())
00096     {
00097         if (firstToken.pToken() != token::BEGIN_LIST)
00098         {
00099             FatalIOErrorIn
00100             (
00101                 "PtrList<T>::read(Istream&, const INew&)",
00102                 is
00103             )   << "incorrect first token, '(', found " << firstToken.info()
00104                 << exit(FatalIOError);
00105         }
00106 
00107         SLList<T*> sllPtrs;
00108 
00109         token lastToken(is);
00110         while
00111         (
00112            !(
00113                 lastToken.isPunctuation()
00114              && lastToken.pToken() == token::END_LIST
00115             )
00116         )
00117         {
00118             is.putBack(lastToken);
00119             sllPtrs.append(inewt(is).ptr());
00120             is >> lastToken;
00121         }
00122 
00123         setSize(sllPtrs.size());
00124 
00125         label i = 0;
00126         for
00127         (
00128             typename SLList<T*>::iterator iter = sllPtrs.begin();
00129             iter != sllPtrs.end();
00130             ++iter
00131         )
00132         {
00133             set(i++, iter());
00134         }
00135     }
00136     else
00137     {
00138         FatalIOErrorIn
00139         (
00140             "PtrList<T>::read(Istream&, const INew&)",
00141             is
00142         )   << "incorrect first token, expected <int> or '(', found "
00143             << firstToken.info()
00144             << exit(FatalIOError);
00145     }
00146 }
00147 
00148 
00149 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00150 
00151 template<class T>
00152 template<class INew>
00153 Foam::PtrList<T>::PtrList(Istream& is, const INew& inewt)
00154 {
00155     read(is, inewt);
00156 }
00157 
00158 
00159 template<class T>
00160 Foam::PtrList<T>::PtrList(Istream& is)
00161 {
00162     read(is, INew<T>());
00163 }
00164 
00165 
00166 // * * * * * * * * * * * * * * * Istream Operator  * * * * * * * * * * * * * //
00167 
00168 template<class T>
00169 Foam::Istream& Foam::operator>>(Istream& is, PtrList<T>& L)
00170 {
00171     L.clear();
00172     L.read(is, INew<T>());
00173 
00174     return is;
00175 }
00176 
00177 
00178 // * * * * * * * * * * * * * * * Ostream Operators * * * * * * * * * * * * * //
00179 
00180 template<class T>
00181 Foam::Ostream& Foam::operator<<(Ostream& os, const PtrList<T>& L)
00182 {
00183     // Write size and start delimiter
00184     os << nl << L.size() << nl << token::BEGIN_LIST;
00185 
00186     // Write contents
00187     forAll(L, i)
00188     {
00189         os << nl << L[i];
00190     }
00191 
00192     // Write end delimiter
00193     os << nl << token::END_LIST << nl;
00194 
00195     // Check state of IOstream
00196     os.check("Ostream& operator<<(Ostream&, const PtrList&)");
00197 
00198     return os;
00199 }
00200 
00201 
00202 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd