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 template<class Container, class Delimiters>
126 class OutputAdaptor
127 {
128  // Private Data
129 
130  //- The container of values for output
131  const Container& values;
132 
133 public:
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,
192  const OutputAdaptor<Container, Delimiters>& adaptor
193  )
194  {
195  return adaptor.write(os);
196  }
197 };
199 
200 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
201 
202 } // End namespace FlatOutput
203 } // End namespace Foam
204 
205 
206 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
207 
208 namespace Foam
209 {
210 
211 //- Global flatOutput() function with specified output delimiters
212 template<class Container, class Delimiters>
213 inline FlatOutput::OutputAdaptor<Container, Delimiters>
215 (
216  const Container& obj,
217  Delimiters delim
218 )
219 {
220  return FlatOutput::OutputAdaptor<Container, Delimiters>(obj);
221 }
222 
223 
224 //- Global flatOutput() function with default (parenthesis/space) delimiters
225 template<class Container>
228 (
229  const Container& obj
230 )
231 {
233 }
234 
235 
236 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
237 
238 } // End namespace Foam
239 
241 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
242 
243 #endif
244 
245 // ************************************************************************* //
static constexpr char open
Definition: FlatOutput.H:69
Surround with &#39;{&#39; and &#39;}&#39; separate with &#39;,&#39;.
Definition: FlatOutput.H:83
Surround with &#39;[&#39; and &#39;]&#39; separate with &#39; &#39;.
Definition: FlatOutput.H:93
Surround with &#39;\0&#39; and &#39;\0&#39; separate with &#39; &#39;.
Definition: FlatOutput.H:81
Surround with &#39;\0&#39; and &#39;\0&#39; separate with &#39;,&#39;.
Definition: FlatOutput.H:80
static constexpr char close
Definition: FlatOutput.H:70
Surround with &#39;{&#39; and &#39;}&#39; separate with &#39; &#39;.
Definition: FlatOutput.H:84
An output adaptor with a write method and an Ostream operator.
Definition: FlatOutput.H:42
static constexpr char separator
Definition: FlatOutput.H:71
OutputAdaptor(const Container &obj)
Construct from component.
Definition: FlatOutput.H:141
#define makeDecorator(Name, Open, Close, Sep)
Definition: FlatOutput.H:76
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
Surround with &#39;<&#39; and &#39;>&#39; separate with &#39;,&#39;.
Definition: FlatOutput.H:89
OBJstream os(runTime.globalPath()/outputName)
Ostream & write(Ostream &os) const
Write list using open, close and separator characters specified by Delimiters template, which generally results in a single line without line breaks.
Definition: FlatOutput.H:158
List decorators with open, close and separator characters.
Definition: FlatOutput.H:67
Surround with &#39;(&#39; and &#39;)&#39; separate with &#39;,&#39;.
Definition: FlatOutput.H:86
Surround with &#39;[&#39; and &#39;]&#39; separate with &#39;,&#39;.
Definition: FlatOutput.H:92
Surround with &#39;<&#39; and &#39;>&#39; separate with &#39; &#39;.
Definition: FlatOutput.H:90
Surround with &#39;(&#39; and &#39;)&#39; separate with &#39; &#39;.
Definition: FlatOutput.H:87
Namespace for OpenFOAM.
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:225