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

UIndirectListIO.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 "UIndirectList.H"
00028 #include "Ostream.H"
00029 #include "token.H"
00030 #include "contiguous.H"
00031 
00032 // * * * * * * * * * * * * * * * Ostream Operator *  * * * * * * * * * * * * //
00033 
00034 template<class T>
00035 Foam::Ostream& Foam::operator<<
00036 (
00037     Foam::Ostream& os,
00038     const Foam::UIndirectList<T>& L
00039 )
00040 {
00041     // Write list contents depending on data format
00042     if (os.format() == IOstream::ASCII || !contiguous<T>())
00043     {
00044         bool uniform = false;
00045 
00046         if (L.size() > 1 && contiguous<T>())
00047         {
00048             uniform = true;
00049 
00050             forAll(L, i)
00051             {
00052                 if (L[i] != L[0])
00053                 {
00054                     uniform = false;
00055                     break;
00056                 }
00057             }
00058         }
00059 
00060         if (uniform)
00061         {
00062             // Write size and start delimiter
00063             os << L.size() << token::BEGIN_BLOCK;
00064 
00065             // Write contents
00066             os << L[0];
00067 
00068             // Write end delimiter
00069             os << token::END_BLOCK;
00070         }
00071         else if (L.size() < 11 && contiguous<T>())
00072         {
00073             // Write size and start delimiter
00074             os << L.size() << token::BEGIN_LIST;
00075 
00076             // Write contents
00077             forAll(L, i)
00078             {
00079                 if (i) os << token::SPACE;
00080                 os << L[i];
00081             }
00082 
00083             // Write end delimiter
00084             os << token::END_LIST;
00085         }
00086         else
00087         {
00088             // Write size and start delimiter
00089             os << nl << L.size() << nl << token::BEGIN_LIST;
00090 
00091             // Write contents
00092             forAll(L, i)
00093             {
00094                 os << nl << L[i];
00095             }
00096 
00097             // Write end delimiter
00098             os << nl << token::END_LIST << nl;
00099         }
00100     }
00101     else
00102     {
00103         // this is annoying, and wasteful, but there's currently no alternative
00104 
00105         os << nl << L.size() << nl;
00106 
00107         if (L.size())
00108         {
00109             List<T> lst = L();
00110 
00111             os.write
00112             (
00113                 reinterpret_cast<const char*>(lst.cdata()),
00114                 lst.byteSize()
00115             );
00116         }
00117     }
00118 
00119     // Check state of IOstream
00120     os.check("Ostream& operator<<(Ostream&, const UIndirectList&)");
00121 
00122     return os;
00123 }
00124 
00125 
00126 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd