ConditionalOStream Class Reference
[Textual output]

List of all members.

Public Member Functions

 ConditionalOStream (std::ostream &stream, const bool active=true)
void set_condition (const bool active)
bool is_active () const
template<typename T >
const ConditionalOStreamoperator<< (const T &t) const
const ConditionalOStreamoperator<< (std::ostream &(*p)(std::ostream &)) const

Private Attributes

std::ostream & output_stream
bool active_flag

Detailed Description

A class that allows printing to an output stream, e.g. std::cout, depending on the ConditionalOStream object being active (default) or not. The condition of this object can be changed by set_condition() and in the constructor. This class is used in the step-17, step-18, and step-33 tutorial programs.

This class is mostly useful in parallel computations. Ordinarily, you would use std::cout to print messages like what the program is presently doing, or the number of degrees of freedom in each step. However, in parallel programs, this means that each of the MPI processes write to the screen, which yields many repetitions of the same text. To avoid it, one would have to have a designated process, say the one with MPI process number zero, do the output, and guard each write statement with an if-condition. This becomes cumbersome and clutters up the code. Rather than doing so, the present class can be used: objects of its type act just like a standard output stream, but they only print something based on a condition that can be set to, for example, mpi_process==0, so that only one process has a true condition and in all other processes writes to this object just disappear in nirvana.

The usual usage of this class is as follows:

 ConditionalOStream pout(std::cout, this_mpi_process==0);

                                  // all processes print following
                                  // information to standard output
 std::cout << "Reading parameter file on process "
           << this_mpi_process << std::endl;

                                  // following is printed by
                                  // process 0 only
 pout << "Solving ..." << std::endl;
 solve();
 pout << "done" << std::endl;

Here, `Reading parameter file on process xy' is printed by each process separately. In contrast to that, `Solving ...' and `done' is printed to standard output only once, namely by process 0.

This class is not derived from ostream. Therefore

 system_matrix.print_formatted(pout);

is not possible. Instead use the is_active() funtion for a work-around:

 if (pout.is_active())
   system_matrix.print_formatted(cout);
Author:
Ralf Hartmann, Wolfgang Bangerth, 2004

Constructor & Destructor Documentation

ConditionalOStream::ConditionalOStream ( std::ostream &  stream,
const bool  active = true 
)

Constructor. Set the stream to which we want to write, and the condition based on which writes are actually forwarded. Per default the condition of an object is active.


Member Function Documentation

void ConditionalOStream::set_condition ( const bool  active  ) 

Depending on the active flag set the condition of this stream to active (true) or non-active (false). An object of this class prints to cout if and only if its condition is active.

bool ConditionalOStream::is_active (  )  const

Return the condition of the object.

template<class T >
const ConditionalOStream & ConditionalOStream::operator<< ( const T &  t  )  const [inline]

Output a constant something through this stream. This function must be const so that member objects of this type can also be used from const member functions of the surrounding class.

References active_flag, and output_stream.

const ConditionalOStream & ConditionalOStream::operator<< ( std::ostream &(*)(std::ostream &)  p  )  const [inline]

Treat ostream manipulators. This function must be const so that member objects of this type can also be used from const member functions of the surrounding class.

Note that compilers want to see this treated differently from the general template above since functions like std::endl are actually overloaded and can't be bound directly to a template type.

References active_flag, and output_stream.


Member Data Documentation

std::ostream& ConditionalOStream::output_stream [private]

Reference to the stream we want to write to.

Referenced by operator<<().

Stores the actual condition the object is in.

Referenced by operator<<().


The documentation for this class was generated from the following file:

deal.II documentation generated on Mon Nov 23 22:57:33 2009 by doxygen 1.6.1