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

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