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-2020 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 FlatOutput_H
36#define 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> {};
83
84makeDecorator(BareComma, '\0','\0', ',');
85makeDecorator(BareSpace, '\0','\0', ' ');
86
89
91makeDecorator(ParenSpace, '(',')', ' '); // Normal default
92
95
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>
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 component
138 explicit OutputAdaptor(const Container& obj)
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 bool started = false;
155
156 // In c++17, can use constexpr if
157
158 if (Delimiters::open)
159 {
160 os << Delimiters::open;
161 }
162 for (const auto& item : values)
163 {
164 if (started)
165 {
166 if (Delimiters::separator)
167 {
168 os << Delimiters::separator;
169 }
170 }
171 else
172 {
173 started = true;
174 }
175 os << item;
176 }
177 if (Delimiters::close)
178 {
179 os << Delimiters::close;
180 }
181
182 return os;
183 }
184
185
186 // Operators
187
188 //- Ostream Operator
189 friend Ostream& operator<<
190 (
191 Ostream& os,
193 )
194 {
195 return adaptor.write(os);
196 }
197};
198
199
200// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
201
202} // End namespace FlatOutput
203} // End namespace Foam
204
205
206// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
207
208namespace Foam
209{
210
211//- Global flatOutput() function with specified output delimiters
212template<class Container, class Delimiters>
213inline FlatOutput::OutputAdaptor<Container, Delimiters>
215(
216 const Container& obj,
217 Delimiters delim
218)
219{
221}
222
223
224//- Global flatOutput() function with default (parenthesis/space) delimiters
225template<class Container>
226inline FlatOutput::OutputAdaptor<Container, FlatOutput::ParenSpace>
228(
229 const Container& obj
230)
231{
233}
234
235
236// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
237
238} // End namespace Foam
239
240
241// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
242
243#endif
244
245// ************************************************************************* //
#define makeDecorator(Name, Open, Close, Sep)
Definition: FlatOutput.H:80
An output adaptor with a write method and an Ostream operator.
Definition: FlatOutput.H:127
Ostream & write(Ostream &os) const
Definition: FlatOutput.H:152
OutputAdaptor(const Container &obj)
Construct from component.
Definition: FlatOutput.H:138
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
OBJstream os(runTime.globalPath()/outputName)
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:215
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
runTime write()
Surround with '\0' and '\0' separate with ','.
Definition: FlatOutput.H:84
Surround with '\0' and '\0' separate with ' '.
Definition: FlatOutput.H:85
Surround with '{' and '}' separate with ','.
Definition: FlatOutput.H:87
Surround with '{' and '}' separate with ' '.
Definition: FlatOutput.H:88
List decorators with open, close and separator characters.
Definition: FlatOutput.H:72
static constexpr char close
Definition: FlatOutput.H:74
static constexpr char separator
Definition: FlatOutput.H:75
static constexpr char open
Definition: FlatOutput.H:73
Surround with '(' and ')' separate with ','.
Definition: FlatOutput.H:90
Surround with '(' and ')' separate with ' '.
Definition: FlatOutput.H:91
Surround with '<' and '>' separate with ','.
Definition: FlatOutput.H:93
Surround with '<' and '>' separate with ' '.
Definition: FlatOutput.H:94
Surround with '[' and ']' separate with ','.
Definition: FlatOutput.H:96
Surround with '[' and ']' separate with ' '.
Definition: FlatOutput.H:97