From 0c3d0a9c74be0323fbd8b93328fda2ccaab39b5f Mon Sep 17 00:00:00 2001 From: David Wells Date: Sat, 13 Jun 2015 12:50:23 -0400 Subject: [PATCH] Refactor output streams to use a base class. The various output format stream classes have very similar properties. --- source/base/data_out_base.cc | 674 ++++++++++------------------------- 1 file changed, 186 insertions(+), 488 deletions(-) diff --git a/source/base/data_out_base.cc b/source/base/data_out_base.cc index 6f2482b1d9..57e3ca85d0 100644 --- a/source/base/data_out_base.cc +++ b/source/base/data_out_base.cc @@ -666,43 +666,119 @@ namespace } /** - * Class for writing basic - * entities in @ref - * SoftwareOpenDX format, - * depending on the flags. + * Class describing common functionality between different output streams. + * + * @ingroup output */ - class DXStream + template + class StreamBase { public: + /* + * Constructor. Stores a reference to the output stream for immediate use. + */ + StreamBase (std::ostream &stream, + const FlagsType &flags) + : + selected_component (numbers::invalid_unsigned_int), + stream (stream), + flags (flags) + {} + /** - * Constructor, storing - * persistent values for - * later use. + * Output operator for points. All inheriting classes should implement this + * function. */ - DXStream (std::ostream &stream, - const DataOutBase::DXFlags flags); + template + void write_point (const unsigned int, + const Point &) + { + Assert (false, ExcMessage ("The derived class you are using needs to " + "reimplement this function if you want to call " + "it.")); + } /** - * Output operator for points. + * Do whatever is necessary to terminate the list of points. The default + * implementation does nothing; derived classes that do not require any + * action do not need to reimplement this. + */ + void flush_points () {} + + /** + * Write dim-dimensional cell with first vertex at number start and further + * vertices offset by the specified values. Values not needed are + * ignored. All inheriting classes should implement this function. */ template - void write_point (const unsigned int index, - const Point &); + void write_cell (const unsigned int /*index*/, + const unsigned int /*start*/, + const unsigned int /*x_offset*/, + const unsigned int /*y_offset*/, + const unsigned int /*z_offset*/) + { + Assert (false, ExcMessage ("The derived class you are using needs to " + "reimplement this function if you want to call " + "it.")); + } /** - * Do whatever is necessary to - * terminate the list of points. + * Do whatever is necessary to terminate the list of cells. This function is + * usually only reimplemented if deal.II is compiled with zlib. The default + * implementation does nothing; derived classes that do not require any + * action do not need to reimplement this. */ - void flush_points (); + void flush_cells () {} + + /** + * Forwarding of an output stream. This function is usually only + * reimplemented if inheriting classes use zlib. + */ + template + std::ostream &operator<< (const T &t) + { + stream << t; + return stream; + } + + /** + * Since the GMV and Tecplot formats read the x, y and z coordinates in + * separate fields, we enable write() to output only a single selected + * component at once and do this dim times for the whole set of nodes. This + * integer can be used to select the component written. + */ + unsigned int selected_component; + + protected: + /** + * The ostream to use. Since the life span of these objects is small, we use + * a very simple storage technique. + */ + std::ostream &stream; + + /** + * The flags controlling the output. + */ + const FlagsType flags; + }; + + /** + * Class for writing basic + * entities in @ref + * SoftwareOpenDX format, + * depending on the flags. + */ + class DXStream : public StreamBase + { + public: + DXStream (std::ostream &stream, + const DataOutBase::DXFlags &flags); + + template + void write_point (const unsigned int index, + const Point &); /** - * Write dim-dimensional cell - * with first vertex at - * number start and further - * vertices offset by the - * specified values. Values - * not needed are ignored. - * * The order of vertices for * these cells in different * dimensions is @@ -719,12 +795,6 @@ namespace const unsigned int y_offset, const unsigned int z_offset); - /** - * Do whatever is necessary to - * terminate the list of cells. - */ - void flush_cells (); - /** * Write a complete set of * data for a single node. @@ -738,27 +808,6 @@ namespace template void write_dataset (const unsigned int index, const std::vector &values); - - /** - * Forwarding of output stream - */ - template - std::ostream &operator<< (const T &); - - private: - /** - * The ostream to use. Since - * the life span of these - * objects is small, we use a - * very simple storage - * technique. - */ - std::ostream &stream; - - /** - * The flags controlling the output - */ - const DataOutBase::DXFlags flags; }; /** @@ -767,38 +816,17 @@ namespace * format, depending on the * flags. */ - class GmvStream + class GmvStream : public StreamBase { public: - /** - * Constructor, storing - * persistent values for - * later use. - */ GmvStream (std::ostream &stream, - const DataOutBase::GmvFlags flags); + const DataOutBase::GmvFlags &flags); - /** - * Output operator for points. - */ template void write_point (const unsigned int index, const Point &); /** - * Do whatever is necessary to - * terminate the list of points. - */ - void flush_points (); - - /** - * Write dim-dimensional cell - * with first vertex at - * number start and further - * vertices offset by the - * specified values. Values - * not needed are ignored. - * * The order of vertices for * these cells in different * dimensions is @@ -809,54 +837,11 @@ namespace * */ template - void write_cell(const unsigned int index, - const unsigned int start, - const unsigned int x_offset, - const unsigned int y_offset, - const unsigned int z_offset); - - /** - * Do whatever is necessary to - * terminate the list of cells. - */ - void flush_cells (); - - /** - * Forwarding of output stream - */ - template - std::ostream &operator<< (const T &); - - /** - * Since GMV reads the x, y - * and z coordinates in - * separate fields, we enable - * write() to output only a - * single selected component - * at once and do this dim - * times for the whole set of - * nodes. This integer can be - * used to select the - * component written. - */ - unsigned int selected_component; - - private: - /** - * The ostream to use. Since - * the life span of these - * objects is small, we use a - * very simple storage - * technique. - */ - std::ostream &stream; - - /** - * The flags controlling the output - */ - DEAL_II_DISABLE_EXTRA_DIAGNOSTICS - const DataOutBase::GmvFlags flags; - DEAL_II_ENABLE_EXTRA_DIAGNOSTICS + void write_cell (const unsigned int index, + const unsigned int start, + const unsigned int x_offset, + const unsigned int y_offset, + const unsigned int z_offset); }; /** @@ -865,37 +850,17 @@ namespace * SoftwareTecplot format, * depending on the flags. */ - class TecplotStream + class TecplotStream : public StreamBase { public: - /** - * Constructor, storing - * persistent values for - * later use. - */ - TecplotStream (std::ostream &stream, const DataOutBase::TecplotFlags flags); + TecplotStream (std::ostream &stream, + const DataOutBase::TecplotFlags &flags); - /** - * Output operator for points. - */ template void write_point (const unsigned int index, const Point &); /** - * Do whatever is necessary to - * terminate the list of points. - */ - void flush_points (); - - /** - * Write dim-dimensional cell - * with first vertex at - * number start and further - * vertices offset by the - * specified values. Values - * not needed are ignored. - * * The order of vertices for * these cells in different * dimensions is @@ -906,52 +871,11 @@ namespace * */ template - void write_cell(const unsigned int index, - const unsigned int start, - const unsigned int x_offset, - const unsigned int y_offset, - const unsigned int z_offset); - - /** - * Do whatever is necessary to - * terminate the list of cells. - */ - void flush_cells (); - - /** - * Forwarding of output stream - */ - template - std::ostream &operator<< (const T &); - - /** - * Since TECPLOT reads the x, y - * and z coordinates in - * separate fields, we enable - * write() to output only a - * single selected component - * at once and do this dim - * times for the whole set of - * nodes. This integer can be - * used to select the - * component written. - */ - unsigned int selected_component; - - private: - /** - * The ostream to use. Since - * the life span of these - * objects is small, we use a - * very simple storage - * technique. - */ - std::ostream &stream; - - /** - * The flags controlling the output - */ - const DataOutBase::TecplotFlags flags; + void write_cell (const unsigned int index, + const unsigned int start, + const unsigned int x_offset, + const unsigned int y_offset, + const unsigned int z_offset); }; /** @@ -960,38 +884,17 @@ namespace * @ref SoftwareAVS, depending on * the flags. */ - class UcdStream + class UcdStream : public StreamBase { public: - /** - * Constructor, storing - * persistent values for - * later use. - */ UcdStream (std::ostream &stream, - const DataOutBase::UcdFlags flags); + const DataOutBase::UcdFlags &flags); - /** - * Output operator for points. - */ template void write_point (const unsigned int index, const Point &); /** - * Do whatever is necessary to - * terminate the list of points. - */ - void flush_points (); - - /** - * Write dim-dimensional cell - * with first vertex at - * number start and further - * vertices offset by the - * specified values. Values - * not needed are ignored. - * * The additional offset 1 is * added inside this * function. @@ -1006,17 +909,11 @@ namespace * */ template - void write_cell(const unsigned int index, - const unsigned int start, - const unsigned int x_offset, - const unsigned int y_offset, - const unsigned int z_offset); - - /** - * Do whatever is necessary to - * terminate the list of cells. - */ - void flush_cells (); + void write_cell (const unsigned int index, + const unsigned int start, + const unsigned int x_offset, + const unsigned int y_offset, + const unsigned int z_offset); /** * Write a complete set of @@ -1031,26 +928,6 @@ namespace template void write_dataset (const unsigned int index, const std::vector &values); - - /** - * Forwarding of output stream - */ - template - std::ostream &operator<< (const T &); - private: - /** - * The ostream to use. Since - * the life span of these - * objects is small, we use a - * very simple storage - * technique. - */ - std::ostream &stream; - - /** - * The flags controlling the output - */ - const DataOutBase::UcdFlags flags; }; /** @@ -1059,38 +936,17 @@ namespace * format, depending on the * flags. */ - class VtkStream + class VtkStream : public StreamBase { public: - /** - * Constructor, storing - * persistent values for - * later use. - */ VtkStream (std::ostream &stream, - const DataOutBase::VtkFlags flags); + const DataOutBase::VtkFlags &flags); - /** - * Output operator for points. - */ template void write_point (const unsigned int index, const Point &); /** - * Do whatever is necessary to - * terminate the list of points. - */ - void flush_points (); - - /** - * Write dim-dimensional cell - * with first vertex at - * number start and further - * vertices offset by the - * specified values. Values - * not needed are ignored. - * * The order of vertices for * these cells in different * dimensions is @@ -1101,73 +957,27 @@ namespace * */ template - void write_cell(const unsigned int index, - const unsigned int start, - const unsigned int x_offset, - const unsigned int y_offset, - const unsigned int z_offset); - - /** - * Do whatever is necessary to - * terminate the list of cells. - */ - void flush_cells (); - - /** - * Forwarding of output stream - */ - template - std::ostream &operator<< (const T &); - - private: - /** - * The ostream to use. Since - * the life span of these - * objects is small, we use a - * very simple storage - * technique. - */ - std::ostream &stream; - - /** - * The flags controlling the output - */ - const DataOutBase::VtkFlags flags; + void write_cell (const unsigned int index, + const unsigned int start, + const unsigned int x_offset, + const unsigned int y_offset, + const unsigned int z_offset); }; - class VtuStream + class VtuStream : public StreamBase { public: - /** - * Constructor, storing - * persistent values for - * later use. - */ VtuStream (std::ostream &stream, - const DataOutBase::VtkFlags flags); + const DataOutBase::VtkFlags &flags); - /** - * Output operator for points. - */ template void write_point (const unsigned int index, const Point &); - /** - * Do whatever is necessary to - * terminate the list of points. - */ void flush_points (); /** - * Write dim-dimensional cell - * with first vertex at - * number start and further - * vertices offset by the - * specified values. Values - * not needed are ignored. - * * The order of vertices for * these cells in different * dimensions is @@ -1178,21 +988,14 @@ namespace * */ template - void write_cell(const unsigned int index, - const unsigned int start, - const unsigned int x_offset, - const unsigned int y_offset, - const unsigned int z_offset); + void write_cell (const unsigned int index, + const unsigned int start, + const unsigned int x_offset, + const unsigned int y_offset, + const unsigned int z_offset); - /** - * Do whatever is necessary to - * terminate the list of cells. - */ void flush_cells (); - /** - * Forwarding of output stream - */ template std::ostream &operator<< (const T &); @@ -1211,20 +1014,6 @@ namespace std::ostream &operator<< (const std::vector &); private: - /** - * The ostream to use. Since - * the life span of these - * objects is small, we use a - * very simple storage - * technique. - */ - std::ostream &stream; - - /** - * The flags controlling the output - */ - const DataOutBase::VtkFlags flags; - /** * A list of vertices and * cells, to be used in case we @@ -1244,10 +1033,10 @@ namespace //----------------------------------------------------------------------// - DXStream::DXStream(std::ostream &out, - const DataOutBase::DXFlags f) + DXStream::DXStream (std::ostream &out, + const DataOutBase::DXFlags &f) : - stream(out), flags(f) + StreamBase (out, f) {} @@ -1273,19 +1062,14 @@ namespace } - void - DXStream::flush_points () - {} - template void - DXStream::write_cell( - unsigned int, - unsigned int start, - unsigned int d1, - unsigned int d2, - unsigned int d3) + DXStream::write_cell (unsigned int, + unsigned int start, + unsigned int d1, + unsigned int d2, + unsigned int d3) { int nodes[1<::dx_to_deal[0]] = start; @@ -1318,16 +1102,12 @@ namespace } - void - DXStream::flush_cells () - {} - template inline void - DXStream::write_dataset(const unsigned int /*index*/, - const std::vector &values) + DXStream::write_dataset (const unsigned int, + const std::vector &values) { if (flags.data_binary) { @@ -1347,10 +1127,9 @@ namespace //----------------------------------------------------------------------// GmvStream::GmvStream (std::ostream &out, - const DataOutBase::GmvFlags f) + const DataOutBase::GmvFlags &f) : - selected_component(numbers::invalid_unsigned_int), - stream(out), flags(f) + StreamBase (out, f) {} @@ -1365,19 +1144,14 @@ namespace } - void - GmvStream::flush_points () - {} - template void - GmvStream::write_cell( - unsigned int, - unsigned int s, - unsigned int d1, - unsigned int d2, - unsigned int d3) + GmvStream::write_cell (unsigned int, + unsigned int s, + unsigned int d1, + unsigned int d2, + unsigned int d3) { // Vertices are numbered starting // with one. @@ -1403,17 +1177,10 @@ namespace - void - GmvStream::flush_cells () - {} - - -//----------------------------------------------------------------------// - - TecplotStream::TecplotStream(std::ostream &out, const DataOutBase::TecplotFlags f) + TecplotStream::TecplotStream (std::ostream &out, + const DataOutBase::TecplotFlags &f) : - selected_component(numbers::invalid_unsigned_int), - stream(out), flags(f) + StreamBase (out, f) {} @@ -1428,19 +1195,14 @@ namespace } - void - TecplotStream::flush_points () - {} - template void - TecplotStream::write_cell( - unsigned int, - unsigned int s, - unsigned int d1, - unsigned int d2, - unsigned int d3) + TecplotStream::write_cell (unsigned int, + unsigned int s, + unsigned int d1, + unsigned int d2, + unsigned int d3) { const unsigned int start = s+1; @@ -1463,17 +1225,10 @@ namespace - void - TecplotStream::flush_cells () - {} - - - -//----------------------------------------------------------------------// - - UcdStream::UcdStream(std::ostream &out, const DataOutBase::UcdFlags f) + UcdStream::UcdStream (std::ostream &out, + const DataOutBase::UcdFlags &f) : - stream(out), flags(f) + StreamBase (out, f) {} @@ -1495,19 +1250,13 @@ namespace - void - UcdStream::flush_points () - {} - - template void - UcdStream::write_cell( - unsigned int index, - unsigned int start, - unsigned int d1, - unsigned int d2, - unsigned int d3) + UcdStream::write_cell (unsigned int index, + unsigned int start, + unsigned int d1, + unsigned int d2, + unsigned int d3) { int nodes[1<::ucd_to_deal[0]] = start; @@ -1539,16 +1288,11 @@ namespace - void - UcdStream::flush_cells () - {} - - template inline void - UcdStream::write_dataset(const unsigned int index, - const std::vector &values) + UcdStream::write_dataset (const unsigned int index, + const std::vector &values) { stream << index+1; for (unsigned int i=0; i void - VtkStream::write_cell( - unsigned int, - unsigned int start, - unsigned int d1, - unsigned int d2, - unsigned int d3) + VtkStream::write_cell (unsigned int, + unsigned int start, + unsigned int d1, + unsigned int d2, + unsigned int d3) { stream << GeometryInfo::vertices_per_cell << '\t' << start << '\t' @@ -1614,15 +1353,11 @@ namespace } - void - VtkStream::flush_cells () - {} - - - VtuStream::VtuStream(std::ostream &out, const DataOutBase::VtkFlags f) + VtuStream::VtuStream (std::ostream &out, + const DataOutBase::VtkFlags &f) : - stream(out), flags(f) + StreamBase (out, f) {} @@ -1665,12 +1400,11 @@ namespace template void - VtuStream::write_cell( - unsigned int, - unsigned int start, - unsigned int d1, - unsigned int d2, - unsigned int d3) + VtuStream::write_cell (unsigned int, + unsigned int start, + unsigned int d1, + unsigned int d2, + unsigned int d3) { #if !defined(DEAL_II_WITH_ZLIB) stream << start << '\t' @@ -1737,45 +1471,9 @@ namespace return stream; } - - template - std::ostream & - DXStream::operator<< (const T &t) - { - stream << t; - return stream; - } - - - template - std::ostream & - GmvStream::operator<< (const T &t) - { - stream << t; - return stream; - } - - - template - std::ostream & - UcdStream::operator<< (const T &t) - { - stream << t; - return stream; - } - - - template - std::ostream & - VtkStream::operator<< (const T &t) - { - stream << t; - return stream; - } } -//----------------------------------------------------------------------// namespace DataOutBase { -- 2.39.5