FreeFOAM The Cross-Platform CFD Toolkit
FixedListIO.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 "FixedList.H"
27 #include <OpenFOAM/Istream.H>
28 #include <OpenFOAM/Ostream.H>
29 #include <OpenFOAM/token.H>
30 #include <OpenFOAM/contiguous.H>
31 
32 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
33 
34 template<class T, unsigned Size>
36 {
37  operator>>(is, *this);
38 }
39 
40 
41 template<class T, unsigned Size>
43 {
44  is.fatalCheck("operator>>(Istream&, FixedList<T, Size>&)");
45 
46  if (is.format() == IOstream::ASCII || !contiguous<T>())
47  {
48  token firstToken(is);
49 
50  is.fatalCheck
51  (
52  "operator>>(Istream&, FixedList<T, Size>&) : reading first token"
53  );
54 
55  if (firstToken.isCompound())
56  {
57  L = dynamicCast<token::Compound<List<T> > >
58  (
59  firstToken.transferCompoundToken()
60  );
61  }
62  else if (firstToken.isLabel())
63  {
64  label s = firstToken.labelToken();
65 
66  // Set list length to that read
67  L.checkSize(s);
68  }
69  else if (!firstToken.isPunctuation())
70  {
71  FatalIOErrorIn("operator>>(Istream&, FixedList<T, Size>&)", is)
72  << "incorrect first token, expected <label> "
73  "or '(' or '{', found "
74  << firstToken.info()
75  << exit(FatalIOError);
76  }
77  else
78  {
79  // Putback the opening bracket
80  is.putBack(firstToken);
81  }
82 
83  // Read beginning of contents
84  char delimiter = is.readBeginList("FixedList");
85 
86  if (delimiter == token::BEGIN_LIST)
87  {
88  for (register unsigned i=0; i<Size; i++)
89  {
90  is >> L[i];
91 
92  is.fatalCheck
93  (
94  "operator>>(Istream&, FixedList<T, Size>&) : "
95  "reading entry"
96  );
97  }
98  }
99  else
100  {
101  T element;
102  is >> element;
103 
104  is.fatalCheck
105  (
106  "operator>>(Istream&, FixedList<T, Size>&) : "
107  "reading the single entry"
108  );
109 
110  for (register unsigned i=0; i<Size; i++)
111  {
112  L[i] = element;
113  }
114  }
115 
116  // Read end of contents
117  is.readEndList("FixedList");
118  }
119  else
120  {
121  is.read(reinterpret_cast<char*>(L.data()), Size*sizeof(T));
122 
123  is.fatalCheck
124  (
125  "operator>>(Istream&, FixedList<T, Size>&) : "
126  "reading the binary block"
127  );
128  }
129 
130  return is;
131 }
132 
133 
134 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
135 
136 template<class T, unsigned Size>
138 {
139  if
140  (
141  size()
142  && token::compound::isCompound
143  (
144  "List<" + word(pTraits<T>::typeName) + '>'
145  )
146  )
147  {
148  os << word("List<" + word(pTraits<T>::typeName) + '>') << " ";
149  }
150 
151  os << *this;
152 }
153 
154 
155 template<class T, unsigned Size>
157 (
158  const word& keyword,
159  Ostream& os
160 ) const
161 {
162  os.writeKeyword(keyword);
163  writeEntry(os);
164  os << token::END_STATEMENT << endl;
165 }
166 
167 
168 template<class T, unsigned Size>
169 Foam::Ostream& Foam::operator<<(Ostream& os, const FixedList<T, Size>& L)
170 {
171  // Write list contents depending on data format
172  if (os.format() == IOstream::ASCII || !contiguous<T>())
173  {
174  bool uniform = false;
175 
176  if (Size > 1 && contiguous<T>())
177  {
178  uniform = true;
179 
180  forAll(L, i)
181  {
182  if (L[i] != L[0])
183  {
184  uniform = false;
185  break;
186  }
187  }
188  }
189 
190  if (uniform)
191  {
192  // Write size (so it is valid dictionary entry) and start delimiter
193  os << L.size() << token::BEGIN_BLOCK;
194 
195  // Write contents
196  os << L[0];
197 
198  // Write end delimiter
199  os << token::END_BLOCK;
200  }
201  else if (Size < 11 && contiguous<T>())
202  {
203  // Write start delimiter
204  os << token::BEGIN_LIST;
205 
206  // Write contents
207  forAll(L, i)
208  {
209  if (i > 0) os << token::SPACE;
210  os << L[i];
211  }
212 
213  // Write end delimiter
214  os << token::END_LIST;
215  }
216  else
217  {
218  // Write start delimiter
219  os << nl << token::BEGIN_LIST;
220 
221  // Write contents
222  forAll(L, i)
223  {
224  os << nl << L[i];
225  }
226 
227  // Write end delimiter
228  os << nl << token::END_LIST << nl;
229  }
230  }
231  else
232  {
233  os.write(reinterpret_cast<const char*>(L.cdata()), Size*sizeof(T));
234  }
235 
236  // Check state of IOstream
237  os.check("Ostream& operator<<(Ostream&, const FixedList&)");
238 
239  return os;
240 }
241 
242 
243 // ************************ vim: set sw=4 sts=4 et: ************************ //