My Project
ErrorMacros.hpp
1/*
2 Copyright 2013 Andreas Lauser
3 Copyright 2009, 2010 SINTEF ICT, Applied Mathematics.
4 Copyright 2009, 2010 Statoil ASA.
5
6 This file is part of the Open Porous Media project (OPM).
7
8 OPM is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 OPM is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with OPM. If not, see <http://www.gnu.org/licenses/>.
20*/
21#ifndef OPM_GRID_ERRORMACROS_HEADER_INCLUDED
22#define OPM_GRID_ERRORMACROS_HEADER_INCLUDED
23
24#if HAVE_OPM_COMMON
25#include <opm/common/ErrorMacros.hpp>
26#else
27
28// reimplementation of OPM macro when opm-common is not present
29
30#include <string>
31#include <sstream>
32#include <exception>
33#include <stdexcept>
34#include <cassert>
35
36// macros for reporting to stderr
37#ifdef OPM_VERBOSE // Verbose mode
38# include <iostream>
39# define OPM_REPORT do { std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] " } while (false)
40# define OPM_MESSAGE(x) do { OPM_REPORT; std::cerr << x << "\n"; } while (false)
41# define OPM_MESSAGE_IF(cond, m) do {if(cond) OPM_MESSAGE(m);} while (false)
42#else // non-verbose mode (default)
43# define OPM_REPORT do {} while (false)
44# define OPM_MESSAGE(x) do {} while (false)
45# define OPM_MESSAGE_IF(cond, m) do {} while (false)
46#endif
47
48// Macro to throw an exception. NOTE: For this macro to work, the
49// exception class must exhibit a constructor with the signature
50// (const std::string &message). Since this condition is not fulfilled
51// for the std::exception, you should use this macro with some
52// exception class derived from either std::logic_error or
53// std::runtime_error.
54//
55// Usage: OPM_THROW(ExceptionClass, "Error message " << value);
56#define OPM_THROW(Exception, message) \
57 do { \
58 std::ostringstream oss__; \
59 oss__ << "[" << __FILE__ << ":" << __LINE__ << "] " << message; \
60 throw Exception(oss__.str()); \
61 } while (false)
62
63// Same as OPM_THROW, except for not making an OpmLog::error() call.
64//
65// Usage: OPM_THROW_NOLOG(ExceptionClass, "Error message " << value);
66#define OPM_THROW_NOLOG(Exception, message) \
67 do { \
68 std::ostringstream oss__; \
69 oss__ << "[" << __FILE__ << ":" << __LINE__ << "] " << message; \
70 throw Exception(oss__.str()); \
71 } while (false)
72
73// throw an exception if a condition is true
74#define OPM_ERROR_IF(condition, message) do {if(condition){ OPM_THROW(std::logic_error, message);}} while(false)
75
76#endif // #if HAVE_OPM_COMMON
77
78#endif // OPM_ERRORMACROS_HPP