From 42144356e8f2997d99586679dbd10476c62c9c1a Mon Sep 17 00:00:00 2001 From: hartmann Date: Wed, 26 May 2004 16:19:27 +0000 Subject: [PATCH] Initial implementation of ConditionalOStream class. Pregenerated pout object. git-svn-id: https://svn.dealii.org/trunk@9332 0785d39b-7218-0410-832d-ea1e28bc413d --- .../base/include/base/conditional_ostream.h | 178 ++++++++++++++++++ deal.II/base/source/conditional_ostream.cc | 35 ++++ 2 files changed, 213 insertions(+) create mode 100644 deal.II/base/include/base/conditional_ostream.h create mode 100644 deal.II/base/source/conditional_ostream.cc diff --git a/deal.II/base/include/base/conditional_ostream.h b/deal.II/base/include/base/conditional_ostream.h new file mode 100644 index 0000000000..56171dcd2e --- /dev/null +++ b/deal.II/base/include/base/conditional_ostream.h @@ -0,0 +1,178 @@ +//----------------------- conditional_ostream.h ---------------------- +// $Id$ +// Version: $Name$ +// +// Copyright (C) 2004 by the deal.II authors +// +// This file is subject to QPL and may not be distributed +// without copyright and license information. Please refer +// to the file deal.II/doc/license.html for the text and +// further information on this license. +// +//----------------------- conditional_ostream.h ---------------------- +#ifndef __deal2__conditional_ostream_h +#define __deal2__conditional_ostream_h + + + +#ifdef HAVE_STD_OSTREAM_HEADER +# include +#else +# include +#endif + + +/** + * A class that allows printing to standard output, i.e. cout, + * depending on the ConditionalOStream object being active (default) + * or not. The condition of this object can be changed by + * set_condition(). + * + * The usual usage of this class is through the pregenerated object + * pout which can be used within parallel computations as + * follows: + * + * @code + * pout.set_condition(this_mpi_process==0); + * + * // all processes print following + * // information to standard output + * cout << "Reading parameter file on process " << this_mpi_process << endl; + * + * // following is printed by + * // process 0 only + * pout << "Solving ..." << endl; + * solve(); + * pout << "done" << endl; + * @endcode + * + * 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 + * @code + * system_matrix.print_formatted(pout); + * @endcode + * is not possible. Instead use the is_active() funtion for a + * work-around: + * + * @code + * if (pout.is_active()) + * system_matrix.print_formatted(cout); + * @endcode + * + * @author Ralf Hartmann, 2004 + */ +class ConditionalOStream +{ + public: + /** + * Constructor. Per default the + * condition of an object is + * active. + */ + ConditionalOStream(); + + /** + * 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. + */ + void set_condition(bool active); + + /** + * Give read access to the + * condition of the object. + */ + bool is_active() const; + + /** + * Output a constant something + * through this stream. + */ + template + ConditionalOStream & operator << (const T &t); + + /** + * Treat ostream manipulators. + */ + ConditionalOStream & operator<< (std::ostream& (*p) (std::ostream&)); + + private: + /** + * Pointer to cout. This + * class could easily be extended + * to treat streams different + * to the standard output. + */ + std::ostream *std_out; + + /** + * Stores the actual condition + * the object is in. + */ + bool active_flag; +}; + + +template +inline +ConditionalOStream & +ConditionalOStream::operator<< (const T& t) +{ + if (active_flag) + *std_out << t; + + return *this; +} + + +inline +ConditionalOStream & +ConditionalOStream::operator<< (std::ostream& (*p) (std::ostream&)) +{ + if (active_flag) + *std_out << p; + + return *this; +} + + +/** + * Pregenerated object for conditional output to cout. Prints + * to standard output depending on pout being active (default) + * or not. The output can be disabled by set_condition(false). + * + * This object is particularly useful in the context of parallel + * computations in order to avoid multiple but identical output by + * several processes. + * + * @code + * pout.set_condition(this_mpi_process==0); + * + * // all processes print following + * // information to standard output + * cout << "Reading parameter file on process " << this_mpi_process << endl; + * + * // following is printed by + * // process 0 only + * pout << "Solving ..." << endl; + * solve(); + * pout << "done" << endl; + * @endcode + * + * 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. + * + * @author Ralf Hartmann, 2004 + */ +extern ConditionalOStream pout; + +#endif diff --git a/deal.II/base/source/conditional_ostream.cc b/deal.II/base/source/conditional_ostream.cc new file mode 100644 index 0000000000..0f6646f5d5 --- /dev/null +++ b/deal.II/base/source/conditional_ostream.cc @@ -0,0 +1,35 @@ +//---------------------------- log.cc --------------------------- +// $Id$ +// Version: $Name$ +// +// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 by the deal.II authors +// +// This file is subject to QPL and may not be distributed +// without copyright and license information. Please refer +// to the file deal.II/doc/license.html for the text and +// further information on this license. +// +//---------------------------- log.cc --------------------------- + +#include + + +ConditionalOStream pout; + + +ConditionalOStream::ConditionalOStream(): + std_out(&cout), + active_flag(true) +{} + + +void ConditionalOStream::set_condition(bool flag) +{ + active_flag=flag; +} + + +bool ConditionalOStream::is_active() const +{ + return active_flag; +} -- 2.39.5