Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
10License
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
36template<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
58inline Foam::vtk::formatter::formatter(std::ostream& os)
59:
60 os_(os),
61 xmlTags_(),
62 inTag_(false),
64{}
65
66
67// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
69inline std::ostream& Foam::vtk::formatter::os() noexcept
70{
71 return os_;
72}
73
76{
77 indent(2*xmlTags_.size());
78}
79
80
81inline 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
101template<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;
128
129 return *this;
130}
131
132
133template<class... Args>
135(
136 const word& tagName,
137 Args&&... args
138)
139{
140 if (openTagImpl(tagName))
141 {
142 xmlAttr(std::forward<Args>(args)...);
144
145 return *this;
146}
147
148
149template<class... Args>
151(
152 vtk::fileTag t,
153 Args&&... args
155{
156 return openTag(vtk::fileTagNames[t], std::forward<Args>(args)...);
157}
158
159
160template<class... Args>
162(
163 const word& t,
164 Args&&... args
165)
166{
167 openTagImpl(t);
168 xmlAttr(std::forward<Args>(args)...);
170
171 return *this;
172}
173
174
175template<class... Args>
177(
178 vtk::fileTag t,
179 Args&&... args
180)
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
219template<Foam::vtk::fileTag ContentType>
221{
222 return beginVTKFile
223 (
224 vtk::fileTagNames[ContentType],
226 leaveOpen
227 );
228}
229
230
231template<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
252}
253
258}
259
260
264}
265
266
267// End tags
272}
273
278}
279
284}
285
290}
291
296}
297
302}
303
308}
309
310
312{
314}
315
316
317// Attributes
318
319template<class Type>
320inline void Foam::vtk::formatter::writeAttr(const word& k, const Type& v)
321{
322 os_ << ' ' << k << '=' << quote_ << v << quote_;
323}
324
325
326template<class... Args>
328(
329 const word& k,
330 const std::string& v,
331 Args&&... args
332)
333{
334 if (!canWriteAttr(k)) return *this;
336 writeAttr(k, v.c_str());
337 return xmlAttr(std::forward<Args>(args)...);
338}
339
340
341template<class... Args>
343(
344 const word& k,
345 const int32_t v,
346 Args&&... args
347)
348{
349 if (!canWriteAttr(k)) return *this;
351 writeAttr(k, v);
352 return xmlAttr(std::forward<Args>(args)...);
353}
354
355
356template<class... Args>
358(
359 const word& k,
360 const int64_t v,
361 Args&&... args
362)
363{
364 if (!canWriteAttr(k)) return *this;
366 writeAttr(k, v);
367 return xmlAttr(std::forward<Args>(args)...);
368}
369
370
371template<class... Args>
373(
374 const word& k,
375 const uint64_t v,
376 Args&&... args
377)
378{
379 if (!canWriteAttr(k)) return *this;
381 writeAttr(k, v);
382 return xmlAttr(std::forward<Args>(args)...);
383}
384
385
386template<class... Args>
388(
389 const word& k,
390 const scalar v,
391 Args&&... args
392)
393{
394 if (!canWriteAttr(k)) return *this;
396 writeAttr(k, v);
397 return xmlAttr(std::forward<Args>(args)...);
398}
399
400
401template<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;
411 writeAttr(vtk::fileAttrNames[k], v.c_str());
412 return xmlAttr(std::forward<Args>(args)...);
413}
414
415
416template<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;
427 return xmlAttr(std::forward<Args>(args)...);
428}
429
430
431template<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;
442 return xmlAttr(std::forward<Args>(args)...);
443}
444
445
446template<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;
457 return xmlAttr(std::forward<Args>(args)...);
458}
459
460
461template<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// ************************************************************************* //
label k
label n
Abstract class for a VTK output stream formatter.
virtual formatter & endVTKFile()
End "VTKFile" XML section.
virtual formatter & endPointData()
End "PointData" XML section.
formatter & endBlock()
End "Block" XML section.
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.
char quote_
Quoting character for XML attributes.
formatter & xmlAttr()
No-op write XML attribute (for templating code).
bool inTag_
Tag open/closed/ended state.
DynamicList< word > xmlTags_
LIFO stack of current XML tags.
formatter & beginPointData()
Begin "PointData" XML section.
formatter & tag(const word &t, Args &&... args)
Write XML tag without any attributes. Combines openTag/closeTag.
formatter & beginFieldData()
Begin "FieldData" XML section.
@ SINGLE_QUOTE
Single-quote XML attributes.
formatter & beginDataArray(const word &dataName, uint64_t payLoad=npos, bool leaveOpen=false)
Begin "DataArray" XML section.
virtual formatter & endCellData()
End "CellData" XML section.
formatter(std::ostream &os)
Construct and attach to an output stream.
formatter & endTag(const word &tagName=word::null)
An end XML tag, optional with sanity check.
void xmlCommentLoop(const std::string &text, Args &&... args)
Loop/output XML comments.
bool canWriteToplevel(const char *what) const
Can write tag-like top-level content (eg, comment, ...) when not already inside a tag....
formatter & closeTag(const bool isEmpty=false)
Finish an XML tag, optional as an empty container.
bool canWriteAttr(const word &k) const
Can write XML key/value attribute pair when inside a tag. Emit warning and return false if this condi...
virtual formatter & endFieldData()
End "FieldData" XML section.
std::ostream & os() noexcept
Access to the underlying output stream.
virtual formatter & endPiece()
End "Piece" XML section.
formatter & beginCellData()
Begin "CellData" XML section.
formatter & xmlHeader()
Write XML header.
formatter & openTag(const word &tagName, Args &&... args)
Start an XML tag, optionally with attributes.
std::ostream & os_
The output stream for the formatter.
void writeAttr(const word &k, const Type &v)
Write XML key/value attribute pair (implementation).
bool openTagImpl(const word &tagName)
Open XML tag (implementation), checking if not already inside another tag. Emit warning and return fa...
virtual formatter & endDataArray()
End "DataArray" XML section.
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.
A class for handling words, derived from Foam::string.
Definition word.H:66
OBJstream os(runTime.globalPath()/outputName)
fileAttr
Some common XML attributes for vtk files.
const Foam::Enum< dataArrayAttr > dataArrayAttrNames
Strings corresponding to the vtk XML DataArray attributes.
dataArrayAttr
Some common names for XML DataArray entries.
fileTag
Some common XML tags for vtk files.
@ FIELD_DATA
"FieldData"
@ CELL_DATA
"CellData"
@ POINT_DATA
"PointData"
@ DATA_ARRAY
"DataArray"
@ VTK_FILE
"VTKFile"
const Foam::Enum< fileTag > fileContentVersions
Version string for some vtk XML file content types.
const Foam::Enum< fileAttr > fileAttrNames
Strings corresponding to the vtk XML attributes.
const Foam::Enum< fileTag > fileTagNames
Strings corresponding to the vtk XML tags.
Ostream & indent(Ostream &os)
Indent stream.
Definition Ostream.H:481
const direction noexcept
Definition scalarImpl.H:265
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
Foam::argList args(argc, argv)