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-------------------------------------------------------------------------------
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
37{}
38
39
40template<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
60:
61 os_(os),
62 xmlTags_(),
63 inTag_(false),
64 quote_(SINGLE_QUOTE)
65{}
66
67
68// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
69
70inline std::ostream& Foam::vtk::formatter::os() noexcept
71{
72 return os_;
73}
74
75
77{
78 indent(2*xmlTags_.size());
79}
80
81
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
102template<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
134template<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
150template<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
161template<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
176template<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
220template<Foam::vtk::fileTag ContentType>
222{
223 return beginVTKFile
224 (
225 vtk::fileTagNames[ContentType],
226 vtk::fileContentVersions[ContentType],
227 leaveOpen
228 );
229}
230
231
232template<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
320template<class Type>
321inline void Foam::vtk::formatter::writeAttr(const word& k, const Type& v)
322{
323 os_ << ' ' << k << '=' << quote_ << v << quote_;
324}
325
326
327template<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
342template<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
357template<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
372template<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
387template<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
402template<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
417template<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
432template<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
447template<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
462template<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// ************************************************************************* //
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.
formatter & xmlAttr()
No-op write XML attribute (for templating code).
formatter & beginPointData()
Begin "PointData" XML section.
formatter & beginFieldData()
Begin "FieldData" XML section.
formatter & beginDataArray(const word &dataName, uint64_t payLoad=npos, bool leaveOpen=false)
Begin "DataArray" XML section.
virtual formatter & endCellData()
End "CellData" XML section.
formatter & beginVTKFile(const word &contentType, const word &contentVersion, const bool leaveOpen=false)
void xmlCommentLoop()
No-op XML comment loop (for templating code).
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 & endTag(const word &tagName=word::null)
An end XML tag, optional with sanity check.
formatter & xmlHeader()
Write XML header.
formatter & openTag(const word &tagName, Args &&... args)
Start an XML tag, optionally with attributes.
void writeAttr(const word &k, const Type &v)
Write XML key/value attribute pair (implementation).
virtual formatter & endDataArray()
End "DataArray" XML section.
A class for handling words, derived from Foam::string.
Definition: word.H:68
OBJstream os(runTime.globalPath()/outputName)
const Foam::Enum< dataArrayAttr > dataArrayAttrNames
Strings corresponding to the vtk XML DataArray attributes.
const Foam::Enum< fileAttr > fileAttrNames
Strings corresponding to the vtk XML attributes.
fileAttr
Some common XML attributes for vtk files.
Definition: foamVtkCore.H:144
dataArrayAttr
Some common names for XML DataArray entries.
Definition: foamVtkCore.H:160
fileTag
Some common XML tags for vtk files.
Definition: foamVtkCore.H:114
@ FIELD_DATA
"FieldData"
@ CELL_DATA
"CellData"
@ POINT_DATA
"PointData"
@ DATA_ARRAY
"DataArray"
@ VTK_FILE
"VTKFile"
const Foam::Enum< fileTag > fileTagNames
Strings corresponding to the vtk XML tags.
const Foam::Enum< fileTag > fileContentVersions
Version string for some vtk XML file content types.
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:342
const direction noexcept
Definition: Scalar.H:223
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
Foam::argList args(argc, argv)