]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Initial implementation of ConditionalOStream class. Pregenerated pout object.
authorRalf Hartmann <Ralf.Hartmann@dlr.de>
Wed, 26 May 2004 16:19:27 +0000 (16:19 +0000)
committerRalf Hartmann <Ralf.Hartmann@dlr.de>
Wed, 26 May 2004 16:19:27 +0000 (16:19 +0000)
git-svn-id: https://svn.dealii.org/trunk@9332 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/base/include/base/conditional_ostream.h [new file with mode: 0644]
deal.II/base/source/conditional_ostream.cc [new file with mode: 0644]

diff --git a/deal.II/base/include/base/conditional_ostream.h b/deal.II/base/include/base/conditional_ostream.h
new file mode 100644 (file)
index 0000000..56171dc
--- /dev/null
@@ -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 <ostream>
+#else
+#  include <iostream>
+#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
+ * <tt>pout</tt> 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 <em>not</em> 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
+                                     * <tt>active</tt> flag set the
+                                     * condition of this stream to
+                                     * active (true) or non-active
+                                     * (false). An object of this
+                                     * class prints to <tt>cout</tt>
+                                     * 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 <typename T>
+    ConditionalOStream & operator << (const T &t);
+
+                                    /**
+                                     * Treat ostream manipulators.
+                                     */
+    ConditionalOStream & operator<< (std::ostream& (*p) (std::ostream&));
+
+  private:
+                                    /**
+                                     * Pointer to <tt>cout</tt>. 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 <class T>
+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 <tt>cout</tt>. Prints
+ * to standard output depending on <tt>pout</tt> 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 (file)
index 0000000..0f6646f
--- /dev/null
@@ -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 <base/conditional_ostream.h>
+
+
+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;
+}

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.