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

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