diff --git a/dune/vtk/utility/CMakeLists.txt b/dune/vtk/utility/CMakeLists.txt index efed088eecbd4209a6ed113647ea3500efe5b064..a611ab983b2140448d2b872e2084ba5ef99df37d 100644 --- a/dune/vtk/utility/CMakeLists.txt +++ b/dune/vtk/utility/CMakeLists.txt @@ -4,6 +4,7 @@ dune_add_library("filesystem" OBJECT #install headers install(FILES enum.hh + errors.hh filesystem.hh lagrangepoints.hh lagrangepoints.impl.hh diff --git a/dune/vtk/utility/errors.hh b/dune/vtk/utility/errors.hh new file mode 100644 index 0000000000000000000000000000000000000000..2961c0ce6073f4f5b1cc2d0cc6aa86b53d529599 --- /dev/null +++ b/dune/vtk/utility/errors.hh @@ -0,0 +1,23 @@ +#pragma once + +#include <dune/common/exceptions.hh> + +/** + * \file + * \brief Macro for wrapping error checks and throwing exceptions + */ + +namespace Dune { + +class VtkError : public Exception {}; + +} + +/** + * \brief check if condition \a cond holds; otherwise, throw a VtkError. + */ +#define VTK_ASSERT(cond, text) \ + do { \ + if (!(cond)) \ + DUNE_THROW(Dune::VtkError, text); \ + } while (false) diff --git a/dune/vtk/vtkreader.impl.hh b/dune/vtk/vtkreader.impl.hh index 308b33dd296cf03a407fed7ec512834213d0d580..b9f42f8d2a21a80bfaaf36b77bf1bc23eb283364 100644 --- a/dune/vtk/vtkreader.impl.hh +++ b/dune/vtk/vtkreader.impl.hh @@ -10,6 +10,7 @@ #include <dune/common/classname.hh> #include <dune/common/version.hh> +#include "utility/errors.hh" #include "utility/filesystem.hh" #include "utility/string.hh" @@ -32,7 +33,7 @@ void VtkReader<Grid,Creator>::read (std::string const& filename, bool fillCreato } else if (ext == ".pvtu") { readParallelFileFromStream(input, comm().rank(), comm().size(), fillCreator); } else { - DUNE_THROW(IOError, "File has unknown file-extension '" << ext << "'. Allowed are only '.vtu' and '.pvtu'."); + DUNE_THROW(Dune::VtkError, "File has unknown file-extension '" << ext << "'. Allowed are only '.vtu' and '.pvtu'."); } } @@ -56,16 +57,16 @@ void VtkReader<Grid,Creator>::readSerialFileFromStream (std::ifstream& input, bo auto attr = parseXml(line, closed); if (!attr["type"].empty()) - assert(attr["type"] == "UnstructuredGrid"); + VTK_ASSERT(attr["type"] == "UnstructuredGrid", "VtkReader supports UnstructuredGrid types"); if (!attr["version"].empty()) - assert(std::stod(attr["version"]) == 1.0); + VTK_ASSERT(std::stod(attr["version"]) == 1.0, "File format must be 1.0"); if (!attr["byte_order"].empty()) - assert(attr["byte_order"] == "LittleEndian"); + VTK_ASSERT(attr["byte_order"] == "LittleEndian", "LittleEndian byte order supported"); if (!attr["header_type"].empty()) - assert(attr["header_type"] == "UInt64"); + VTK_ASSERT(attr["header_type"] == "UInt64", "Header integer type must be UInt64"); if (!attr["compressor"].empty()) { compressor = attr["compressor"]; - assert(compressor == "vtkZLibDataCompressor"); // only ZLib compression supported + VTK_ASSERT(compressor == "vtkZLibDataCompressor", "Only ZLib compression supported"); } section = VTK_FILE; } @@ -79,7 +80,7 @@ void VtkReader<Grid,Creator>::readSerialFileFromStream (std::ifstream& input, bo bool closed = false; auto attr = parseXml(line, closed); - assert(attr.count("NumberOfPoints") > 0 && attr.count("NumberOfCells") > 0); + VTK_ASSERT(attr.count("NumberOfPoints") > 0 && attr.count("NumberOfCells") > 0, "Number of points or cells in file must be > 0"); numberOfPoints_ = std::stoul(attr["NumberOfPoints"]); numberOfCells_ = std::stoul(attr["NumberOfCells"]); section = PIECE; @@ -129,7 +130,7 @@ void VtkReader<Grid,Creator>::readSerialFileFromStream (std::ifstream& input, bo data_offset = 0; if (!attr["offset"].empty()) { data_offset = std::stoul(attr["offset"]); - assert(data_format == "appended"); + VTK_ASSERT(data_format == "appended", "Attribute 'offset' only supported by appended mode"); } // Store attributes of DataArray @@ -156,7 +157,7 @@ void VtkReader<Grid,Creator>::readSerialFileFromStream (std::ifstream& input, bo else if (section == CELLS) section = CELLS_DATA_ARRAY; else - DUNE_THROW(Exception, "Wrong section for <DataArray>"); + DUNE_THROW(Dune::VtkError, "Wrong section for <DataArray>"); } else if (line.substr(1,10) == "/DataArray") { if (section == PD_DATA_ARRAY) @@ -168,13 +169,13 @@ void VtkReader<Grid,Creator>::readSerialFileFromStream (std::ifstream& input, bo else if (section == CELLS_DATA_ARRAY) section = CELLS; else - DUNE_THROW(Exception, "Wrong section for </DataArray>"); + DUNE_THROW(Dune::VtkError, "Wrong section for </DataArray>"); } else if (isSection(line, "AppendedData", section, VTK_FILE)) { bool closed = false; auto attr = parseXml(line, closed); if (!attr["encoding"].empty()) - assert(attr["encoding"] == "raw"); // base64 encoding not supported + VTK_ASSERT(attr["encoding"] == "raw", "base64 encoding not supported"); offset0_ = findAppendedDataPosition(input); if (dataArray_["points"].type == Vtk::FLOAT32) @@ -223,7 +224,7 @@ void VtkReader<Grid,Creator>::readSerialFileFromStream (std::ifstream& input, bo } if (section != NO_SECTION) - DUNE_THROW(IOError, "VTK-File is incomplete. It must end with </VTKFile>!"); + DUNE_THROW(Dune::VtkError, "VTK-File is incomplete. It must end with </VTKFile>!"); if (fillCreator) fillGridCreator(); @@ -244,15 +245,15 @@ void VtkReader<Grid,Creator>::readParallelFileFromStream (std::ifstream& input, auto attr = parseXml(line, closed); if (!attr["type"].empty()) - assert(attr["type"] == "PUnstructuredGrid"); + VTK_ASSERT(attr["type"] == "PUnstructuredGrid", "VtkReader supports PUnstructuredGrid types"); if (!attr["version"].empty()) - assert(std::stod(attr["version"]) == 1.0); + VTK_ASSERT(std::stod(attr["version"]) == 1.0, "File format must be 1.0"); if (!attr["byte_order"].empty()) - assert(attr["byte_order"] == "LittleEndian"); + VTK_ASSERT(attr["byte_order"] == "LittleEndian", "LittleEndian byte order supported"); if (!attr["header_type"].empty()) - assert(attr["header_type"] == "UInt64"); + VTK_ASSERT(attr["header_type"] == "UInt64", "Header integer type must be UInt64"); if (!attr["compressor"].empty()) - assert(attr["compressor"] == "vtkZLibDataCompressor"); // only ZLib compression supported + VTK_ASSERT(attr["compressor"] == "vtkZLibDataCompressor", "Only ZLib compression supported"); section = VTK_FILE; } else if (isSection(line, "/VTKFile", section, VTK_FILE)) @@ -265,7 +266,7 @@ void VtkReader<Grid,Creator>::readParallelFileFromStream (std::ifstream& input, bool closed = false; auto attr = parseXml(line, closed); - assert(attr.count("Source") > 0); + VTK_ASSERT(attr.count("Source") > , "No source files for partitions provided"); pieces_.push_back(attr["Source"]); } @@ -274,7 +275,7 @@ void VtkReader<Grid,Creator>::readParallelFileFromStream (std::ifstream& input, } if (section != NO_SECTION) - DUNE_THROW(IOError, "VTK-File is incomplete. It must end with </VTKFile>!"); + DUNE_THROW(Dune::VtkError, "VTK-File is incomplete. It must end with </VTKFile>!"); if (fillCreator) fillGridCreator();