From: Wolfgang Bangerth Date: Mon, 25 Oct 2021 22:17:58 +0000 (-0600) Subject: Provide input and output operators for ReferenceCell. X-Git-Tag: v9.4.0-rc1~888^2~2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f445925e2b0be822b594da1bfcca70ecbfdbc1f4;p=dealii.git Provide input and output operators for ReferenceCell. --- diff --git a/include/deal.II/grid/reference_cell.h b/include/deal.II/grid/reference_cell.h index 190f3d3fef..d34ccd09e4 100644 --- a/include/deal.II/grid/reference_cell.h +++ b/include/deal.II/grid/reference_cell.h @@ -25,6 +25,8 @@ #include #include +#include +#include DEAL_II_NAMESPACE_OPEN @@ -582,9 +584,35 @@ private: */ friend DEAL_II_CONSTEXPR ReferenceCell internal::ReferenceCell::make_reference_cell_from_int(const std::uint8_t); + + friend std::ostream & + operator<<(std::ostream &out, const ReferenceCell &reference_cell); + + friend std::istream & + operator>>(std::istream &in, ReferenceCell &reference_cell); }; +/** + * Output operator that writes the @p reference_cell object to the stream + * in a text format in which the object is represented by an integer. The + * details of which integer value represents each kind of reference cell + * is unimportant and consequently not specified. If you want a string + * representation of what a ReferenceCell is, use ReferenceCell::to_string(). + */ +std::ostream & +operator<<(std::ostream &out, const ReferenceCell &reference_cell); + +/** + * Input operator that reads the @p reference_cell object from the stream + * in a text format in which the object is represented by an integer. Which + * specific integer value represents which reference cell is unspecified, + * but the function uses the same translation as the corresponding + * output `operator<<`. + */ +std::istream & +operator>>(std::istream &in, ReferenceCell &reference_cell); + inline constexpr ReferenceCell::ReferenceCell(const std::uint8_t kind) : kind(kind) diff --git a/source/grid/reference_cell.cc b/source/grid/reference_cell.cc index a730be32c1..b8f518ef62 100644 --- a/source/grid/reference_cell.cc +++ b/source/grid/reference_cell.cc @@ -30,6 +30,7 @@ #include #include +#include #include DEAL_II_NAMESPACE_OPEN @@ -455,6 +456,50 @@ ReferenceCell::vtk_lagrange_type() const return VTKCellType::VTK_INVALID; } + + +std::ostream & +operator<<(std::ostream &out, const ReferenceCell &reference_cell) +{ + AssertThrow(out, ExcIO()); + + // Output as an integer to avoid outputting it as a character with + // potentially non-printing value: + out << static_cast(reference_cell.kind); + return out; +} + + + +std::istream & +operator>>(std::istream &in, ReferenceCell &reference_cell) +{ + AssertThrow(in, ExcIO()); + + // Read the information as an integer and convert it to the correct type + unsigned int value; + in >> value; + reference_cell.kind = static_cast(value); + + // Ensure that the object we read is valid + Assert( + (reference_cell == ReferenceCells::Vertex) || + (reference_cell == ReferenceCells::Line) || + (reference_cell == ReferenceCells::Triangle) || + (reference_cell == ReferenceCells::Quadrilateral) || + (reference_cell == ReferenceCells::Tetrahedron) || + (reference_cell == ReferenceCells::Hexahedron) || + (reference_cell == ReferenceCells::Wedge) || + (reference_cell == ReferenceCells::Pyramid) || + (reference_cell == ReferenceCells::Invalid), + ExcMessage( + "The reference cell kind just read does not correspond to one of the valid choices. There must be an error.")); + + return in; +} + + + #include "reference_cell.inst" DEAL_II_NAMESPACE_CLOSE