]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Provide input and output operators for ReferenceCell.
authorWolfgang Bangerth <bangerth@colostate.edu>
Mon, 25 Oct 2021 22:17:58 +0000 (16:17 -0600)
committerWolfgang Bangerth <bangerth@colostate.edu>
Tue, 26 Oct 2021 02:41:21 +0000 (20:41 -0600)
include/deal.II/grid/reference_cell.h
source/grid/reference_cell.cc

index 190f3d3fef0e493932413e37daff25cbdee8ae34..d34ccd09e49c6414131ce65e1bbc567da02d1301 100644 (file)
@@ -25,6 +25,8 @@
 #include <deal.II/base/tensor.h>
 #include <deal.II/base/utilities.h>
 
+#include <iosfwd>
+#include <string>
 
 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)
index a730be32c16cc582826c0544529b4a871c19d938..b8f518ef620d28fb52855a62ea6ab4a04607f020 100644 (file)
@@ -30,6 +30,7 @@
 #include <deal.II/grid/reference_cell.h>
 #include <deal.II/grid/tria.h>
 
+#include <iostream>
 #include <memory>
 
 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<unsigned int>(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<decltype(reference_cell.kind)>(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

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.