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