SoPlex Documentation
Loading...
Searching...
No Matches
spxout.h
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the class library */
4/* SoPlex --- the Sequential object-oriented simPlex. */
5/* */
6/* Copyright (c) 1996-2023 Zuse Institute Berlin (ZIB) */
7/* */
8/* Licensed under the Apache License, Version 2.0 (the "License"); */
9/* you may not use this file except in compliance with the License. */
10/* You may obtain a copy of the License at */
11/* */
12/* http://www.apache.org/licenses/LICENSE-2.0 */
13/* */
14/* Unless required by applicable law or agreed to in writing, software */
15/* distributed under the License is distributed on an "AS IS" BASIS, */
16/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
17/* See the License for the specific language governing permissions and */
18/* limitations under the License. */
19/* */
20/* You should have received a copy of the Apache-2.0 license */
21/* along with SoPlex; see the file LICENSE. If not email to soplex@zib.de. */
22/* */
23/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
24
25/**@file spxout.h
26 * @brief Wrapper for different output streams and verbosity levels.
27 */
28#ifndef _SPXOUT_H_
29#define _SPXOUT_H_
30
31#include <iostream>
32#include <iomanip>
33#include <assert.h>
34
35#include "soplex/spxdefines.h"
36
37// ----------------------------------------------------------------------
38// class SPxOut
39// ----------------------------------------------------------------------
40
41namespace soplex
42{
43
44/**@class SPxOut
45 @ingroup Elementary
46
47 @brief Wrapper for several output streams.
48 A verbosity level is used to decide which stream to use and whether to
49 really print a given message. Regardless of whether the verbosity level
50 is set via a manipulator or via the member function, it is persistent
51 until a new value is set.
52
53 Most ostream member functions are not provided here; use the corresponding
54 stream manipulators (e.g., @c setprecision()) instead. These are passed on
55 to the <em>current</em> ostream, which is chosen according to the verbosity
56 level. In particular, this means that the first element in an output stream
57 should always be the verbosity. For instance, use
58 @code
59 spxout << verb( SPxOut::WARNING ) << std::setw( 15 ) << 42 << std::endl;
60 @endcode
61 or
62 @code
63 spxout.setVerbosity( SPxOut::WARNING );
64 spxout << std::setw( 15 ) << 42 << std::endl;
65 @endcode
66 instead of
67 @code
68 spxout << std::setw( 15 ) << verb( SPxOut::WARNING ) << 42 << std::endl;
69 @endcode
70 in order to make sure that @c std::setw( 15 ) is applied to the warning stream.
71*/
72class SPxOut
73{
74public:
75
76
77 //-----------------------------------
78 /**@name Output control types */
79 ///@{
80 /// Verbosity level
81 typedef enum
82 {
83 // Note: the implementation uses the fact that ERROR == 0
84 // and that the verbosity levels are subsequent numbers.
85 // If you change this, change the implementation as well.
86 ERROR = 0,
88 DEBUG = 2,
89 INFO1 = 3,
90 INFO2 = 4,
91 INFO3 = 5
93
94 /// helper struct for the output operator
96 {
97 /// verbosity level
99 };
100 ///@}
101
102 //-----------------------------------
103 /**@name Construction / destruction */
104 ///@{
105 /// constructor
106 SPxOut();
107 /// destructor
108 virtual ~SPxOut();
109 /// copy constructor
110 SPxOut(const SPxOut&);
111 /// assignment operator
112 SPxOut& operator=(const SPxOut&);
113 ///@}
114
115 //-----------------------------------
116 /**@name Verbosity */
117 ///@{
118 ///
119 virtual void
121 {
122 m_verbosity = v;
123 }
124 ///
125 inline Verbosity
127 const
128 {
129 return m_verbosity;
130 }
131
132 ///@}
133
134 //----------------------------------------
135 /**@name Wrappers for the current stream */
136 ///@{
137 ///
138 inline bool good() const
139 {
140 return getCurrentStream().good();
141 }
142 ///
143 inline bool operator !() const
144 {
145 return ! getCurrentStream();
146 }
147 ///
148 inline std::streamsize precision() const
149 {
150 return getCurrentStream().precision();
151 }
152 ///@}
153
154 //-----------------------------------
155 /**@name Getting / setting streams */
156 ///@{
157 /// Sets the stream for the specified verbosity level.
158 virtual void
160 std::ostream& stream)
161 {
162 m_streams[ verbosity ] = &stream;
163 }
164 /// Returns the stream for the specified verbosity level.
165 inline std::ostream&
167 const
168 {
169 return *(m_streams[ verbosity ]);
170 }
171 /// Returns the stream for the current verbosity.
172 inline std::ostream&
174 const
175 {
176 return getStream(getVerbosity());
177 }
178
179 /// Sets the precision of the stream to 16 and the floatfield to scientifix.
180 static inline void setScientific(std::ostream& stream, int precision = 8)
181 {
182 stream << std::setprecision(precision) << std::scientific;
183 }
184
185 /// Sets the precision of the stream to 8 and the floatfield to fixed.
186 static inline void setFixed(std::ostream& stream, int precision = 8)
187 {
188 stream << std::setprecision(precision) << std::fixed;
189 }
190 ///@}
191
192private:
193
194 //-----------------------------------
195 /**@name Private data */
196 ///@{
197 /// verbosity level
199 /// array of pointers to internal streams, indexed by verbosity level
200 std::ostream** m_streams;
201 ///@}
202};
203
204// ---------------------------------------------------------
205// Manipulators
206// ---------------------------------------------------------
207
208
209//-------------------------------------------
210/**@name Verbosity manipulator
211 Manipulators are implemented in a similar way as done for @c setw(),
212 @c setprecision(), etc. in the standard library file iomanip. For
213 instance, the non-member function \ref verb() "verb(v)" returns a
214 struct struct_Severity which contains only the verbosity level.
215 Calling
216 @code
217 SPxOut spxout;
218 spxout << verb( SPxOut::ERROR ) << "This is an error!" << std::endl;
219 @endcode
220 passes such a struct to the output operator defined below, which
221 extracts the verbosity level from the struct and passes it to the
222 member function SPxOut::setVerbosity().
223*/
224//@{
225/// manipulator to be used in an output statement
228{
230 verbosity.v_ = v;
231 return verbosity;
232}
233
234/// output operator with verbosity level struct
235inline SPxOut&
236operator<< (SPxOut& stream,
238{
239 stream.setVerbosity(verbosity.v_);
240 return stream;
241}
242///@}
243
244//--------------------------------------------------------
245/**@name Output of standard manipulators and other types
246 *
247 * We have to define an output operator for many kinds of numeric
248 * types here because they can all be more or less casted into each
249 * other. When using only a template type, it is not clear what the
250 * compiler makes out of it (according to lint).
251 */
252//@{
253///
254/// Passes instances of type \p Type to the current stream.
256{
257 _spxout.getCurrentStream() << t;
258 return _spxout;
259}
260
261inline SPxOut& operator<< (SPxOut& _spxout, unsigned long t)
262{
263 _spxout.getCurrentStream() << t;
264 return _spxout;
265}
266
268{
269 _spxout.getCurrentStream() << t;
270 return _spxout;
271}
272
274{
275 _spxout.getCurrentStream() << t;
276 return _spxout;
277}
278
279inline SPxOut& operator<< (SPxOut& _spxout, unsigned short t)
280{
281 _spxout.getCurrentStream() << t;
282 return _spxout;
283}
284
286{
287 _spxout.getCurrentStream() << t;
288 return _spxout;
289}
290
291inline SPxOut& operator<< (SPxOut& _spxout, unsigned int t)
292{
293 _spxout.getCurrentStream() << t;
294 return _spxout;
295}
296
298{
299 _spxout.getCurrentStream() << t;
300 return _spxout;
301}
302
304{
305 _spxout.getCurrentStream() << t;
306 return _spxout;
307}
308
309inline SPxOut& operator<< (SPxOut& _spxout, long double t)
310{
311 _spxout.getCurrentStream() << t;
312 return _spxout;
313}
314
315inline SPxOut& operator<< (SPxOut& _spxout, const void* t)
316{
317 _spxout.getCurrentStream() << t;
318 return _spxout;
319}
320
321/// Passes standard manipulators without arguments, like @c std::endl
322/// or @c std::ios::right to the current stream.
323inline SPxOut&
325 std::ostream & (*manip)(std::ostream&))
326{
327 _spxout.getCurrentStream() << manip;
328 return _spxout;
329}
330
331//lint -e{818} (pointer could be made const; this is ok.)
332/// Passes everything else to the current stream. In particular,
333/// this includes structs corresponding to manipulators with arguments,
334/// such as the struct @c _Setw for the @c setw() manipulator.
335template <typename T>
337{
338 _spxout.getCurrentStream() << t;
339 return _spxout;
340}
341//@}
342
343} // namespace soplex
344
345#endif // _SPXOUT_H_
Safe arrays of data objects.
Definition dataarray.h:75
Wrapper for several output streams. A verbosity level is used to decide which stream to use and wheth...
Definition spxout.h:73
std::ostream ** m_streams
array of pointers to internal streams, indexed by verbosity level
Definition spxout.h:200
Verbosity getVerbosity() const
Definition spxout.h:126
std::ostream & getStream(const Verbosity &verbosity) const
Returns the stream for the specified verbosity level.
Definition spxout.h:166
std::streamsize precision() const
Definition spxout.h:148
bool operator!() const
Definition spxout.h:143
SPxOut & operator=(const SPxOut &)
assignment operator
Definition spxout.cpp:52
static void setFixed(std::ostream &stream, int precision=8)
Sets the precision of the stream to 8 and the floatfield to fixed.
Definition spxout.h:186
virtual void setVerbosity(const Verbosity &v)
Definition spxout.h:120
bool good() const
Definition spxout.h:138
Verbosity
Verbosity level.
Definition spxout.h:82
std::ostream & getCurrentStream() const
Returns the stream for the current verbosity.
Definition spxout.h:173
virtual void setStream(const Verbosity &verbosity, std::ostream &stream)
Sets the stream for the specified verbosity level.
Definition spxout.h:159
SPxOut()
constructor
Definition spxout.cpp:32
virtual ~SPxOut()
destructor
Definition spxout.cpp:47
Verbosity m_verbosity
verbosity level
Definition spxout.h:198
static void setScientific(std::ostream &stream, int precision=8)
Sets the precision of the stream to 16 and the floatfield to scientifix.
Definition spxout.h:180
Everything should be within this namespace.
SPxOut::struct_Verbosity verb(const SPxOut::Verbosity &v)
manipulator to be used in an output statement
Definition spxout.h:227
Debugging, floating point type and parameter definitions.
helper struct for the output operator
Definition spxout.h:96
Verbosity v_
verbosity level
Definition spxout.h:98