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 -------------------------------------------------------------------------------
10 License
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 Namespace
27  Foam::FlatOutput
28 
29 Description
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 
42 namespace Foam
43 {
44 namespace FlatOutput
45 {
46 
47 // Forward Declarations
48 template<class Container, class Delimiters> class OutputAdaptor;
49 
50 } // End namespace FlatOutput
51 
52 
53 // Forward Declarations
54 template<class Container, class Delimiters>
55 inline Ostream& operator<<
56 (
57  Ostream& os,
59 );
60 
61 
62 namespace FlatOutput
63 {
64 
65 /*---------------------------------------------------------------------------*\
66  Class Decorators Declaration
67 \*---------------------------------------------------------------------------*/
68 
69 //- List decorators with \c open, \c close and \c separator characters
70 template<char OpenChar, char CloseChar, char SepChar>
71 struct Decorators
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 
84 makeDecorator(BareComma, '\0','\0', ',');
85 makeDecorator(BareSpace, '\0','\0', ' ');
86 
87 makeDecorator(BraceComma, '{','}', ',');
88 makeDecorator(BraceSpace, '{','}', ' ');
89 
90 makeDecorator(ParenComma, '(',')', ',');
91 makeDecorator(ParenSpace, '(',')', ' '); // Normal default
92 
93 makeDecorator(PointyComma, '<','>', ',');
94 makeDecorator(PointySpace, '<','>', ' ');
95 
96 makeDecorator(SquareComma,'[',']', ',');
97 makeDecorator(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 //
125 // \noop
126 template<class Container, class Delimiters>
127 class OutputAdaptor
128 {
129  // Private Data
130 
131  //- The container of values for output
132  const Container& values;
133 
134 public:
135 
136  // Constructors
137 
138  //- Construct from component
139  explicit OutputAdaptor(const Container& obj)
140  :
141  values(obj)
142  {}
143 
144 
145  // Member Functions
146 
147  //- Write list using \c open, \c close and \c separator characters
148  //- specified by Delimiters template, which generally results in
149  //- a single line without line breaks.
150  //
151  // \note Suppresses nul char output.
152  // No special handling for newline separators.
153  inline Ostream& write(Ostream& os) const
154  {
155  bool started = false;
156 
157  // In c++17, can use constexpr if
158 
159  if (Delimiters::open)
160  {
161  os << Delimiters::open;
162  }
163  for (const auto& item : values)
164  {
165  if (started)
166  {
167  if (Delimiters::separator)
168  {
169  os << Delimiters::separator;
170  }
171  }
172  else
173  {
174  started = true;
175  }
176  os << item;
177  }
178  if (Delimiters::close)
179  {
180  os << Delimiters::close;
181  }
182 
183  return os;
184  }
185 
186 
187  // Operators
188 
189  //- Ostream Operator
190  friend Ostream& operator<<
191  (
192  Ostream& os,
193  const OutputAdaptor<Container, Delimiters>& adaptor
194  )
195  {
196  return adaptor.write(os);
197  }
198 };
199 
200 
201 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
202 
203 } // End namespace FlatOutput
204 } // End namespace Foam
205 
206 
207 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
208 
209 namespace Foam
210 {
211 
212 //- Global flatOutput() function with specified output delimiters
213 template<class Container, class Delimiters>
214 inline FlatOutput::OutputAdaptor<Container, Delimiters>
216 (
217  const Container& obj,
218  Delimiters delim
219 )
220 {
222 }
223 
224 
225 //- Global flatOutput() function with default (parenthesis/space) delimiters
226 template<class Container>
227 inline FlatOutput::OutputAdaptor<Container, FlatOutput::ParenSpace>
229 (
230  const Container& obj
231 )
232 {
234 }
235 
236 
237 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
238 
239 } // End namespace Foam
240 
241 
242 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
243 
244 #endif
245 
246 // ************************************************************************* //
Foam::FlatOutput::ParenComma
Surround with '(' and ')' separate with ','.
Definition: FlatOutput.H:90
makeDecorator
#define makeDecorator(Name, Open, Close, Sep)
Definition: FlatOutput.H:80
Foam::FlatOutput::Decorators::open
static constexpr char open
Definition: FlatOutput.H:73
Foam::FlatOutput::Decorators::close
static constexpr char close
Definition: FlatOutput.H:74
Foam::FlatOutput::SquareComma
Surround with '[' and ']' separate with ','.
Definition: FlatOutput.H:96
Foam::FlatOutput::OutputAdaptor::write
Ostream & write(Ostream &os) const
Definition: FlatOutput.H:153
Foam::FlatOutput::OutputAdaptor::OutputAdaptor
OutputAdaptor(const Container &obj)
Construct from component.
Definition: FlatOutput.H:139
Foam::FlatOutput::BraceSpace
Surround with '{' and '}' separate with ' '.
Definition: FlatOutput.H:88
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::FlatOutput::PointyComma
Surround with '<' and '>' separate with ','.
Definition: FlatOutput.H:93
Foam::FlatOutput::Decorators::separator
static constexpr char separator
Definition: FlatOutput.H:75
Foam::FlatOutput::SquareSpace
Surround with '[' and ']' separate with ' '.
Definition: FlatOutput.H:97
Foam::FlatOutput::PointySpace
Surround with '<' and '>' separate with ' '.
Definition: FlatOutput.H:94
os
OBJstream os(runTime.globalPath()/outputName)
Foam::Ostream::write
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::flatOutput
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:216
Ostream.H
Foam::FlatOutput::Decorators
List decorators with open, close and separator characters.
Definition: FlatOutput.H:71
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::FlatOutput::BareComma
Surround with '\0' and '\0' separate with ','.
Definition: FlatOutput.H:84
Foam::FlatOutput::BareSpace
Surround with '\0' and '\0' separate with ' '.
Definition: FlatOutput.H:85
Foam::FlatOutput::ParenSpace
Surround with '(' and ')' separate with ' '.
Definition: FlatOutput.H:91
Foam::FlatOutput::BraceComma
Surround with '{' and '}' separate with ','.
Definition: FlatOutput.H:87
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::PtrListOps::names
List< word > names(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
Foam::FlatOutput::OutputAdaptor
An output adaptor with a write method and an Ostream operator.
Definition: FlatOutput.H:48