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-2025 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 
36 template<class... Args>
38 (
39  const std::string& text,
40  Args&&... args
41 )
42 {
43  if (text.length())
44  {
45  indent(); indent(4);
46  os_ << text << nl;
47  }
48 
49  if constexpr (sizeof...(Args))
50  {
51  xmlCommentLoop(std::forward<Args>(args)...);
52  }
53 }
54 
55 
56 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
57 
58 inline Foam::vtk::formatter::formatter(std::ostream& os)
59 :
60  os_(os),
61  xmlTags_(),
62  inTag_(false),
63  quote_(SINGLE_QUOTE)
64 {}
65 
66 
67 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
68 
69 inline std::ostream& Foam::vtk::formatter::os() noexcept
70 {
71  return os_;
72 }
73 
74 
75 inline void Foam::vtk::formatter::indent()
76 {
77  indent(2*xmlTags_.size());
78 }
79 
80 
81 inline void Foam::vtk::formatter::indent(label n)
82 {
83  while (n-- > 0)
84  {
85  os_ << ' ';
86  }
87 }
88 
89 
91 {
92  if (canWriteToplevel("xml header"))
93  {
94  os_ << "<?xml version='1.0'?>" << nl;
95  }
96 
97  return *this;
98 }
99 
100 
101 template<class... Args>
103 (
104  const std::string& text,
105  Args&&... args
106 )
107 {
108  if (canWriteToplevel("xml comment"))
109  {
110  indent();
111  os_ << "<!--";
112 
113  if constexpr (sizeof...(Args))
114  {
115  os_ << nl;
116 
117  xmlCommentLoop(text, std::forward<Args>(args)...);
118 
119  indent(); indent(2);
120  }
121  else
122  {
123  os_ << ' ' << text << ' ';
124  }
125 
126  os_ << "-->" << nl;
127  }
129  return *this;
130 }
131 
132 
133 template<class... Args>
135 (
136  const word& tagName,
137  Args&&... args
138 )
139 {
140  if (openTagImpl(tagName))
141  {
142  xmlAttr(std::forward<Args>(args)...);
143  }
145  return *this;
146 }
147 
148 
149 template<class... Args>
151 (
152  vtk::fileTag t,
153  Args&&... args
154 )
155 {
156  return openTag(vtk::fileTagNames[t], std::forward<Args>(args)...);
157 }
158 
159 
160 template<class... Args>
162 (
163  const word& t,
164  Args&&... args
165 )
166 {
167  openTagImpl(t);
168  xmlAttr(std::forward<Args>(args)...);
169  closeTag();
171  return *this;
172 }
173 
174 
175 template<class... Args>
177 (
178  vtk::fileTag t,
179  Args&&... args
180 )
181 {
182  return tag(vtk::fileTagNames[t], std::forward<Args>(args)...);
183 }
184 
185 
186 // Begin tags
187 
189 (
190  vtk::fileTag contentType,
191  const word& contentVersion,
192  bool leaveOpen
193 )
194 {
195  return beginVTKFile
196  (
197  vtk::fileTagNames[contentType],
198  contentVersion,
199  leaveOpen
200  );
201 }
202 
203 
205 (
206  vtk::fileTag contentType,
207  bool leaveOpen
208 )
209 {
210  return beginVTKFile
211  (
212  vtk::fileTagNames[contentType],
214  leaveOpen
215  );
216 }
217 
218 
219 template<Foam::vtk::fileTag ContentType>
221 {
222  return beginVTKFile
223  (
224  vtk::fileTagNames[ContentType],
225  vtk::fileContentVersions[ContentType],
226  leaveOpen
227  );
228 }
229 
230 
231 template<class Type, Foam::direction nComp, int nTuple>
233 (
234  const vtk::dataArrayAttr& dataName,
235  uint64_t payLoad,
236  bool leaveOpen
237 )
238 {
239  return
240  beginDataArray<Type, nComp, nTuple>
241  (
243  payLoad,
244  leaveOpen
245  );
246 }
247 
250 {
251  return tag(vtk::fileTag::CELL_DATA);
252 }
253 
256 {
257  return tag(vtk::fileTag::POINT_DATA);
258 }
259 
260 
262 {
263  return tag(vtk::fileTag::FIELD_DATA);
264 }
265 
266 
267 // End tags
270 {
271  return endTag(vtk::fileTagNames[t]);
272 }
273 
276 {
277  return endTag(vtk::fileTag::CELL_DATA);
278 }
279 
282 {
283  return endTag(vtk::fileTag::POINT_DATA);
284 }
285 
288 {
289  return endTag(vtk::fileTag::FIELD_DATA);
290 }
291 
294 {
295  return endTag(vtk::fileTag::DATA_ARRAY);
296 }
297 
300 {
301  return endTag(vtk::fileTag::BLOCK);
302 }
303 
306 {
307  return endTag(vtk::fileTag::PIECE);
308 }
309 
310 
312 {
313  return endTag(vtk::fileTag::VTK_FILE);
314 }
315 
316 
317 // Attributes
318 
319 template<class Type>
320 inline void Foam::vtk::formatter::writeAttr(const word& k, const Type& v)
321 {
322  os_ << ' ' << k << '=' << quote_ << v << quote_;
323 }
324 
325 
326 template<class... Args>
328 (
329  const word& k,
330  const std::string& v,
331  Args&&... args
332 )
333 {
334  if (!canWriteAttr(k)) return *this;
335 
336  writeAttr(k, v.c_str());
337  return xmlAttr(std::forward<Args>(args)...);
338 }
339 
340 
341 template<class... Args>
343 (
344  const word& k,
345  const int32_t v,
346  Args&&... args
347 )
348 {
349  if (!canWriteAttr(k)) return *this;
350 
351  writeAttr(k, v);
352  return xmlAttr(std::forward<Args>(args)...);
353 }
354 
355 
356 template<class... Args>
358 (
359  const word& k,
360  const int64_t v,
361  Args&&... args
362 )
363 {
364  if (!canWriteAttr(k)) return *this;
365 
366  writeAttr(k, v);
367  return xmlAttr(std::forward<Args>(args)...);
368 }
369 
370 
371 template<class... Args>
373 (
374  const word& k,
375  const uint64_t v,
376  Args&&... args
377 )
378 {
379  if (!canWriteAttr(k)) return *this;
380 
381  writeAttr(k, v);
382  return xmlAttr(std::forward<Args>(args)...);
383 }
384 
385 
386 template<class... Args>
388 (
389  const word& k,
390  const scalar v,
391  Args&&... args
392 )
393 {
394  if (!canWriteAttr(k)) return *this;
395 
396  writeAttr(k, v);
397  return xmlAttr(std::forward<Args>(args)...);
398 }
399 
400 
401 template<class... Args>
403 (
404  const vtk::fileAttr& k,
405  const std::string& v,
406  Args&&... args
407 )
408 {
409  if (!canWriteAttr(vtk::fileAttrNames[k])) return *this;
410 
411  writeAttr(vtk::fileAttrNames[k], v.c_str());
412  return xmlAttr(std::forward<Args>(args)...);
413 }
414 
415 
416 template<class... Args>
418 (
419  const vtk::fileAttr& k,
420  const int32_t v,
421  Args&&... args
422 )
423 {
424  if (!canWriteAttr(vtk::fileAttrNames[k])) return *this;
425 
426  writeAttr(vtk::fileAttrNames[k], v);
427  return xmlAttr(std::forward<Args>(args)...);
428 }
429 
430 
431 template<class... Args>
433 (
434  const vtk::fileAttr& k,
435  const int64_t v,
436  Args&&... args
437 )
438 {
439  if (!canWriteAttr(vtk::fileAttrNames[k])) return *this;
440 
441  writeAttr(vtk::fileAttrNames[k], v);
442  return xmlAttr(std::forward<Args>(args)...);
443 }
444 
445 
446 template<class... Args>
448 (
449  const vtk::fileAttr& k,
450  const uint64_t v,
451  Args&&... args
452 )
453 {
454  if (!canWriteAttr(vtk::fileAttrNames[k])) return *this;
455 
456  writeAttr(vtk::fileAttrNames[k], v);
457  return xmlAttr(std::forward<Args>(args)...);
458 }
459 
460 
461 template<class... Args>
463 (
464  const vtk::fileAttr& k,
465  const scalar v,
466  Args&&... args
467 )
468 {
469  if (!canWriteAttr(vtk::fileAttrNames[k])) return *this;
470 
471  writeAttr(vtk::fileAttrNames[k], v);
472  return xmlAttr(std::forward<Args>(args)...);
473 }
474 
475 
476 // ************************************************************************* //
formatter & beginPointData()
Begin "PointData" XML section.
formatter & endTag(const word &tagName=word::null)
An end XML tag, optional with sanity check.
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:491
Abstract class for a VTK output stream formatter.
formatter(std::ostream &os)
Construct and attach to an output stream.
virtual formatter & endPointData()
End "PointData" XML section.
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
formatter & xmlComment(const std::string &text, Args &&... args)
Write XML comment (at the current indentation level)
void indent()
Add indenting according to the current XML tag depth.
virtual formatter & endFieldData()
End "FieldData" XML section.
label k
Boltzmann constant.
const Foam::Enum< fileTag > fileContentVersions
Version string for some vtk XML file content types.
formatter & beginDataArray(const word &dataName, uint64_t payLoad=npos, bool leaveOpen=false)
Begin "DataArray" XML section.
virtual formatter & endPiece()
End "Piece" XML section.
std::ostream & os() noexcept
Access to the underlying output stream.
fileAttr
Some common XML attributes for vtk files.
Definition: foamVtkCore.H:160
const Foam::Enum< fileAttr > fileAttrNames
Strings corresponding to the vtk XML attributes.
formatter & beginFieldData()
Begin "FieldData" XML section.
A class for handling words, derived from Foam::string.
Definition: word.H:63
formatter & beginCellData()
Begin "CellData" XML section.
dataArrayAttr
Some common names for XML DataArray entries.
Definition: foamVtkCore.H:180
const direction noexcept
Definition: scalarImpl.H:255
OBJstream os(runTime.globalPath()/outputName)
formatter & beginVTKFile(const word &contentType, const word &contentVersion, const bool leaveOpen=false)
Add a "VTKFile" XML tag for contentType, followed by a tag for the contentType itself.
const Foam::Enum< fileTag > fileTagNames
Strings corresponding to the vtk XML tags.
const Foam::Enum< dataArrayAttr > dataArrayAttrNames
Strings corresponding to the vtk XML DataArray attributes.
virtual formatter & endDataArray()
End "DataArray" XML section.
fileTag
Some common XML tags for vtk files.
Definition: foamVtkCore.H:122
virtual formatter & endVTKFile()
End "VTKFile" XML section.
void writeAttr(const word &k, const Type &v)
Write XML key/value attribute pair (implementation).
virtual formatter & endCellData()
End "CellData" XML section.
label n
formatter & openTag(const word &tagName, Args &&... args)
Start an XML tag, optionally with attributes.
void xmlCommentLoop(const std::string &text, Args &&... args)
Loop/output XML comments.
formatter & xmlHeader()
Write XML header.
formatter & endBlock()
End "Block" XML section.
Foam::argList args(argc, argv)
formatter & tag(const word &t, Args &&... args)
Write XML tag without any attributes. Combines openTag/closeTag.
formatter & xmlAttr()
No-op write XML attribute (for templating code).