Loading...
Searching...
No Matches
FlatOutput.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
26Namespace
27 Foam::FlatOutput
28
29Description
30 Various output adaptors, principally to output a list of items
31 on a single line.
32
33\*---------------------------------------------------------------------------*/
34
35#ifndef Foam_FlatOutput_H
36#define Foam_FlatOutput_H
37
38#include "Ostream.H"
39
40// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
41
42namespace Foam
43{
44namespace FlatOutput
45{
46
47// Forward Declarations
48template<class Container, class Delimiters> class OutputAdaptor;
49
50} // End namespace FlatOutput
51
52
53// Forward Declarations
54template<class Container, class Delimiters>
55inline Ostream& operator<<
56(
57 Ostream& os,
59);
60
61
62namespace FlatOutput
63{
64
65/*---------------------------------------------------------------------------*\
66 Class Decorators Declaration
67\*---------------------------------------------------------------------------*/
68
69//- List decorators with \c open, \c close and \c separator characters
70template<char OpenChar, char CloseChar, char SepChar>
72{
73 static constexpr char open = OpenChar;
74 static constexpr char close = CloseChar;
75 static constexpr char separator = SepChar;
76};
77
78
79#undef makeDecorator
80#define makeDecorator(Name, Open, Close, Sep) \
81 \
82 struct Name : public Decorators<Open, Close, Sep> {};
84makeDecorator(BareComma, '\0','\0', ',');
85makeDecorator(BareSpace, '\0','\0', ' ');
88makeDecorator(BraceSpace, '{','}', ' ');
91makeDecorator(ParenSpace, '(',')', ' '); // Normal default
94makeDecorator(PointySpace, '<','>', ' ');
95
96makeDecorator(SquareComma,'[',']', ',');
97makeDecorator(SquareSpace,'[',']', ' ');
98
99#undef makeDecorator
100
101
102/*---------------------------------------------------------------------------*\
103 Class OutputAdaptor Declaration
104\*---------------------------------------------------------------------------*/
105
106//- An output adaptor with a write method and an Ostream operator.
107//
108// Generate single line (flat) output using the characters specified by
109// the templated Delimiters.
110// Normally called with the global flatOutput() function.
111// For example,
112// \code
113//
114// /* With default parenthesis/space delimiters */
115// Info<< "Names: " << flatOutput(names) << nl;
116//
117// /* Other delimiters */
118// Info<< flatOutput(names, FlatOutput::SquareComma{}) << nl;
119//
120// /* User-specified delimiters */
121// Info<< flatOutput(names, FlatOutput::Decorators<'[',')',':'>{}) << nl;
122//
123// \endcode
124//
125template<class Container, class Delimiters>
126class OutputAdaptor
127{
128 // Private Data
129
130 //- The container of values for output
131 const Container& values;
132
133public:
134
135 // Constructors
136
137 //- Construct from const reference
138 explicit OutputAdaptor(const Container& obj) noexcept
139 :
140 values(obj)
141 {}
142
143
144 // Member Functions
145
146 //- Write list using \c open, \c close and \c separator characters
147 //- specified by Delimiters template, which generally results in
148 //- a single line without line breaks.
149 //
150 // \note Suppresses nul char output.
151 // No special handling for newline separators.
152 inline Ostream& write(Ostream& os) const
153 {
154 [[maybe_unused]] bool started = false;
155
156 if constexpr (Delimiters::open) // Not nul char
157 {
158 os << Delimiters::open;
159 }
160 for (const auto& item : values)
161 {
162 if constexpr (Delimiters::separator) // Not nul char
163 {
164 if (started) os << Delimiters::separator;
165 started = true;
166 }
167 os << item;
168 }
169 if constexpr (Delimiters::close) // Not nul char
170 {
171 os << Delimiters::close;
172 }
173
174 return os;
175 }
176
177
178 // Operators
179
180 //- Ostream Operator
181 friend Ostream& operator<<
182 (
183 Ostream& os,
185 )
186 {
187 return adaptor.write(os);
188 }
190
191
192// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
193
194} // End namespace FlatOutput
195} // End namespace Foam
196
197
198// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
199
200namespace Foam
201{
202
203//- Global flatOutput() function with specified output delimiters
204template<class Container, class Delimiters>
205inline FlatOutput::OutputAdaptor<Container, Delimiters>
207(
208 const Container& obj,
209 Delimiters delim
210)
211{
212 return FlatOutput::OutputAdaptor<Container, Delimiters>(obj);
213}
214
215
216//- Global flatOutput() function with default (parenthesis/space) delimiters
217template<class Container>
220(
221 const Container& obj
222)
223{
225}
226
227
228// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
229
230} // End namespace Foam
232
233// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
234
235#endif
236
237// ************************************************************************* //
#define makeDecorator(Name, Open, Close, Sep)
Definition FlatOutput.H:76
An output adaptor with a write method and an Ostream operator.
Definition FlatOutput.H:126
Ostream & write(Ostream &os) const
Write list using open, close and separator characters specified by Delimiters template,...
Definition FlatOutput.H:158
OutputAdaptor(const Container &obj) noexcept
Construct from const reference.
Definition FlatOutput.H:141
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
OBJstream os(runTime.globalPath()/outputName)
auto & names
Various output adaptors, principally to output a list of items on a single line.
Namespace for OpenFOAM.
messageStream Info
Information stream (stdout output on master, null elsewhere).
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition FlatOutput.H:217
const direction noexcept
Definition scalarImpl.H:265
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
runTime write()
Surround with '\0' and '\0' separate with ','.
Definition FlatOutput.H:80
Surround with '\0' and '\0' separate with ' '.
Definition FlatOutput.H:81
Surround with '{' and '}' separate with ','.
Definition FlatOutput.H:83
Surround with '{' and '}' separate with ' '.
Definition FlatOutput.H:84
List decorators with open, close and separator characters.
Definition FlatOutput.H:68
static constexpr char close
Definition FlatOutput.H:70
static constexpr char separator
Definition FlatOutput.H:71
static constexpr char open
Definition FlatOutput.H:69
Surround with '(' and ')' separate with ','.
Definition FlatOutput.H:86
Surround with '(' and ')' separate with ' '.
Definition FlatOutput.H:87
Surround with '<' and '>' separate with ','.
Definition FlatOutput.H:89
Surround with '<' and '>' separate with ' '.
Definition FlatOutput.H:90
Surround with '[' and ']' separate with ','.
Definition FlatOutput.H:92
Surround with '[' and ']' separate with ' '.
Definition FlatOutput.H:93