From f445925e2b0be822b594da1bfcca70ecbfdbc1f4 Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Mon, 25 Oct 2021 16:17:58 -0600 Subject: [PATCH] Provide input and output operators for ReferenceCell. --- include/deal.II/grid/reference_cell.h | 28 +++++++++++++++++ source/grid/reference_cell.cc | 45 +++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) 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 -- 2.39.5