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

FixedListIO.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 "FixedList.H"
00028 #include "Istream.H"
00029 #include "Ostream.H"
00030 #include "token.H"
00031 #include "contiguous.H"
00032 
00033 // * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
00034 
00035 template<class T, unsigned Size>
00036 Foam::FixedList<T, Size>::FixedList(Istream& is)
00037 {
00038     operator>>(is, *this);
00039 }
00040 
00041 
00042 template<class T, unsigned Size>
00043 Foam::Istream& Foam::operator>>(Foam::Istream& is, FixedList<T, Size>& L)
00044 {
00045     is.fatalCheck("operator>>(Istream&, FixedList<T, Size>&)");
00046 
00047     if (is.format() == IOstream::ASCII || !contiguous<T>())
00048     {
00049         token firstToken(is);
00050 
00051         is.fatalCheck
00052         (
00053             "operator>>(Istream&, FixedList<T, Size>&) : reading first token"
00054         );
00055 
00056         if (firstToken.isCompound())
00057         {
00058             L = dynamicCast<token::Compound<List<T> > >
00059             (
00060                 firstToken.transferCompoundToken()
00061             );
00062         }
00063         else if (firstToken.isLabel())
00064         {
00065             label s = firstToken.labelToken();
00066 
00067             // Set list length to that read
00068             L.checkSize(s);
00069         }
00070         else if (!firstToken.isPunctuation())
00071         {
00072             FatalIOErrorIn("operator>>(Istream&, FixedList<T, Size>&)", is)
00073                 << "incorrect first token, expected <label> "
00074                    "or '(' or '{', found "
00075                 << firstToken.info()
00076                 << exit(FatalIOError);
00077         }
00078         else
00079         {
00080             // Putback the opening bracket
00081             is.putBack(firstToken);
00082         }
00083 
00084         // Read beginning of contents
00085         char delimiter = is.readBeginList("FixedList");
00086 
00087         if (delimiter == token::BEGIN_LIST)
00088         {
00089             for (register unsigned i=0; i<Size; i++)
00090             {
00091                 is >> L[i];
00092 
00093                 is.fatalCheck
00094                 (
00095                     "operator>>(Istream&, FixedList<T, Size>&) : "
00096                     "reading entry"
00097                 );
00098             }
00099         }
00100         else
00101         {
00102             T element;
00103             is >> element;
00104 
00105             is.fatalCheck
00106             (
00107                 "operator>>(Istream&, FixedList<T, Size>&) : "
00108                 "reading the single entry"
00109             );
00110 
00111             for (register unsigned i=0; i<Size; i++)
00112             {
00113                 L[i] = element;
00114             }
00115         }
00116 
00117         // Read end of contents
00118         is.readEndList("FixedList");
00119     }
00120     else
00121     {
00122         is.read(reinterpret_cast<char*>(L.data()), Size*sizeof(T));
00123 
00124         is.fatalCheck
00125         (
00126             "operator>>(Istream&, FixedList<T, Size>&) : "
00127             "reading the binary block"
00128         );
00129     }
00130 
00131     return is;
00132 }
00133 
00134 
00135 // * * * * * * * * * * * * * * * Ostream Operator *  * * * * * * * * * * * * //
00136 
00137 template<class T, unsigned Size>
00138 void Foam::FixedList<T, Size>::writeEntry(Ostream& os) const
00139 {
00140     if
00141     (
00142         size()
00143      && token::compound::isCompound
00144         (
00145             "List<" + word(pTraits<T>::typeName) + '>'
00146         )
00147     )
00148     {
00149         os  << word("List<" + word(pTraits<T>::typeName) + '>') << " ";
00150     }
00151 
00152     os << *this;
00153 }
00154 
00155 
00156 template<class T, unsigned Size>
00157 void Foam::FixedList<T, Size>::writeEntry
00158 (
00159     const word& keyword,
00160     Ostream& os
00161 ) const
00162 {
00163     os.writeKeyword(keyword);
00164     writeEntry(os);
00165     os << token::END_STATEMENT << endl;
00166 }
00167 
00168 
00169 template<class T, unsigned Size>
00170 Foam::Ostream& Foam::operator<<(Ostream& os, const FixedList<T, Size>& L)
00171 {
00172     // Write list contents depending on data format
00173     if (os.format() == IOstream::ASCII || !contiguous<T>())
00174     {
00175         bool uniform = false;
00176 
00177         if (Size > 1 && contiguous<T>())
00178         {
00179             uniform = true;
00180 
00181             forAll(L, i)
00182             {
00183                 if (L[i] != L[0])
00184                 {
00185                     uniform = false;
00186                     break;
00187                 }
00188             }
00189         }
00190 
00191         if (uniform)
00192         {
00193             // Write size (so it is valid dictionary entry) and start delimiter
00194             os << L.size() << token::BEGIN_BLOCK;
00195 
00196             // Write contents
00197             os << L[0];
00198 
00199             // Write end delimiter
00200             os << token::END_BLOCK;
00201         }
00202         else if (Size < 11 && contiguous<T>())
00203         {
00204             // Write start delimiter
00205             os << token::BEGIN_LIST;
00206 
00207             // Write contents
00208             forAll(L, i)
00209             {
00210                 if (i > 0) os << token::SPACE;
00211                 os << L[i];
00212             }
00213 
00214             // Write end delimiter
00215             os << token::END_LIST;
00216         }
00217         else
00218         {
00219             // Write start delimiter
00220             os << nl << token::BEGIN_LIST;
00221 
00222             // Write contents
00223             forAll(L, i)
00224             {
00225                 os << nl << L[i];
00226             }
00227 
00228             // Write end delimiter
00229             os << nl << token::END_LIST << nl;
00230         }
00231     }
00232     else
00233     {
00234         os.write(reinterpret_cast<const char*>(L.cdata()), Size*sizeof(T));
00235     }
00236 
00237     // Check state of IOstream
00238     os.check("Ostream& operator<<(Ostream&, const FixedList&)");
00239 
00240     return os;
00241 }
00242 
00243 
00244 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd