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

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