Scippy

SoPlex

Sequential object-oriented simPlex

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-2017 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SoPlex is distributed under the terms of the ZIB Academic Licence. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SoPlex; see the file COPYING. If not email to soplex@zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file spxout.h
17  * @brief Wrapper for different output streams and verbosity levels.
18  */
19 #ifndef _SPXOUT_H_
20 #define _SPXOUT_H_
21 
22 #include <iostream>
23 #include <iomanip>
24 #include <assert.h>
25 
26 #include "spxdefines.h"
27 
28 // ----------------------------------------------------------------------
29 // class SPxOut
30 // ----------------------------------------------------------------------
31 
32 namespace soplex
33 {
34 
35 /**@class SPxOut
36  @ingroup Elementary
37 
38  @brief Wrapper for several output streams.
39  A verbosity level is used to decide which stream to use and whether to
40  really print a given message. Regardless of whether the verbosity level
41  is set via a manipulator or via the member function, it is persistent
42  until a new value is set.
43 
44  Most ostream member functions are not provided here; use the corresponding
45  stream manipulators (e.g., @c setprecision()) instead. These are passed on
46  to the <em>current</em> ostream, which is chosen according to the verbosity
47  level. In particular, this means that the first element in an output stream
48  should always be the verbosity. For instance, use
49  @code
50  spxout << verb( SPxOut::WARNING ) << std::setw( 15 ) << 42 << std::endl;
51  @endcode
52  or
53  @code
54  spxout.setVerbosity( SPxOut::WARNING );
55  spxout << std::setw( 15 ) << 42 << std::endl;
56  @endcode
57  instead of
58  @code
59  spxout << std::setw( 15 ) << verb( SPxOut::WARNING ) << 42 << std::endl;
60  @endcode
61  in order to make sure that @c std::setw( 15 ) is applied to the warning stream.
62 */
63 class SPxOut
64 {
65 public:
66 
67 
68  //-----------------------------------
69  /**@name Output control types */
70  //@{
71  /// Verbosity level
72  typedef enum
73  {
74  // Note: the implementation uses the fact that ERROR == 0
75  // and that the verbosity levels are subsequent numbers.
76  // If you change this, change the implementation as well.
77  ERROR = 0,
78  WARNING = 1,
79  DEBUG = 2,
80  INFO1 = 3,
81  INFO2 = 4,
82  INFO3 = 5
83  } Verbosity;
84 
85  /// helper struct for the output operator
87  {
88  /// verbosity level
90  };
91  //@}
92 
93  //-----------------------------------
94  /**@name Construction / destruction */
95  //@{
96  /// constructor
97  SPxOut();
98  /// destructor
99  virtual ~SPxOut();
100  /// copy constructor
101  SPxOut( const SPxOut& );
102  /// assignment operator
103  SPxOut& operator=( const SPxOut& );
104  //@}
105 
106  //-----------------------------------
107  /**@name Verbosity */
108  //@{
109  ///
110  virtual void
112  {
113  m_verbosity = v;
114  }
115  ///
116  inline Verbosity
118  const
119  {
120  return m_verbosity;
121  }
122 
123  //@}
124 
125  //----------------------------------------
126  /**@name Wrappers for the current stream */
127  //@{
128  ///
129  inline bool good() const
130  {
131  return getCurrentStream().good();
132  }
133  ///
134  inline bool operator ! () const
135  {
136  return ! getCurrentStream();
137  }
138  ///
139  inline std::streamsize precision() const
140  {
141  return getCurrentStream().precision();
142  }
143  //@}
144 
145  //-----------------------------------
146  /**@name Getting / setting streams */
147  //@{
148  /// Sets the stream for the specified verbosity level.
149  virtual void
150  setStream( const Verbosity& verbosity,
151  std::ostream& stream )
152  {
153  m_streams[ verbosity ] = &stream;
154  }
155  /// Returns the stream for the specified verbosity level.
156  inline std::ostream&
157  getStream( const Verbosity& verbosity )
158  const
159  {
160  return *(m_streams[ verbosity ]);
161  }
162  /// Returns the stream for the current verbosity.
163  inline std::ostream&
165  const
166  {
167  return getStream( getVerbosity() );
168  }
169  //@}
170 
171 private:
172 
173  //-----------------------------------
174  /**@name Private data */
175  //@{
176  /// verbosity level
178  /// array of pointers to internal streams, indexed by verbosity level
179  std::ostream** m_streams;
180  //@}
181 };
182 
183  // ---------------------------------------------------------
184  // Manipulators
185  // ---------------------------------------------------------
186 
187 
188  //-------------------------------------------
189  /**@name Verbosity manipulator
190  Manipulators are implemented in a similar way as done for @c setw(),
191  @c setprecision(), etc. in the standard library file iomanip. For
192  instance, the non-member function \ref verb() "verb(v)" returns a
193  struct struct_Severity which contains only the verbosity level.
194  Calling
195  @code
196  SPxOut spxout;
197  spxout << verb( SPxOut::ERROR ) << "This is an error!" << std::endl;
198  @endcode
199  passes such a struct to the output operator defined below, which
200  extracts the verbosity level from the struct and passes it to the
201  member function SPxOut::setVerbosity().
202  */
203  //@{
204  /// manipulator to be used in an output statement
207  {
208  SPxOut::struct_Verbosity verbosity;
209  verbosity.v_ = v;
210  return verbosity;
211  }
212 
213  /// output operator with verbosity level struct
214  inline SPxOut&
215  operator<< ( SPxOut& stream,
216  const SPxOut::struct_Verbosity& verbosity )
217  {
218  stream.setVerbosity( verbosity.v_ );
219  return stream;
220  }
221  //@}
222 
223  //--------------------------------------------------------
224  /**@name Output of standard manipulators and other types
225  *
226  * We have to define an output operator for many kinds of numeric
227  * types here because they can all be more or less casted into each
228  * other. When using only a template type, it is not clear what the
229  * compiler makes out of it (according to lint).
230  */
231  //@{
232  ///
233 #define PASS_TO_CURRENT_OSTREAM( t ) \
234  _spxout.getCurrentStream() << t; \
235  return _spxout;
236 
237  /// Passes instances of type \p Type to the current stream.
238 #define DEFINE_OUTPUT_OPERATOR( Type ) \
239  inline SPxOut& \
240  operator<< ( SPxOut& _spxout, Type t ) \
241  { PASS_TO_CURRENT_OSTREAM( t ) }
242 
243  DEFINE_OUTPUT_OPERATOR( long )
244  DEFINE_OUTPUT_OPERATOR( unsigned long )
245  DEFINE_OUTPUT_OPERATOR( bool )
246  DEFINE_OUTPUT_OPERATOR( short )
247  DEFINE_OUTPUT_OPERATOR( unsigned short )
249  DEFINE_OUTPUT_OPERATOR( unsigned int )
250  DEFINE_OUTPUT_OPERATOR( double )
251  DEFINE_OUTPUT_OPERATOR( float )
252  DEFINE_OUTPUT_OPERATOR( long double )
253  DEFINE_OUTPUT_OPERATOR( const void* )
254 
255  /// Passes standard manipulators without arguments, like @c std::endl
256  /// or @c std::ios::right to the current stream.
257  inline SPxOut&
258  operator<< ( SPxOut& _spxout,
259  std::ostream& (*manip)( std::ostream& ) )
260  { PASS_TO_CURRENT_OSTREAM( manip ) }
261 
262  //lint -e{818} (pointer could be made const; this is ok.)
263  /// Passes everything else to the current stream. In particular,
264  /// this includes structs corresponding to manipulators with arguments,
265  /// such as the struct @c _Setw for the @c setw() manipulator.
266  template< typename T >
267  inline SPxOut&
268  operator<< ( SPxOut& _spxout, T t )
269  { PASS_TO_CURRENT_OSTREAM( t ) }
270  //@}
271 
272 } // namespace soplex
273 
274 #endif // _SPXOUT_H_
SPxOut::struct_Verbosity verb(const SPxOut::Verbosity &v)
manipulator to be used in an output statement
Definition: spxout.h:206
virtual void setStream(const Verbosity &verbosity, std::ostream &stream)
Sets the stream for the specified verbosity level.
Definition: spxout.h:150
std::ostream ** m_streams
array of pointers to internal streams, indexed by verbosity level
Definition: spxout.h:179
Verbosity m_verbosity
verbosity level
Definition: spxout.h:177
virtual ~SPxOut()
destructor
Definition: spxout.cpp:37
helper struct for the output operator
Definition: spxout.h:86
std::ostream & getStream(const Verbosity &verbosity) const
Returns the stream for the specified verbosity level.
Definition: spxout.h:157
bool good() const
Definition: spxout.h:129
SPxOut()
constructor
Definition: spxout.cpp:23
std::ostream & getCurrentStream() const
Returns the stream for the current verbosity.
Definition: spxout.h:164
std::ostream & operator<<(std::ostream &s, const VectorBase< R > &vec)
Output operator.
Definition: basevectors.h:1151
Wrapper for several output streams. A verbosity level is used to decide which stream to use and wheth...
Definition: spxout.h:63
#define DEFINE_OUTPUT_OPERATOR(Type)
Passes instances of type Type to the current stream.
Definition: spxout.h:238
virtual void setVerbosity(const Verbosity &v)
Definition: spxout.h:111
Verbosity getVerbosity() const
Definition: spxout.h:117
Debugging, floating point type and parameter definitions.
Verbosity v_
verbosity level
Definition: spxout.h:89
Everything should be within this namespace.
std::streamsize precision() const
Definition: spxout.h:139
Verbosity
Verbosity level.
Definition: spxout.h:72
#define PASS_TO_CURRENT_OSTREAM(t)
Definition: spxout.h:233
SPxOut & operator=(const SPxOut &)
assignment operator
Definition: spxout.cpp:42
bool operator!() const
Definition: spxout.h:134