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

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