FreeFOAM The Cross-Platform CFD Toolkit
UListIO.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "UList.H"
27 #include <OpenFOAM/Ostream.H>
28 #include <OpenFOAM/token.H>
29 #include <OpenFOAM/contiguous.H>
30 
31 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
32 
33 template<class T>
35 {
36  if
37  (
38  size()
39  && token::compound::isCompound
40  (
41  "List<" + word(pTraits<T>::typeName) + '>'
42  )
43  )
44  {
45  os << word("List<" + word(pTraits<T>::typeName) + '>') << " ";
46  }
47 
48  os << *this;
49 }
50 
51 
52 template<class T>
53 void Foam::UList<T>::writeEntry(const word& keyword, Ostream& os) const
54 {
55  os.writeKeyword(keyword);
56  writeEntry(os);
57  os << token::END_STATEMENT << endl;
58 }
59 
60 
61 template<class T>
62 Foam::Ostream& Foam::operator<<(Foam::Ostream& os, const Foam::UList<T>& L)
63 {
64  // Write list contents depending on data format
65  if (os.format() == IOstream::ASCII || !contiguous<T>())
66  {
67  bool uniform = false;
68 
69  if (L.size() > 1 && contiguous<T>())
70  {
71  uniform = true;
72 
73  forAll(L, i)
74  {
75  if (L[i] != L[0])
76  {
77  uniform = false;
78  break;
79  }
80  }
81  }
82 
83  if (uniform)
84  {
85  // Write size and start delimiter
86  os << L.size() << token::BEGIN_BLOCK;
87 
88  // Write contents
89  os << L[0];
90 
91  // Write end delimiter
92  os << token::END_BLOCK;
93  }
94  else if (L.size() < 11 && contiguous<T>())
95  {
96  // Write size and start delimiter
97  os << L.size() << token::BEGIN_LIST;
98 
99  // Write contents
100  forAll(L, i)
101  {
102  if (i > 0) os << token::SPACE;
103  os << L[i];
104  }
105 
106  // Write end delimiter
107  os << token::END_LIST;
108  }
109  else
110  {
111  // Write size and start delimiter
112  os << nl << L.size() << nl << token::BEGIN_LIST;
113 
114  // Write contents
115  forAll(L, i)
116  {
117  os << nl << L[i];
118  }
119 
120  // Write end delimiter
121  os << nl << token::END_LIST << nl;
122  }
123  }
124  else
125  {
126  os << nl << L.size() << nl;
127  if (L.size())
128  {
129  os.write(reinterpret_cast<const char*>(L.v_), L.byteSize());
130  }
131  }
132 
133  // Check state of IOstream
134  os.check("Ostream& operator<<(Ostream&, const UList&)");
135 
136  return os;
137 }
138 
139 
140 // ************************ vim: set sw=4 sts=4 et: ************************ //