foamVtkFormatterI.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2017-2018 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
29 
31 {
32  return *this;
33 }
34 
35 
37 {}
38 
39 
40 template<class... Args>
42 (
43  const std::string& text,
44  Args&&... args
45 )
46 {
47  if (text.length())
48  {
49  indent(); indent(4);
50  os_ << text << nl;
51  }
52 
53  xmlCommentLoop(std::forward<Args>(args)...);
54 }
55 
56 
57 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
58 
59 inline Foam::vtk::formatter::formatter(std::ostream& os)
60 :
61  os_(os),
62  xmlTags_(),
63  inTag_(false),
64  quote_(SINGLE_QUOTE)
65 {}
66 
67 
68 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
69 
70 inline std::ostream& Foam::vtk::formatter::os()
71 {
72  return os_;
73 }
74 
75 
77 {
78  indent(2*xmlTags_.size());
79 }
80 
81 
82 inline void Foam::vtk::formatter::indent(label n)
83 {
84  while (n-- > 0)
85  {
86  os_ << ' ';
87  }
88 }
89 
90 
92 {
93  if (canWriteToplevel("xml header"))
94  {
95  os_ << "<?xml version='1.0'?>" << nl;
96  }
97 
98  return *this;
99 }
100 
101 
102 template<class... Args>
104 (
105  const std::string& text,
106  Args&&... args
107 )
108 {
109  if (canWriteToplevel("xml comment"))
110  {
111  indent();
112  os_ << "<!--";
113 
114  if (sizeof...(Args))
115  {
116  os_ << nl;
117 
118  xmlCommentLoop(text, std::forward<Args>(args)...);
119 
120  indent(); indent(2);
121  }
122  else
123  {
124  os_ << ' ' << text << ' ';
125  }
126 
127  os_ << "-->" << nl;
128  }
129 
130  return *this;
131 }
132 
133 
134 template<class... Args>
136 (
137  const word& tagName,
138  Args&&... args
139 )
140 {
141  if (openTagImpl(tagName))
142  {
143  xmlAttr(std::forward<Args>(args)...);
144  }
145 
146  return *this;
147 }
148 
149 
150 template<class... Args>
152 (
153  vtk::fileTag t,
154  Args&&... args
155 )
156 {
157  return openTag(vtk::fileTagNames[t], std::forward<Args>(args)...);
158 }
159 
160 
161 template<class... Args>
163 (
164  const word& t,
165  Args&&... args
166 )
167 {
168  openTagImpl(t);
169  xmlAttr(std::forward<Args>(args)...);
170  closeTag();
171 
172  return *this;
173 }
174 
175 
176 template<class... Args>
178 (
179  vtk::fileTag t,
180  Args&&... args
181 )
182 {
183  return tag(vtk::fileTagNames[t], std::forward<Args>(args)...);
184 }
185 
186 
187 // Begin tags
188 
190 (
191  vtk::fileTag contentType,
192  const word& contentVersion,
193  bool leaveOpen
194 )
195 {
196  return beginVTKFile
197  (
198  vtk::fileTagNames[contentType],
199  contentVersion,
200  leaveOpen
201  );
202 }
203 
204 
206 (
207  vtk::fileTag contentType,
208  bool leaveOpen
209 )
210 {
211  return beginVTKFile
212  (
213  vtk::fileTagNames[contentType],
214  vtk::fileContentVersions[contentType],
215  leaveOpen
216  );
217 }
218 
219 
220 template<Foam::vtk::fileTag ContentType>
222 {
223  return beginVTKFile
224  (
225  vtk::fileTagNames[ContentType],
226  vtk::fileContentVersions[ContentType],
227  leaveOpen
228  );
229 }
230 
231 
232 template<class Type, Foam::direction nComp, int nTuple>
234 (
235  const vtk::dataArrayAttr& dataName,
236  uint64_t payLoad,
237  bool leaveOpen
238 )
239 {
240  return
241  beginDataArray<Type, nComp, nTuple>
242  (
243  vtk::dataArrayAttrNames[dataName],
244  payLoad,
245  leaveOpen
246  );
247 }
248 
249 
251 {
252  return tag(vtk::fileTag::CELL_DATA);
253 }
254 
255 
257 {
258  return tag(vtk::fileTag::POINT_DATA);
259 }
260 
261 
263 {
264  return tag(vtk::fileTag::FIELD_DATA);
265 }
266 
267 
268 // End tags
269 
271 {
272  return endTag(vtk::fileTagNames[t]);
273 }
274 
275 
277 {
278  return endTag(vtk::fileTag::CELL_DATA);
279 }
280 
281 
283 {
284  return endTag(vtk::fileTag::POINT_DATA);
285 }
286 
287 
289 {
290  return endTag(vtk::fileTag::FIELD_DATA);
291 }
292 
293 
295 {
296  return endTag(vtk::fileTag::DATA_ARRAY);
297 }
298 
299 
301 {
302  return endTag(vtk::fileTag::BLOCK);
303 }
304 
305 
307 {
308  return endTag(vtk::fileTag::PIECE);
309 }
310 
311 
313 {
314  return endTag(vtk::fileTag::VTK_FILE);
315 }
316 
317 
318 // Attributes
319 
320 template<class Type>
321 inline void Foam::vtk::formatter::writeAttr(const word& k, const Type& v)
322 {
323  os_ << ' ' << k << '=' << quote_ << v << quote_;
324 }
325 
326 
327 template<class... Args>
329 (
330  const word& k,
331  const std::string& v,
332  Args&&... args
333 )
334 {
335  if (!canWriteAttr(k)) return *this;
336 
337  writeAttr(k, v.c_str());
338  return xmlAttr(std::forward<Args>(args)...);
339 }
340 
341 
342 template<class... Args>
344 (
345  const word& k,
346  const int32_t v,
347  Args&&... args
348 )
349 {
350  if (!canWriteAttr(k)) return *this;
351 
352  writeAttr(k, v);
353  return xmlAttr(std::forward<Args>(args)...);
354 }
355 
356 
357 template<class... Args>
359 (
360  const word& k,
361  const int64_t v,
362  Args&&... args
363 )
364 {
365  if (!canWriteAttr(k)) return *this;
366 
367  writeAttr(k, v);
368  return xmlAttr(std::forward<Args>(args)...);
369 }
370 
371 
372 template<class... Args>
374 (
375  const word& k,
376  const uint64_t v,
377  Args&&... args
378 )
379 {
380  if (!canWriteAttr(k)) return *this;
381 
382  writeAttr(k, v);
383  return xmlAttr(std::forward<Args>(args)...);
384 }
385 
386 
387 template<class... Args>
389 (
390  const word& k,
391  const scalar v,
392  Args&&... args
393 )
394 {
395  if (!canWriteAttr(k)) return *this;
396 
397  writeAttr(k, v);
398  return xmlAttr(std::forward<Args>(args)...);
399 }
400 
401 
402 template<class... Args>
404 (
405  const vtk::fileAttr& k,
406  const std::string& v,
407  Args&&... args
408 )
409 {
410  if (!canWriteAttr(vtk::fileAttrNames[k])) return *this;
411 
412  writeAttr(vtk::fileAttrNames[k], v.c_str());
413  return xmlAttr(std::forward<Args>(args)...);
414 }
415 
416 
417 template<class... Args>
419 (
420  const vtk::fileAttr& k,
421  const int32_t v,
422  Args&&... args
423 )
424 {
425  if (!canWriteAttr(vtk::fileAttrNames[k])) return *this;
426 
427  writeAttr(vtk::fileAttrNames[k], v);
428  return xmlAttr(std::forward<Args>(args)...);
429 }
430 
431 
432 template<class... Args>
434 (
435  const vtk::fileAttr& k,
436  const int64_t v,
437  Args&&... args
438 )
439 {
440  if (!canWriteAttr(vtk::fileAttrNames[k])) return *this;
441 
442  writeAttr(vtk::fileAttrNames[k], v);
443  return xmlAttr(std::forward<Args>(args)...);
444 }
445 
446 
447 template<class... Args>
449 (
450  const vtk::fileAttr& k,
451  const uint64_t v,
452  Args&&... args
453 )
454 {
455  if (!canWriteAttr(vtk::fileAttrNames[k])) return *this;
456 
457  writeAttr(vtk::fileAttrNames[k], v);
458  return xmlAttr(std::forward<Args>(args)...);
459 }
460 
461 
462 template<class... Args>
464 (
465  const vtk::fileAttr& k,
466  const scalar v,
467  Args&&... args
468 )
469 {
470  if (!canWriteAttr(vtk::fileAttrNames[k])) return *this;
471 
472  writeAttr(vtk::fileAttrNames[k], v);
473  return xmlAttr(std::forward<Args>(args)...);
474 }
475 
476 
477 // ************************************************************************* //
Foam::vtk::dataArrayAttrNames
const Foam::Enum< dataArrayAttr > dataArrayAttrNames
Strings corresponding to the vtk XML DataArray attributes.
Foam::vtk::fileTag::VTK_FILE
"VTKFile"
Foam::vtk::formatter::openTag
formatter & openTag(const word &tagName, Args &&... args)
Start an XML tag, optionally with attributes.
Foam::vtk::formatter::endDataArray
virtual formatter & endDataArray()
End "DataArray" XML section.
Definition: foamVtkFormatterI.H:294
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::vtk::fileTag::DATA_ARRAY
"DataArray"
Foam::vtk::formatter::endCellData
virtual formatter & endCellData()
End "CellData" XML section.
Definition: foamVtkFormatterI.H:276
Foam::vtk::formatter::xmlComment
formatter & xmlComment(const std::string &text, Args &&... args)
Write XML comment (at the current indentation level)
Foam::vtk::fileTag::BLOCK
"Block"
Foam::vtk::formatter::endPiece
virtual formatter & endPiece()
End "Piece" XML section.
Definition: foamVtkFormatterI.H:306
Foam::vtk::fileAttr
fileAttr
Some common XML attributes for vtk files.
Definition: foamVtkCore.H:143
Foam::vtk::fileAttrNames
const Foam::Enum< fileAttr > fileAttrNames
Strings corresponding to the vtk XML attributes.
Foam::vtk::formatter::xmlAttr
formatter & xmlAttr()
No-op write XML attribute (for templating code).
Definition: foamVtkFormatterI.H:30
Foam::vtk::formatter::beginDataArray
formatter & beginDataArray(const word &dataName, uint64_t payLoad=npos, bool leaveOpen=false)
Begin "DataArray" XML section.
Foam::vtk::formatter::endPointData
virtual formatter & endPointData()
End "PointData" XML section.
Definition: foamVtkFormatterI.H:282
Foam::vtk::formatter::xmlCommentLoop
void xmlCommentLoop()
No-op XML comment loop (for templating code).
Definition: foamVtkFormatterI.H:36
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::vtk::formatter::os
std::ostream & os()
Access to the underlying output stream.
Definition: foamVtkFormatterI.H:70
Foam::vtk::formatter::indent
void indent()
Add indenting according to the current XML tag depth.
Definition: foamVtkFormatterI.H:76
Foam::vtk::formatter::formatter
formatter(std::ostream &os)
Construct and attach to an output stream.
Definition: foamVtkFormatterI.H:59
Foam::vtk::formatter::endVTKFile
virtual formatter & endVTKFile()
End "VTKFile" XML section.
Definition: foamVtkFormatterI.H:312
Foam::vtk::formatter::writeAttr
void writeAttr(const word &k, const Type &v)
Write XML key/value attribute pair (implementation).
Definition: foamVtkFormatterI.H:321
Foam::vtk::dataArrayAttr
dataArrayAttr
Some common names for XML DataArray entries.
Definition: foamVtkCore.H:159
Foam::vtk::formatter::endBlock
formatter & endBlock()
End "Block" XML section.
Definition: foamVtkFormatterI.H:300
os
OBJstream os(runTime.globalPath()/outputName)
Foam::vtk::fileTag::FIELD_DATA
"FieldData"
Foam::vtk::fileTag
fileTag
Some common XML tags for vtk files.
Definition: foamVtkCore.H:113
Foam::vtk::fileTagNames
const Foam::Enum< fileTag > fileTagNames
Strings corresponding to the vtk XML tags.
Foam::vtk::formatter::xmlHeader
formatter & xmlHeader()
Write XML header.
Definition: foamVtkFormatterI.H:91
Foam::indent
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:339
Foam::vtk::fileTag::POINT_DATA
"PointData"
Foam::vtk::formatter::beginFieldData
formatter & beginFieldData()
Begin "FieldData" XML section.
Definition: foamVtkFormatterI.H:262
Foam::vtk::fileContentVersions
const Foam::Enum< fileTag > fileContentVersions
Version string for some vtk XML file content types.
Foam::vtk::formatter::beginPointData
formatter & beginPointData()
Begin "PointData" XML section.
Definition: foamVtkFormatterI.H:256
Foam::vtk::formatter::endTag
formatter & endTag(const word &tagName=word::null)
An end XML tag, optional with sanity check.
Definition: foamVtkFormatter.C:125
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::vtk::formatter::tag
formatter & tag(const word &t, Args &&... args)
Write XML tag without any attributes. Combines openTag/closeTag.
k
label k
Boltzmann constant.
Definition: LISASMDCalcMethod2.H:41
Foam::vtk::formatter::beginCellData
formatter & beginCellData()
Begin "CellData" XML section.
Definition: foamVtkFormatterI.H:250
Foam::vtk::fileTag::CELL_DATA
"CellData"
Foam::vtk::fileTag::PIECE
"Piece"
Foam::vtk::formatter::beginVTKFile
formatter & beginVTKFile(const word &contentType, const word &contentVersion, const bool leaveOpen=false)
Definition: foamVtkFormatter.C:158
args
Foam::argList args(argc, argv)
Foam::vtk::formatter::endFieldData
virtual formatter & endFieldData()
End "FieldData" XML section.
Definition: foamVtkFormatterI.H:288
Foam::vtk::formatter
Abstract class for a VTK output stream formatter.
Definition: foamVtkFormatter.H:68