From 4a3e1a2fd95bc5a8a17d25b9f85f211851b168a2 Mon Sep 17 00:00:00 2001 From: Andreas Lauser Date: Thu, 8 Oct 2015 11:37:59 +0200 Subject: [PATCH] add the error macros from opm-core i.e., OPM_THROW(exception, message) and some excption classes --- CMakeLists_files.cmake | 2 ++ opm/common/ErrorMacros.hpp | 62 +++++++++++++++++++++++++++++++++++++ opm/common/Exceptions.hpp | 63 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+) create mode 100644 opm/common/ErrorMacros.hpp create mode 100644 opm/common/Exceptions.hpp diff --git a/CMakeLists_files.cmake b/CMakeLists_files.cmake index c67cc3329..b7f83b5fd 100644 --- a/CMakeLists_files.cmake +++ b/CMakeLists_files.cmake @@ -47,5 +47,7 @@ list (APPEND PROGRAM_SOURCE_FILES list( APPEND PUBLIC_HEADER_FILES + opm/common/ErrorMacros.hpp + opm/common/Exceptions.hpp opm/common/utility/platform_dependent/disable_warnings.h opm/common/utility/platform_dependent/reenable_warnings.h ) diff --git a/opm/common/ErrorMacros.hpp b/opm/common/ErrorMacros.hpp new file mode 100644 index 000000000..092a88bf3 --- /dev/null +++ b/opm/common/ErrorMacros.hpp @@ -0,0 +1,62 @@ +/* + Copyright 2013 Andreas Lauser + Copyright 2009, 2010 SINTEF ICT, Applied Mathematics. + Copyright 2009, 2010 Statoil ASA. + + This file is part of the Open Porous Media project (OPM). + + OPM is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OPM is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with OPM. If not, see . +*/ +#ifndef OPM_ERRORMACROS_HPP +#define OPM_ERRORMACROS_HPP + +#include +#include +#include +#include + +#include + +// macros for reporting to stderr +#ifdef OPM_VERBOSE // Verbose mode +# include +# define OPM_REPORT do { std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] " } while (false) +# define OPM_MESSAGE(x) do { OPM_REPORT; std::cerr << x << "\n"; } while (false) +# define OPM_MESSAGE_IF(cond, m) do {if(cond) OPM_MESSAGE(m);} while (false) +#else // non-verbose mode (default) +# define OPM_REPORT do {} while (false) +# define OPM_MESSAGE(x) do {} while (false) +# define OPM_MESSAGE_IF(cond, m) do {} while (false) +#endif + +// Macro to throw an exception. NOTE: For this macro to work, the +// exception class must exhibit a constructor with the signature +// (const std::string &message). Since this condition is not fulfilled +// for the std::exception, you should use this macro with some +// exception class derived from either std::logic_error or +// std::runtime_error. +// +// Usage: OPM_THROW(ExceptionClass, "Error message " << value); +#define OPM_THROW(Exception, message) \ + do { \ + std::ostringstream oss__; \ + oss__ << "[" << __FILE__ << ":" << __LINE__ << "] " << message; \ + OPM_MESSAGE(message); \ + throw Exception(oss__.str()); \ + } while (false) + +// throw an exception if a condition is true +#define OPM_ERROR_IF(condition, message) do {if(condition){ OPM_THROW(std::logic_error, message);}} while(false) + +#endif // OPM_ERRORMACROS_HPP diff --git a/opm/common/Exceptions.hpp b/opm/common/Exceptions.hpp new file mode 100644 index 000000000..bfe5ebf76 --- /dev/null +++ b/opm/common/Exceptions.hpp @@ -0,0 +1,63 @@ +// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- +// vi: set et ts=4 sw=4 sts=4: +/***************************************************************************** + * Copyright (C) 2013 by Andreas Lauser * + * * + * This program is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see . * + *****************************************************************************/ +/*! + * \file + * \brief Provides the OPM specific exception classes. + */ +#ifndef OPM_EXCEPTIONS_HPP +#define OPM_EXCEPTIONS_HPP + +#include + +// the OPM-specific exception classes +namespace Opm { +class NotImplemented : public std::logic_error +{ +public: + explicit NotImplemented(const std::string &message) + : std::logic_error(message) + {} +}; + +class NumericalProblem : public std::runtime_error +{ +public: + explicit NumericalProblem(const std::string &message) + : std::runtime_error(message) + {} +}; + +class MaterialLawProblem : public NumericalProblem +{ +public: + explicit MaterialLawProblem(const std::string &message) + : NumericalProblem(message) + {} +}; + +class LinearSolverProblem : public NumericalProblem +{ +public: + explicit LinearSolverProblem(const std::string &message) + : NumericalProblem(message) + {} +}; +} + +#endif // OPM_EXCEPTIONS_HPP