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

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