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-2016 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  /**@name Wrappers for the current stream */
126  //@{
127  ///
128  inline bool good() const
129  {
130  return getCurrentStream().good();
131  }
132  ///
133  inline bool operator ! () const
134  {
135  return ! getCurrentStream();
136  }
137  ///
138  inline std::streamsize precision() const
139  {
140  return getCurrentStream().precision();
141  }
142  //@}
143 
144  //-----------------------------------
145  /**@name Getting / setting streams */
146  //@{
147  /// Sets the stream for the specified verbosity level.
148  virtual void
149  setStream( const Verbosity& verbosity,
150  std::ostream& stream )
151  {
152  m_streams[ verbosity ] = &stream;
153  }
154  /// Returns the stream for the specified verbosity level.
155  inline std::ostream&
156  getStream( const Verbosity& verbosity )
157  const
158  {
159  return *(m_streams[ verbosity ]);
160  }
161  /// Returns the stream for the current verbosity.
162  inline std::ostream&
164  const
165  {
166  return getStream( getVerbosity() );
167  }
168  //@}
169 
170 private:
171 
172  //-----------------------------------
173  /**@name Private data */
174  //@{
175  /// verbosity level
177  /// array of pointers to internal streams, indexed by verbosity level
178  std::ostream** m_streams;
179  //@}
180 };
181 
182  // ---------------------------------------------------------
183  // Manipulators
184  // ---------------------------------------------------------
185 
186 
187  //-------------------------------------------
188  /**@name Verbosity manipulator
189  Manipulators are implemented in a similar way as done for @c setw(),
190  @c setprecision(), etc. in the standard library file iomanip. For
191  instance, the non-member function \ref verb() "verb(v)" returns a
192  struct struct_Severity which contains only the verbosity level.
193  Calling
194  @code
195  SPxOut spxout;
196  spxout << verb( SPxOut::ERROR ) << "This is an error!" << std::endl;
197  @endcode
198  passes such a struct to the output operator defined below, which
199  extracts the verbosity level from the struct and passes it to the
200  member function SPxOut::setVerbosity().
201  */
202  //@{
203  /// manipulator to be used in an output statement
206  {
207  SPxOut::struct_Verbosity verbosity;
208  verbosity.v_ = v;
209  return verbosity;
210  }
211 
212  /// output operator with verbosity level struct
213  inline SPxOut&
214  operator<< ( SPxOut& stream,
215  const SPxOut::struct_Verbosity& verbosity )
216  {
217  stream.setVerbosity( verbosity.v_ );
218  return stream;
219  }
220  //@}
221 
222  //--------------------------------------------------------
223  /**@name Output of standard manipulators and other types
224  *
225  * We have to define an output operator for many kinds of numeric
226  * types here because they can all be more or less casted into each
227  * other. When using only a template type, it is not clear what the
228  * compiler makes out of it (according to lint).
229  */
230  //@{
231  ///
232 #define PASS_TO_CURRENT_OSTREAM( t ) \
233  _spxout.getCurrentStream() << t; \
234  return _spxout;
235 
236  /// Passes instances of type \p Type to the current stream.
237 #define DEFINE_OUTPUT_OPERATOR( Type ) \
238  inline SPxOut& \
239  operator<< ( SPxOut& _spxout, Type t ) \
240  { PASS_TO_CURRENT_OSTREAM( t ) }
241 
242  DEFINE_OUTPUT_OPERATOR( long )
243  DEFINE_OUTPUT_OPERATOR( unsigned long )
244  DEFINE_OUTPUT_OPERATOR( bool )
245  DEFINE_OUTPUT_OPERATOR( short )
246  DEFINE_OUTPUT_OPERATOR( unsigned short )
248  DEFINE_OUTPUT_OPERATOR( unsigned int )
249  DEFINE_OUTPUT_OPERATOR( double )
250  DEFINE_OUTPUT_OPERATOR( float )
251  DEFINE_OUTPUT_OPERATOR( long double )
252  DEFINE_OUTPUT_OPERATOR( const void* )
253 
254  /// Passes standard manipulators without arguments, like @c std::endl
255  /// or @c std::ios::right to the current stream.
256  inline SPxOut&
257  operator<< ( SPxOut& _spxout,
258  std::ostream& (*manip)( std::ostream& ) )
259  { PASS_TO_CURRENT_OSTREAM( manip ) }
260 
261  //lint -e{818} (pointer could be made const; this is ok.)
262  /// Passes everything else to the current stream. In particular,
263  /// this includes structs corresponding to manipulators with arguments,
264  /// such as the struct @c _Setw for the @c setw() manipulator.
265  template< typename T >
266  inline SPxOut&
267  operator<< ( SPxOut& _spxout, T t )
268  { PASS_TO_CURRENT_OSTREAM( t ) }
269  //@}
270 
271 } // namespace soplex
272 
273 #endif // _SPXOUT_H_
SPxOut::struct_Verbosity verb(const SPxOut::Verbosity &v)
manipulator to be used in an output statement
Definition: spxout.h:205
virtual void setStream(const Verbosity &verbosity, std::ostream &stream)
Sets the stream for the specified verbosity level.
Definition: spxout.h:149
std::ostream ** m_streams
array of pointers to internal streams, indexed by verbosity level
Definition: spxout.h:178
Verbosity m_verbosity
verbosity level
Definition: spxout.h:176
virtual ~SPxOut()
destructor
Definition: spxout.cpp:37
helper struct for the output operator
Definition: spxout.h:86
bool good() const
Definition: spxout.h:128
SPxOut()
constructor
Definition: spxout.cpp:23
bool operator!() const
Definition: spxout.h:133
std::ostream & operator<<(std::ostream &s, const VectorBase< R > &vec)
Output operator.
Definition: basevectors.h:1087
std::ostream & getStream(const Verbosity &verbosity) const
Returns the stream for the specified verbosity level.
Definition: spxout.h:156
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:237
virtual void setVerbosity(const Verbosity &v)
Definition: spxout.h:111
Debugging, floating point type and parameter definitions.
Verbosity v_
verbosity level
Definition: spxout.h:89
Everything should be within this namespace.
Verbosity getVerbosity() const
Definition: spxout.h:117
Verbosity
Verbosity level.
Definition: spxout.h:72
#define PASS_TO_CURRENT_OSTREAM(t)
Definition: spxout.h:232
std::streamsize precision() const
Definition: spxout.h:138
SPxOut & operator=(const SPxOut &)
assignment operator
Definition: spxout.cpp:42
std::ostream & getCurrentStream() const
Returns the stream for the current verbosity.
Definition: spxout.h:163