/src/quantlib/ql/errors.hpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl |
5 | | Copyright (C) 2003, 2004, 2005 StatPro Italia srl |
6 | | |
7 | | This file is part of QuantLib, a free-software/open-source library |
8 | | for financial quantitative analysts and developers - http://quantlib.org/ |
9 | | |
10 | | QuantLib is free software: you can redistribute it and/or modify it |
11 | | under the terms of the QuantLib license. You should have received a |
12 | | copy of the license along with this program; if not, please email |
13 | | <quantlib-dev@lists.sf.net>. The license is also available online at |
14 | | <https://www.quantlib.org/license.shtml>. |
15 | | |
16 | | This program is distributed in the hope that it will be useful, but WITHOUT |
17 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
18 | | FOR A PARTICULAR PURPOSE. See the license for more details. |
19 | | */ |
20 | | |
21 | | /*! \file errors.hpp |
22 | | \brief Classes and functions for error handling. |
23 | | */ |
24 | | |
25 | | #ifndef quantlib_errors_hpp |
26 | | #define quantlib_errors_hpp |
27 | | |
28 | | #include <ql/qldefines.hpp> |
29 | | #include <ql/shared_ptr.hpp> |
30 | | #include <boost/assert.hpp> |
31 | | #include <boost/current_function.hpp> |
32 | | #include <exception> |
33 | | #include <sstream> |
34 | | #include <string> |
35 | | |
36 | | namespace QuantLib { |
37 | | |
38 | | //! Base error class |
39 | | class Error : public std::exception { |
40 | | public: |
41 | | /*! The explicit use of this constructor is not advised. |
42 | | Use the QL_FAIL macro instead. |
43 | | */ |
44 | | Error(const std::string& file, |
45 | | long line, |
46 | | const std::string& functionName, |
47 | | const std::string& message = ""); |
48 | | //! returns the error message. |
49 | | const char* what() const noexcept override; |
50 | | |
51 | | private: |
52 | | ext::shared_ptr<std::string> message_; |
53 | | }; |
54 | | |
55 | | } |
56 | | |
57 | 0 | #define QL_MULTILINE_FAILURE_BEGIN do { |
58 | | |
59 | | /* Disable warning C4127 (conditional expression is constant) when |
60 | | wrapping macros with the do { ... } while(false) construct on MSVC |
61 | | */ |
62 | | #if defined(BOOST_MSVC) |
63 | | #define QL_MULTILINE_FAILURE_END \ |
64 | | __pragma(warning(push)) \ |
65 | | __pragma(warning(disable:4127)) \ |
66 | | } while(false) \ |
67 | | __pragma(warning(pop)) |
68 | | #else |
69 | 0 | #define QL_MULTILINE_FAILURE_END } while(false) |
70 | | #endif |
71 | | |
72 | | |
73 | 666M | #define QL_MULTILINE_ASSERTION_BEGIN do { |
74 | | |
75 | | /* Disable warning C4127 (conditional expression is constant) when |
76 | | wrapping macros with the do { ... } while(false) construct on MSVC |
77 | | */ |
78 | | #if defined(BOOST_MSVC) |
79 | | #define QL_MULTILINE_ASSERTION_END \ |
80 | | __pragma(warning(push)) \ |
81 | | __pragma(warning(disable:4127)) \ |
82 | | } while(false) \ |
83 | | __pragma(warning(pop)) |
84 | | #else |
85 | 666M | #define QL_MULTILINE_ASSERTION_END } while(false) |
86 | | #endif |
87 | | |
88 | | |
89 | | /*! \def QL_FAIL |
90 | | \brief throw an error (possibly with file and line information) |
91 | | */ |
92 | 0 | #define QL_FAIL(message) \ |
93 | 0 | QL_MULTILINE_FAILURE_BEGIN \ |
94 | 0 | std::ostringstream _ql_msg_stream; \ |
95 | 0 | _ql_msg_stream << message; \ |
96 | 0 | throw QuantLib::Error(__FILE__,__LINE__, \ |
97 | 0 | BOOST_CURRENT_FUNCTION,_ql_msg_stream.str()); \ |
98 | 0 | QL_MULTILINE_FAILURE_END |
99 | | |
100 | | |
101 | | /*! \def QL_ASSERT |
102 | | \brief throw an error if the given condition is not verified |
103 | | */ |
104 | 0 | #define QL_ASSERT(condition,message) \ |
105 | 0 | QL_MULTILINE_ASSERTION_BEGIN \ |
106 | 0 | if (!(condition)) { \ |
107 | 0 | std::ostringstream _ql_msg_stream; \ |
108 | 0 | _ql_msg_stream << message; \ |
109 | 0 | throw QuantLib::Error(__FILE__,__LINE__, \ |
110 | 0 | BOOST_CURRENT_FUNCTION,_ql_msg_stream.str()); \ |
111 | 0 | } \ |
112 | 0 | QL_MULTILINE_ASSERTION_END |
113 | | |
114 | | /*! \def QL_REQUIRE |
115 | | \brief throw an error if the given pre-condition is not verified |
116 | | */ |
117 | 634M | #define QL_REQUIRE(condition,message) \ |
118 | 717M | QL_MULTILINE_ASSERTION_BEGIN \ |
119 | 717M | if (!(condition)) { \Unexecuted instantiation: vectorbsmprocessextractor.cpp:QuantLib::detail::VectorBsmProcessExtractor::getInterestRateDf(QuantLib::Date const&) const::$_1::operator()(double, double) const Unexecuted instantiation: singlefactorbsmbasketengine.cpp:QuantLib::detail::SumExponentialsRootSolver::getRoot(double, QuantLib::detail::SumExponentialsRootSolver::Strategy) const::$_0::operator()(double) const |
120 | 459 | std::ostringstream _ql_msg_stream; \ |
121 | 459 | _ql_msg_stream << message; \ |
122 | 459 | throw QuantLib::Error(__FILE__,__LINE__, \ |
123 | 459 | BOOST_CURRENT_FUNCTION,_ql_msg_stream.str()); \ |
124 | 634M | } \ |
125 | 634M | QL_MULTILINE_ASSERTION_END |
126 | | |
127 | | /*! \def QL_ENSURE |
128 | | \brief throw an error if the given post-condition is not verified |
129 | | */ |
130 | 31.3M | #define QL_ENSURE(condition,message) \ |
131 | 35.0M | QL_MULTILINE_ASSERTION_BEGIN \ |
132 | 54.9M | if (!(condition)) { \ |
133 | 0 | std::ostringstream _ql_msg_stream; \ |
134 | 0 | _ql_msg_stream << message; \ |
135 | 0 | throw QuantLib::Error(__FILE__,__LINE__, \ |
136 | 0 | BOOST_CURRENT_FUNCTION,_ql_msg_stream.str()); \ |
137 | 31.3M | } \ |
138 | 31.3M | QL_MULTILINE_ASSERTION_END |
139 | | |
140 | | |
141 | | #endif |
142 | | |