Loading...
Searching...
No Matches
foamVtkFormatter.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) 2016-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
26Class
27 Foam::vtk::formatter
28
29Description
30 Abstract class for a VTK output stream formatter.
31
32 Includes simple support for writing XML elements.
33 By default uses single-quoting for XML attributes.
34
35SourceFiles
36 foamVtkFormatter.cxx
37 foamVtkFormatter.txx
38 foamVtkFormatterI.H
39
40\*---------------------------------------------------------------------------*/
41
42#ifndef Foam_vtk_formatter_H
43#define Foam_vtk_formatter_H
44
45#include "int.H"
46#include "label.H"
47#include "uint64.H"
48#include "direction.H"
49#include "word.H"
50#include "List.H"
51#include "DynamicList.H"
52#include "foamVtkCore.H"
53#include "foamVtkPTraits.H"
54
55#include <iostream>
56
57// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
58
59namespace Foam
60{
61namespace vtk
62{
63
64// Forward Declarations
65class outputOptions;
67/*---------------------------------------------------------------------------*\
68 Class vtk::formatter Declaration
69\*---------------------------------------------------------------------------*/
70
71class formatter
72{
73public:
74
75 //- Quoting for XML attributes
76 enum quoteChar : char
77 {
78 DOUBLE_QUOTE = '\"',
79 SINGLE_QUOTE = '\'',
80 };
81
82
83protected:
84
85 // Private Data
86
87 //- The output stream for the formatter
88 std::ostream& os_;
89
90 //- LIFO stack of current XML tags
93 //- Tag open/closed/ended state
94 mutable bool inTag_;
95
96 //- Quoting character for XML attributes
97 char quote_;
98
99
100protected:
101
102 // Protected Member Functions
103
104 //- Can write XML key/value attribute pair when inside a tag.
105 //- Emit warning and return false if this condition is not met.
106 bool canWriteAttr(const word& k) const;
107
108 //- Can write tag-like top-level content (eg, comment, ...)
109 //- when not already inside a tag.
110 //- Emit warning and return false if this condition is not met.
111 bool canWriteToplevel(const char* what) const;
112
113 //- Write XML key/value attribute pair (implementation).
114 template<class Type>
115 inline void writeAttr(const word& k, const Type& v);
116
117 //- No-op write XML attribute (for templating code).
118 // \return formatter for chaining
119 inline formatter& xmlAttr();
121 //- Loop/output XML comments
122 template<class... Args>
123 inline void xmlCommentLoop(const std::string& text, Args&&... args);
124
125 //- Open XML tag (implementation), checking if not already inside
126 //- another tag.
127 //- Emit warning and return false if this condition is not met.
128 bool openTagImpl(const word& tagName);
129
130
131 // Constructors
132
133 //- Construct and attach to an output stream
134 inline explicit formatter(std::ostream& os);
135
136
137public:
138
139 // Public typedefs
140
141 //- The header data is vtk UInt64
142 typedef uint64_t headerType;
143
144 //- Out of range position or size.
145 static constexpr uint64_t npos = uint64_t(-1);
147
148 //- Destructor
149 virtual ~formatter() = default;
150
151
152 // Member Functions
153
154 // Access
155
156 //- Access to the underlying output stream
157 inline std::ostream& os() noexcept;
158
159 //- The format-type / output options.
160 virtual const vtk::outputOptions& opts() const = 0;
161
162 //- Name for the XML output type or the legacy output type.
163 virtual const char* name() const = 0;
165 //- Name for the XML append encoding
166 virtual const char* encoding() const = 0;
167
168 //- Change quoting char for XML attributes (default: SINGLE_QUOTE)
170
171 //- Increase the append data offset by numbytes and sizeof(uint64_t).
172 // The additional (uint64_t) size information is consistent with
173 // writeSize()
174 //
175 // \return The previous data offset or formatter::npos for formats
176 // that do not support appending data.
177 virtual uint64_t offset(const uint64_t numbytes);
178
179 //- The encoded length for binary output is pass-through.
180 virtual std::size_t encodedLength(std::size_t n) const;
181
182 //- Write leading size for binary output
183 // \return True if used by the formatter.
184 virtual bool writeSize(const uint64_t numbytes) = 0;
185
186 virtual void write(const uint8_t val) = 0;
187 virtual void write(const label val) = 0;
188 virtual void write(const float val) = 0;
189 virtual void write(const double val) = 0;
191 //- Flush encoding, write newline etc.
192 virtual void flush() = 0;
193
194
195 // General Output
196
197 //- Add indenting according to the current XML tag depth
198 // Two spaces per depth.
199 inline void indent();
201 //- Add indenting of n spaces.
202 inline void indent(label n);
203
204 //- Write XML header
205 // \return formatter for chaining
206 inline formatter& xmlHeader();
207
208 //- Write XML comment (at the current indentation level)
209 // \return formatter for chaining
210 template<class... Args>
211 inline formatter& xmlComment(const std::string& text, Args&&... args);
212
213
214 //- Start an XML tag, optionally with attributes
215 // \return formatter for chaining
216 template<class... Args>
217 inline formatter& openTag(const word& tagName, Args&&... args);
218
219 //- Start an XML tag, optionally with attributes
220 // \return formatter for chaining
221 template<class... Args>
222 inline formatter& openTag(vtk::fileTag t, Args&&... args);
223
224 //- Finish an XML tag, optional as an empty container.
225 // Always adds a trailing newline.
226 // \return formatter for chaining
227 formatter& closeTag(const bool isEmpty = false);
229 //- An end XML tag, optional with sanity check
230 // Always adds a trailing newline.
231 // \return formatter for chaining
232 formatter& endTag(const word& tagName = word::null);
234 //- An end XML tag with sanity check
235 // Always adds a trailing newline.
236 // \return formatter for chaining
237 inline virtual formatter& endTag(vtk::fileTag t);
239 //- Write XML tag without any attributes. Combines openTag/closeTag.
240 // \return formatter for chaining
241 template<class... Args>
242 inline formatter& tag(const word& t, Args&&... args);
243
244 //- Write XML tag without any attributes. Combines openTag/closeTag.
245 // \return formatter for chaining
246 template<class... Args>
247 inline formatter& tag(vtk::fileTag t, Args&&... args);
248
249
250 //- Add a "VTKFile" XML tag for contentType, followed by a tag for
251 //- the contentType itself.
252 // \param leaveOpen Leave tag open for additional attributes.
253 // \return formatter for chaining
255 (
256 const word& contentType,
257 const word& contentVersion,
258 const bool leaveOpen = false
259 );
260
261 //- Add a "VTKFile" XML tag for contentType, followed by a tag for
262 //- the contentType itself.
263 // \param leaveOpen Leave tag open for additional attributes.
264 // \return formatter for chaining
265 inline formatter& beginVTKFile
266 (
267 vtk::fileTag contentType,
268 const word& contentVersion,
269 const bool leaveOpen = false
270 );
271
272 //- Add a "VTKFile" XML tag for contentType, followed by a tag for
273 //- the contentType itself.
274 // \param leaveOpen Leave tag open for additional attributes.
275 // \return formatter for chaining
276 inline formatter& beginVTKFile
278 vtk::fileTag contentType,
279 const bool leaveOpen = false
280 );
281
282 //- Add a "VTKFile" XML tag for contentType, followed by a tag for
283 //- the contentType itself.
284 // \param leaveOpen Leave tag open for additional attributes.
285 // \return formatter for chaining
286 template<vtk::fileTag ContentType>
287 inline formatter& beginVTKFile(bool leaveOpen = false);
288
289 //- Add a "AppendedData" XML tag with the current encoding and output
290 //- the requisite '_' prefix.
291 // \return formatter for chaining
294 //- Begin "Block" XML section.
295 // \param index The index of the block
296 // \param name The name of the block (ignored if empty)
297 // \return formatter for chaining
298 formatter& beginBlock(label index, std::string name = "");
299
300 //- End "Block" XML section.
301 // \return formatter for chaining
302 inline formatter& endBlock();
303
304 //- Begin "Piece" XML section.
305 // \param index The index of the piece
306 // \param name The name of the piece (ignored if empty)
307 // \return formatter for chaining
308 formatter& beginPiece(label index, std::string name = "");
309
310 //- End "Piece" XML section.
311 // \return formatter for chaining
312 inline virtual formatter& endPiece();
313
314 //- Insert a single "DataSet" XML entry tag.
315 // \param index The index of the DataSet
316 // \param file The file name for the data (ignored if empty)
317 // \param autoName The name for the data extracted from the file name
318 // (without extension)
319 // \return formatter for chaining
321 (
322 label index,
323 std::string file = "",
324 bool autoName = true
325 );
326
327 //- Insert a single "DataSet" XML entry tag.
328 // \param index The index of the DataSet
329 // \param file The file name for the data (ignored if empty)
330 // \param name The name for the dataset
331 // \return formatter for chaining
333 (
334 label index,
335 std::string file,
336 std::string name
337 );
338
339 //- Begin "DataArray" XML section.
340 //
341 // \param dataName The name of the DataArray
342 // \param payLoad Additional payLoad information to increment
343 // the offset for an append formatter and add the "offset"
344 // attribute accordingly.
345 // \param leaveOpen Leave tag open for additional attributes.
346 //
347 // \return formatter for chaining
348 template<class Type, direction nComp=1, int nTuple=0>
350 (
351 const word& dataName,
352 uint64_t payLoad = npos,
353 bool leaveOpen = false
354 );
355
356 //- Begin "DataArray" XML section.
357 //
358 // \param dataName The name of the DataArray as an enumeration
359 // \param payLoad Additional payLoad information to increment
360 // the offset for an append formatter and add the "offset"
361 // attribute accordingly.
362 // \param leaveOpen Leave tag open for additional attributes.
363 //
364 // \return formatter for chaining
365 template<class Type, direction nComp=1, int nTuple=0>
367 (
368 const vtk::dataArrayAttr& dataName,
369 uint64_t payLoad = npos,
370 bool leaveOpen = false
371 );
372
373 //- End "DataArray" XML section
374 // \return formatter for chaining
375 inline virtual formatter& endDataArray();
376
377 //- Insert a single "PDataArray" XML entry tag.
378 // For some entries, the name is optional.
379 // \return formatter for chaining
380 template<class Type, direction nComp=1, int nTuple=0>
381 formatter& PDataArray(const word& dataName);
382
383
384 //- Begin "FieldData" XML section.
386
387 //- Begin "CellData" XML section.
388 inline formatter& beginCellData();
389
390 //- Begin "PointData" XML section.
391 inline formatter& beginPointData();
392
393 //- End "FieldData" XML section.
394 inline virtual formatter& endFieldData();
395
396 //- End "CellData" XML section.
397 inline virtual formatter& endCellData();
398
399 //- End "PointData" XML section.
400 inline virtual formatter& endPointData();
401
402
403 //- End "AppendedData" XML section
404 // \return formatter for chaining
406
407 //- End "VTKFile" XML section.
408 // \return formatter for chaining
409 inline virtual formatter& endVTKFile();
411
412 //- Emit "TimeValue" for FieldData (name as per Catalyst output)
413 formatter& writeTimeValue(scalar timeValue);
414
415
416 // XML Attributes
417
418 //- Pair-wise write of XML key/value attributes
419 // \return formatter for chaining
420 template<class... Args>
421 inline formatter& xmlAttr
422 (
423 const word& k,
424 const std::string& v,
425 Args&&... args
426 );
427
428 //- Pair-wise write of XML key/value attributes
429 // \return formatter for chaining
430 template<class... Args>
431 inline formatter& xmlAttr
432 (
433 const word& k,
434 const int32_t v,
435 Args&&... args
436 );
437
438 //- Pair-wise write of XML key/value attributes
439 // \return formatter for chaining
440 template<class... Args>
441 inline formatter& xmlAttr
442 (
443 const word& k,
444 const int64_t v,
445 Args&&... args
446 );
447
448 //- Pair-wise write of XML key/value attributes
449 // \return formatter for chaining
450 template<class... Args>
451 inline formatter& xmlAttr
452 (
453 const word& k,
454 const uint64_t v,
455 Args&&... args
456 );
457
458 //- Pair-wise write of XML key/value attributes
459 // \return formatter for chaining
460 template<class... Args>
461 inline formatter& xmlAttr
462 (
463 const word& k,
464 const scalar v,
465 Args&&... args
466 );
467
468 //- Pair-wise write of XML key/value attributes
469 // \return formatter for chaining
470 template<class... Args>
471 inline formatter& xmlAttr
472 (
473 const vtk::fileAttr& k,
474 const std::string& v,
475 Args&&... args
476 );
477
478 //- Pair-wise write of XML key/value attributes
479 // \return formatter for chaining
480 template<class... Args>
481 inline formatter& xmlAttr
482 (
483 const vtk::fileAttr& k,
484 const int32_t v,
485 Args&&... args
486 );
487
488 //- Pair-wise write of XML key/value attributes
489 // \return formatter for chaining
490 template<class... Args>
491 inline formatter& xmlAttr
492 (
493 const vtk::fileAttr& k,
494 const int64_t v,
495 Args&&... args
496 );
497
498 //- Pair-wise write of XML key/value attributes
499 // \return formatter for chaining
500 template<class... Args>
501 inline formatter& xmlAttr
502 (
503 const vtk::fileAttr& k,
504 const uint64_t v,
505 Args&&... args
506 );
507
508 //- Pair-wise write of XML key/value attributes
509 // \return formatter for chaining
510 template<class... Args>
511 inline formatter& xmlAttr
512 (
513 const vtk::fileAttr& k,
514 const scalar v,
515 Args&&... args
516 );
517
518
519 // Housekeeping
520
521 //- Open "DataArray" XML tag and leave open (requires a closeTag).
522 // \deprecated Use beginDataArray instead (SEPT-2018)
523 template<class Type, direction nComp=1, int nTuple=0>
524 formatter& openDataArray(const word& dataName)
525 {
527 (
528 dataName, formatter::npos, true
529 );
530 }
531
532 //- Open "DataArray" XML tag and leave open (requires a closeTag).
533 // \deprecated Use beginDataArray instead (SEPT-2018)
534 template<class Type, direction nComp=1, int nTuple=0>
536 {
538 (
539 dataName, formatter::npos, true
540 );
541 }
542};
544
545// Global Functions
546
547//- Commonly used calculation for header and payload sizes
548template<class Type, direction nComp=1>
549inline uint64_t sizeofData(label count)
550{
551 return (count * nComp * sizeof(Type));
552}
553
554
555// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
557} // End namespace vtk
558} // End namespace Foam
559
560// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
561
562#include "foamVtkFormatterI.H"
563
564#ifdef NoRepository
565 #include "foamVtkFormatter.txx"
566#endif
568// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
569
570#endif
571
572// ************************************************************************* //
label k
label n
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition DynamicList.H:68
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.
uint64_t headerType
The header data is vtk UInt64.
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 & writeTimeValue(scalar timeValue)
Emit "TimeValue" for FieldData (name as per Catalyst output).
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 & DataSet(label index, std::string file="", bool autoName=true)
Insert a single "DataSet" XML entry tag.
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.
quoteChar
Quoting for XML attributes.
@ DOUBLE_QUOTE
Double-quote XML attributes.
@ SINGLE_QUOTE
Single-quote XML attributes.
virtual void flush()=0
Flush encoding, write newline etc.
void quoting(quoteChar quote) noexcept
Change quoting char for XML attributes (default: SINGLE_QUOTE).
formatter & beginDataArray(const word &dataName, uint64_t payLoad=npos, bool leaveOpen=false)
Begin "DataArray" XML section.
formatter & openDataArray(const vtk::dataArrayAttr &dataName)
Open "DataArray" XML tag and leave open (requires a closeTag).
formatter & PDataArray(const word &dataName)
Insert a single "PDataArray" XML entry tag.
virtual std::size_t encodedLength(std::size_t n) const
The encoded length for binary output is pass-through.
virtual uint64_t offset(const uint64_t numbytes)
Increase the append data offset by numbytes and sizeof(uint64_t).
virtual formatter & endCellData()
End "CellData" XML section.
formatter(std::ostream &os)
Construct and attach to an output stream.
formatter & beginPiece(label index, std::string name="")
Begin "Piece" XML section.
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 & openDataArray(const word &dataName)
Open "DataArray" XML tag and leave open (requires a closeTag).
formatter & closeTag(const bool isEmpty=false)
Finish an XML tag, optional as an empty container.
virtual void write(const uint8_t val)=0
virtual const char * encoding() const =0
Name for the XML append encoding.
formatter & beginAppendedData()
Add a "AppendedData" XML tag with the current encoding and output the requisite '_' prefix.
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 const vtk::outputOptions & opts() const =0
The format-type / output options.
virtual formatter & endFieldData()
End "FieldData" XML section.
virtual bool writeSize(const uint64_t numbytes)=0
Write leading size for binary output.
std::ostream & os() noexcept
Access to the underlying output stream.
static constexpr uint64_t npos
Out of range position or size.
virtual formatter & endPiece()
End "Piece" XML section.
virtual ~formatter()=default
Destructor.
formatter & endAppendedData()
End "AppendedData" XML section.
virtual const char * name() const =0
Name for the XML output type or the legacy output type.
formatter & beginCellData()
Begin "CellData" XML section.
formatter & beginBlock(label index, std::string name="")
Begin "Block" 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.
Encapsulated combinations of output format options. This is primarily useful when defining the output...
A class for handling words, derived from Foam::string.
Definition word.H:66
Direction is an 8-bit unsigned integer type used to represent Cartesian directions,...
System signed integer.
Namespace for handling VTK output. Contains classes and functions for writing VTK file content.
uint64_t sizeofData(label count)
Commonly used calculation for header and payload sizes.
fileAttr
Some common XML attributes for vtk files.
dataArrayAttr
Some common names for XML DataArray entries.
fileTag
Some common XML tags for vtk files.
Namespace for OpenFOAM.
uint8_t direction
Definition direction.H:49
const direction noexcept
Definition scalarImpl.H:265
Foam::argList args(argc, argv)
64bit unsigned integer