Commit b3d478f7 authored by Praetorius, Simon's avatar Praetorius, Simon
Browse files

Correct error handling

parent 4430d61b
...@@ -14,10 +14,20 @@ class VtkError : public Exception {}; ...@@ -14,10 +14,20 @@ class VtkError : public Exception {};
} }
/** /**
* \brief check if condition \a cond holds; otherwise, throw a VtkError. * \brief check if condition \a cond holds; otherwise, throw a VtkError with a message.
*/ */
#define VTK_ASSERT(cond, text) \ #define VTK_ASSERT_MSG(cond, text) \
do { \ do { \
if (!(cond)) \ if (!(cond)) \
DUNE_THROW(Dune::VtkError, text); \ DUNE_THROW(Dune::VtkError, text); \
} while (false) } while (false)
/**
* \brief check if condition \a cond holds; otherwise, throw a VtkError.
*/
#define VTK_ASSERT(cond) \
do { \
if (!(cond)) \
DUNE_THROW(Dune::VtkError, #cond); \
} while (false)
...@@ -24,7 +24,7 @@ void VtkReader<Grid,Creator>::read (std::string const& filename, bool fillCreato ...@@ -24,7 +24,7 @@ void VtkReader<Grid,Creator>::read (std::string const& filename, bool fillCreato
DUNE_THROW(IOError, "File " << filename << " does not exist!"); DUNE_THROW(IOError, "File " << filename << " does not exist!");
std::ifstream input(filename, std::ios_base::in | std::ios_base::binary); std::ifstream input(filename, std::ios_base::in | std::ios_base::binary);
assert(input.is_open()); VTK_ASSERT(input.is_open());
std::string ext = Vtk::Path(filename).extension().string(); std::string ext = Vtk::Path(filename).extension().string();
if (ext == ".vtu") { if (ext == ".vtu") {
...@@ -57,16 +57,16 @@ void VtkReader<Grid,Creator>::readSerialFileFromStream (std::ifstream& input, bo ...@@ -57,16 +57,16 @@ void VtkReader<Grid,Creator>::readSerialFileFromStream (std::ifstream& input, bo
auto attr = parseXml(line, closed); auto attr = parseXml(line, closed);
if (!attr["type"].empty()) if (!attr["type"].empty())
VTK_ASSERT(attr["type"] == "UnstructuredGrid", "VtkReader supports UnstructuredGrid types"); VTK_ASSERT_MSG(attr["type"] == "UnstructuredGrid", "VtkReader supports UnstructuredGrid types");
if (!attr["version"].empty()) if (!attr["version"].empty())
VTK_ASSERT(std::stod(attr["version"]) == 1.0, "File format must be 1.0"); VTK_ASSERT_MSG(std::stod(attr["version"]) == 1.0, "File format must be 1.0");
if (!attr["byte_order"].empty()) if (!attr["byte_order"].empty())
VTK_ASSERT(attr["byte_order"] == "LittleEndian", "LittleEndian byte order supported"); VTK_ASSERT_MSG(attr["byte_order"] == "LittleEndian", "LittleEndian byte order supported");
if (!attr["header_type"].empty()) if (!attr["header_type"].empty())
VTK_ASSERT(attr["header_type"] == "UInt64", "Header integer type must be UInt64"); VTK_ASSERT_MSG(attr["header_type"] == "UInt64", "Header integer type must be UInt64");
if (!attr["compressor"].empty()) { if (!attr["compressor"].empty()) {
compressor = attr["compressor"]; compressor = attr["compressor"];
VTK_ASSERT(compressor == "vtkZLibDataCompressor", "Only ZLib compression supported"); VTK_ASSERT_MSG(compressor == "vtkZLibDataCompressor", "Only ZLib compression supported");
} }
section = VTK_FILE; section = VTK_FILE;
} }
...@@ -80,7 +80,8 @@ void VtkReader<Grid,Creator>::readSerialFileFromStream (std::ifstream& input, bo ...@@ -80,7 +80,8 @@ void VtkReader<Grid,Creator>::readSerialFileFromStream (std::ifstream& input, bo
bool closed = false; bool closed = false;
auto attr = parseXml(line, closed); auto attr = parseXml(line, closed);
VTK_ASSERT(attr.count("NumberOfPoints") > 0 && attr.count("NumberOfCells") > 0, "Number of points or cells in file must be > 0"); VTK_ASSERT_MSG(attr.count("NumberOfPoints") > 0 && attr.count("NumberOfCells") > 0,
"Number of points or cells in file must be > 0");
numberOfPoints_ = std::stoul(attr["NumberOfPoints"]); numberOfPoints_ = std::stoul(attr["NumberOfPoints"]);
numberOfCells_ = std::stoul(attr["NumberOfCells"]); numberOfCells_ = std::stoul(attr["NumberOfCells"]);
section = PIECE; section = PIECE;
...@@ -130,7 +131,7 @@ void VtkReader<Grid,Creator>::readSerialFileFromStream (std::ifstream& input, bo ...@@ -130,7 +131,7 @@ void VtkReader<Grid,Creator>::readSerialFileFromStream (std::ifstream& input, bo
data_offset = 0; data_offset = 0;
if (!attr["offset"].empty()) { if (!attr["offset"].empty()) {
data_offset = std::stoul(attr["offset"]); data_offset = std::stoul(attr["offset"]);
VTK_ASSERT(data_format == "appended", "Attribute 'offset' only supported by appended mode"); VTK_ASSERT_MSG(data_format == "appended", "Attribute 'offset' only supported by appended mode");
} }
// Store attributes of DataArray // Store attributes of DataArray
...@@ -175,7 +176,7 @@ void VtkReader<Grid,Creator>::readSerialFileFromStream (std::ifstream& input, bo ...@@ -175,7 +176,7 @@ void VtkReader<Grid,Creator>::readSerialFileFromStream (std::ifstream& input, bo
bool closed = false; bool closed = false;
auto attr = parseXml(line, closed); auto attr = parseXml(line, closed);
if (!attr["encoding"].empty()) if (!attr["encoding"].empty())
VTK_ASSERT(attr["encoding"] == "raw", "base64 encoding not supported"); VTK_ASSERT_MSG(attr["encoding"] == "raw", "base64 encoding not supported");
offset0_ = findAppendedDataPosition(input); offset0_ = findAppendedDataPosition(input);
if (dataArray_["points"].type == Vtk::FLOAT32) if (dataArray_["points"].type == Vtk::FLOAT32)
...@@ -245,15 +246,15 @@ void VtkReader<Grid,Creator>::readParallelFileFromStream (std::ifstream& input, ...@@ -245,15 +246,15 @@ void VtkReader<Grid,Creator>::readParallelFileFromStream (std::ifstream& input,
auto attr = parseXml(line, closed); auto attr = parseXml(line, closed);
if (!attr["type"].empty()) if (!attr["type"].empty())
VTK_ASSERT(attr["type"] == "PUnstructuredGrid", "VtkReader supports PUnstructuredGrid types"); VTK_ASSERT_MSG(attr["type"] == "PUnstructuredGrid", "VtkReader supports PUnstructuredGrid types");
if (!attr["version"].empty()) if (!attr["version"].empty())
VTK_ASSERT(std::stod(attr["version"]) == 1.0, "File format must be 1.0"); VTK_ASSERT_MSG(std::stod(attr["version"]) == 1.0, "File format must be 1.0");
if (!attr["byte_order"].empty()) if (!attr["byte_order"].empty())
VTK_ASSERT(attr["byte_order"] == "LittleEndian", "LittleEndian byte order supported"); VTK_ASSERT_MSG(attr["byte_order"] == "LittleEndian", "LittleEndian byte order supported");
if (!attr["header_type"].empty()) if (!attr["header_type"].empty())
VTK_ASSERT(attr["header_type"] == "UInt64", "Header integer type must be UInt64"); VTK_ASSERT_MSG(attr["header_type"] == "UInt64", "Header integer type must be UInt64");
if (!attr["compressor"].empty()) if (!attr["compressor"].empty())
VTK_ASSERT(attr["compressor"] == "vtkZLibDataCompressor", "Only ZLib compression supported"); VTK_ASSERT_MSG(attr["compressor"] == "vtkZLibDataCompressor", "Only ZLib compression supported");
section = VTK_FILE; section = VTK_FILE;
} }
else if (isSection(line, "/VTKFile", section, VTK_FILE)) else if (isSection(line, "/VTKFile", section, VTK_FILE))
...@@ -266,7 +267,7 @@ void VtkReader<Grid,Creator>::readParallelFileFromStream (std::ifstream& input, ...@@ -266,7 +267,7 @@ void VtkReader<Grid,Creator>::readParallelFileFromStream (std::ifstream& input,
bool closed = false; bool closed = false;
auto attr = parseXml(line, closed); auto attr = parseXml(line, closed);
VTK_ASSERT(attr.count("Source") > , "No source files for partitions provided"); VTK_ASSERT_MSG(attr.count("Source") > 0, "No source files for partitions provided");
pieces_.push_back(attr["Source"]); pieces_.push_back(attr["Source"]);
} }
...@@ -334,8 +335,8 @@ typename VtkReader<Grid,Creator>::Sections ...@@ -334,8 +335,8 @@ typename VtkReader<Grid,Creator>::Sections
VtkReader<Grid,Creator>::readPoints (std::ifstream& input, std::string name) VtkReader<Grid,Creator>::readPoints (std::ifstream& input, std::string name)
{ {
using T = typename GlobalCoordinate::value_type; using T = typename GlobalCoordinate::value_type;
assert(numberOfPoints_ > 0); VTK_ASSERT(numberOfPoints_ > 0);
assert(dataArray_["points"].components == 3u); VTK_ASSERT(dataArray_["points"].components == 3u);
Sections sec; Sections sec;
...@@ -343,8 +344,8 @@ VtkReader<Grid,Creator>::readPoints (std::ifstream& input, std::string name) ...@@ -343,8 +344,8 @@ VtkReader<Grid,Creator>::readPoints (std::ifstream& input, std::string name)
sec = readDataArray(input, point_values, 3*numberOfPoints_, POINTS_DATA_ARRAY, POINTS); sec = readDataArray(input, point_values, 3*numberOfPoints_, POINTS_DATA_ARRAY, POINTS);
if (sec != POINTS) if (sec != POINTS)
sec = skipRestOfDataArray(input, POINTS_DATA_ARRAY, POINTS); sec = skipRestOfDataArray(input, POINTS_DATA_ARRAY, POINTS);
assert(sec == POINTS); VTK_ASSERT(sec == POINTS);
assert(point_values.size() == 3*numberOfPoints_); VTK_ASSERT(point_values.size() == 3*numberOfPoints_);
// extract points from continuous values // extract points from continuous values
GlobalCoordinate p; GlobalCoordinate p;
...@@ -365,11 +366,11 @@ template <class Grid, class Creator> ...@@ -365,11 +366,11 @@ template <class Grid, class Creator>
template <class T> template <class T>
void VtkReader<Grid,Creator>::readPointsAppended (std::ifstream& input) void VtkReader<Grid,Creator>::readPointsAppended (std::ifstream& input)
{ {
assert(numberOfPoints_ > 0); VTK_ASSERT(numberOfPoints_ > 0);
assert(dataArray_["points"].components == 3u); VTK_ASSERT(dataArray_["points"].components == 3u);
std::vector<T> point_values; std::vector<T> point_values;
readAppended(input, point_values, dataArray_["points"].offset); readAppended(input, point_values, dataArray_["points"].offset);
assert(point_values.size() == 3*numberOfPoints_); VTK_ASSERT(point_values.size() == 3*numberOfPoints_);
// extract points from continuous values // extract points from continuous values
GlobalCoordinate p; GlobalCoordinate p;
...@@ -390,18 +391,18 @@ VtkReader<Grid,Creator>::readCells (std::ifstream& input, std::string name) ...@@ -390,18 +391,18 @@ VtkReader<Grid,Creator>::readCells (std::ifstream& input, std::string name)
{ {
Sections sec = CELLS_DATA_ARRAY; Sections sec = CELLS_DATA_ARRAY;
assert(numberOfCells_ > 0); VTK_ASSERT(numberOfCells_ > 0);
if (name == "types") { if (name == "types") {
sec = readDataArray(input, vec_types, numberOfCells_, CELLS_DATA_ARRAY, CELLS); sec = readDataArray(input, vec_types, numberOfCells_, CELLS_DATA_ARRAY, CELLS);
assert(vec_types.size() == numberOfCells_); VTK_ASSERT(vec_types.size() == numberOfCells_);
} else if (name == "offsets") { } else if (name == "offsets") {
sec = readDataArray(input, vec_offsets, numberOfCells_, CELLS_DATA_ARRAY, CELLS); sec = readDataArray(input, vec_offsets, numberOfCells_, CELLS_DATA_ARRAY, CELLS);
assert(vec_offsets.size() == numberOfCells_); VTK_ASSERT(vec_offsets.size() == numberOfCells_);
} else if (name == "connectivity") { } else if (name == "connectivity") {
sec = readDataArray(input, vec_connectivity, std::size_t(-1), CELLS_DATA_ARRAY, CELLS); sec = readDataArray(input, vec_connectivity, std::size_t(-1), CELLS_DATA_ARRAY, CELLS);
} else if (name == "global_point_ids") { } else if (name == "global_point_ids") {
sec = readDataArray(input, vec_point_ids, numberOfPoints_, CELLS_DATA_ARRAY, CELLS); sec = readDataArray(input, vec_point_ids, numberOfPoints_, CELLS_DATA_ARRAY, CELLS);
assert(vec_point_ids.size() == numberOfPoints_); VTK_ASSERT(vec_point_ids.size() == numberOfPoints_);
} }
return sec; return sec;
...@@ -411,14 +412,14 @@ VtkReader<Grid,Creator>::readCells (std::ifstream& input, std::string name) ...@@ -411,14 +412,14 @@ VtkReader<Grid,Creator>::readCells (std::ifstream& input, std::string name)
template <class Grid, class Creator> template <class Grid, class Creator>
void VtkReader<Grid,Creator>::readCellsAppended (std::ifstream& input) void VtkReader<Grid,Creator>::readCellsAppended (std::ifstream& input)
{ {
assert(numberOfCells_ > 0); VTK_ASSERT(numberOfCells_ > 0);
auto types_data = dataArray_["types"]; auto types_data = dataArray_["types"];
auto offsets_data = dataArray_["offsets"]; auto offsets_data = dataArray_["offsets"];
auto connectivity_data = dataArray_["connectivity"]; auto connectivity_data = dataArray_["connectivity"];
assert(types_data.type == Vtk::UINT8); VTK_ASSERT(types_data.type == Vtk::UINT8);
readAppended(input, vec_types, types_data.offset); readAppended(input, vec_types, types_data.offset);
assert(vec_types.size() == numberOfCells_); VTK_ASSERT(vec_types.size() == numberOfCells_);
if (offsets_data.type == Vtk::INT64) if (offsets_data.type == Vtk::INT64)
readAppended(input, vec_offsets, offsets_data.offset); readAppended(input, vec_offsets, offsets_data.offset);
...@@ -429,7 +430,7 @@ void VtkReader<Grid,Creator>::readCellsAppended (std::ifstream& input) ...@@ -429,7 +430,7 @@ void VtkReader<Grid,Creator>::readCellsAppended (std::ifstream& input)
std::copy(offsets.begin(), offsets.end(), vec_offsets.begin()); std::copy(offsets.begin(), offsets.end(), vec_offsets.begin());
} }
else { DUNE_THROW(Dune::NotImplemented, "Unsupported DataType in Cell offsets."); } else { DUNE_THROW(Dune::NotImplemented, "Unsupported DataType in Cell offsets."); }
assert(vec_offsets.size() == numberOfCells_); VTK_ASSERT(vec_offsets.size() == numberOfCells_);
if (connectivity_data.type == Vtk::INT64) if (connectivity_data.type == Vtk::INT64)
readAppended(input, vec_connectivity, connectivity_data.offset); readAppended(input, vec_connectivity, connectivity_data.offset);
...@@ -440,13 +441,13 @@ void VtkReader<Grid,Creator>::readCellsAppended (std::ifstream& input) ...@@ -440,13 +441,13 @@ void VtkReader<Grid,Creator>::readCellsAppended (std::ifstream& input)
std::copy(connectivity.begin(), connectivity.end(), vec_connectivity.begin()); std::copy(connectivity.begin(), connectivity.end(), vec_connectivity.begin());
} }
else { DUNE_THROW(Dune::NotImplemented, "Unsupported DataType in Cell connectivity."); } else { DUNE_THROW(Dune::NotImplemented, "Unsupported DataType in Cell connectivity."); }
assert(vec_connectivity.size() == std::size_t(vec_offsets.back())); VTK_ASSERT(vec_connectivity.size() == std::size_t(vec_offsets.back()));
if (dataArray_.count("global_point_ids") > 0) { if (dataArray_.count("global_point_ids") > 0) {
auto point_id_data = dataArray_["global_point_ids"]; auto point_id_data = dataArray_["global_point_ids"];
assert(point_id_data.type == Vtk::UINT64); VTK_ASSERT(point_id_data.type == Vtk::UINT64);
readAppended(input, vec_point_ids, point_id_data.offset); readAppended(input, vec_point_ids, point_id_data.offset);
assert(vec_point_ids.size() == numberOfPoints_); VTK_ASSERT(vec_point_ids.size() == numberOfPoints_);
} }
} }
...@@ -471,13 +472,13 @@ void read_compressed (T* buffer, unsigned char* buffer_in, ...@@ -471,13 +472,13 @@ void read_compressed (T* buffer, unsigned char* buffer_in,
Bytef* uncompressed_buffer = reinterpret_cast<Bytef*>(buffer); Bytef* uncompressed_buffer = reinterpret_cast<Bytef*>(buffer);
input.read((char*)(compressed_buffer), compressed_space); input.read((char*)(compressed_buffer), compressed_space);
assert(uLongf(input.gcount()) == compressed_space); VTK_ASSERT(uLongf(input.gcount()) == compressed_space);
if (uncompress(uncompressed_buffer, &uncompressed_space, compressed_buffer, compressed_space) != Z_OK) { if (uncompress(uncompressed_buffer, &uncompressed_space, compressed_buffer, compressed_space) != Z_OK) {
std::cerr << "Zlib error while uncompressing data.\n"; std::cerr << "Zlib error while uncompressing data.\n";
std::abort(); std::abort();
} }
assert(uLongf(bs) == uncompressed_space); VTK_ASSERT(uLongf(bs) == uncompressed_space);
#else #else
std::cerr << "Can not call read_compressed without compression enabled!\n"; std::cerr << "Can not call read_compressed without compression enabled!\n";
std::abort(); std::abort();
...@@ -505,7 +506,7 @@ void VtkReader<Grid,Creator>::readAppended (std::ifstream& input, std::vector<T> ...@@ -505,7 +506,7 @@ void VtkReader<Grid,Creator>::readAppended (std::ifstream& input, std::vector<T>
input.read((char*)&block_size, sizeof(std::uint64_t)); input.read((char*)&block_size, sizeof(std::uint64_t));
input.read((char*)&last_block_size, sizeof(std::uint64_t)); input.read((char*)&last_block_size, sizeof(std::uint64_t));
assert(block_size % sizeof(T) == 0); VTK_ASSERT(block_size % sizeof(T) == 0);
// total size of the uncompressed data // total size of the uncompressed data
size = block_size * (num_blocks-1) + last_block_size; size = block_size * (num_blocks-1) + last_block_size;
...@@ -516,7 +517,7 @@ void VtkReader<Grid,Creator>::readAppended (std::ifstream& input, std::vector<T> ...@@ -516,7 +517,7 @@ void VtkReader<Grid,Creator>::readAppended (std::ifstream& input, std::vector<T>
} else { } else {
input.read((char*)&size, sizeof(std::uint64_t)); input.read((char*)&size, sizeof(std::uint64_t));
} }
assert(size > 0 && (size % sizeof(T)) == 0); VTK_ASSERT(size > 0 && (size % sizeof(T)) == 0);
values.resize(size / sizeof(T)); values.resize(size / sizeof(T));
if (format_ == Vtk::COMPRESSED) { if (format_ == Vtk::COMPRESSED) {
...@@ -532,7 +533,7 @@ void VtkReader<Grid,Creator>::readAppended (std::ifstream& input, std::vector<T> ...@@ -532,7 +533,7 @@ void VtkReader<Grid,Creator>::readAppended (std::ifstream& input, std::vector<T>
} }
} else { } else {
input.read((char*)(values.data()), size); input.read((char*)(values.data()), size);
assert(input.gcount() == std::streamsize(size)); VTK_ASSERT(input.gcount() == std::streamsize(size));
} }
} }
...@@ -540,9 +541,9 @@ void VtkReader<Grid,Creator>::readAppended (std::ifstream& input, std::vector<T> ...@@ -540,9 +541,9 @@ void VtkReader<Grid,Creator>::readAppended (std::ifstream& input, std::vector<T>
template <class Grid, class Creator> template <class Grid, class Creator>
void VtkReader<Grid,Creator>::fillGridCreator (bool insertPieces) void VtkReader<Grid,Creator>::fillGridCreator (bool insertPieces)
{ {
assert(vec_points.size() == numberOfPoints_); VTK_ASSERT(vec_points.size() == numberOfPoints_);
assert(vec_types.size() == numberOfCells_); VTK_ASSERT(vec_types.size() == numberOfCells_);
assert(vec_offsets.size() == numberOfCells_); VTK_ASSERT(vec_offsets.size() == numberOfCells_);
if (!vec_points.empty()) if (!vec_points.empty())
creator_->insertVertices(vec_points, vec_point_ids); creator_->insertVertices(vec_points, vec_point_ids);
...@@ -598,7 +599,7 @@ std::map<std::string, std::string> VtkReader<Grid,Creator>::parseXml (std::strin ...@@ -598,7 +599,7 @@ std::map<std::string, std::string> VtkReader<Grid,Creator>::parseXml (std::strin
case XML_NAME_ASSIGN: case XML_NAME_ASSIGN:
value.clear(); value.clear();
escape = false; escape = false;
assert( c == '"' && "Format error!" ); VTK_ASSERT( c == '"' && "Format error!" );
sec = XML_VALUE; sec = XML_VALUE;
break; break;
case XML_VALUE: case XML_VALUE:
...@@ -613,7 +614,7 @@ std::map<std::string, std::string> VtkReader<Grid,Creator>::parseXml (std::strin ...@@ -613,7 +614,7 @@ std::map<std::string, std::string> VtkReader<Grid,Creator>::parseXml (std::strin
} }
break; break;
default: default:
assert(false && "Format error!"); VTK_ASSERT(false && "Format error!");
} }
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment