From d519913ca66da04832f609e629910201f622f7b8 Mon Sep 17 00:00:00 2001 From: Dmitri Naumov <dmitri.naumov@ufz.de> Date: Fri, 16 Jan 2015 12:47:44 +0100 Subject: [PATCH] [IO] Add is_safely_convertable(). This checks for possible overflows for two arbitrary types. --- FileIO/MPI_IO/NodePartitionedMeshReader.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/FileIO/MPI_IO/NodePartitionedMeshReader.cpp b/FileIO/MPI_IO/NodePartitionedMeshReader.cpp index 8bca8f1fcd4..f600fe580b1 100644 --- a/FileIO/MPI_IO/NodePartitionedMeshReader.cpp +++ b/FileIO/MPI_IO/NodePartitionedMeshReader.cpp @@ -28,6 +28,19 @@ #include "MeshLib/Elements/Tet.h" #include "MeshLib/Elements/Tri.h" +// Check if the value can by converted to given type without overflow. +template <typename VALUE, typename TYPE> +bool +is_safely_convertable(VALUE const& value) +{ + bool const result = value <= std::numeric_limits<TYPE>::max(); + if (!result) { + ERR("The value %d is too large for conversion.", value); + ERR("Maximum available size is %d.", std::numeric_limits<TYPE>::max()); + } + return result; +} + namespace FileIO { -- GitLab