| 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 | <http://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 | #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 | #define QL_MULTILINE_FAILURE_END } while(false) |
| 70 | #endif |
| 71 | |
| 72 | |
| 73 | #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 | #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 | #define QL_FAIL(message) \ |
| 93 | QL_MULTILINE_FAILURE_BEGIN \ |
| 94 | std::ostringstream _ql_msg_stream; \ |
| 95 | _ql_msg_stream << message; \ |
| 96 | throw QuantLib::Error(__FILE__,__LINE__, \ |
| 97 | BOOST_CURRENT_FUNCTION,_ql_msg_stream.str()); \ |
| 98 | QL_MULTILINE_FAILURE_END |
| 99 | |
| 100 | |
| 101 | /*! \def QL_ASSERT |
| 102 | \brief throw an error if the given condition is not verified |
| 103 | */ |
| 104 | #define QL_ASSERT(condition,message) \ |
| 105 | QL_MULTILINE_ASSERTION_BEGIN \ |
| 106 | if (!(condition)) { \ |
| 107 | std::ostringstream _ql_msg_stream; \ |
| 108 | _ql_msg_stream << message; \ |
| 109 | throw QuantLib::Error(__FILE__,__LINE__, \ |
| 110 | BOOST_CURRENT_FUNCTION,_ql_msg_stream.str()); \ |
| 111 | } \ |
| 112 | QL_MULTILINE_ASSERTION_END |
| 113 | |
| 114 | /*! \def QL_REQUIRE |
| 115 | \brief throw an error if the given pre-condition is not verified |
| 116 | */ |
| 117 | #define QL_REQUIRE(condition,message) \ |
| 118 | QL_MULTILINE_ASSERTION_BEGIN \ |
| 119 | if (!(condition)) { \ |
| 120 | std::ostringstream _ql_msg_stream; \ |
| 121 | _ql_msg_stream << message; \ |
| 122 | throw QuantLib::Error(__FILE__,__LINE__, \ |
| 123 | BOOST_CURRENT_FUNCTION,_ql_msg_stream.str()); \ |
| 124 | } \ |
| 125 | QL_MULTILINE_ASSERTION_END |
| 126 | |
| 127 | /*! \def QL_ENSURE |
| 128 | \brief throw an error if the given post-condition is not verified |
| 129 | */ |
| 130 | #define QL_ENSURE(condition,message) \ |
| 131 | QL_MULTILINE_ASSERTION_BEGIN \ |
| 132 | if (!(condition)) { \ |
| 133 | std::ostringstream _ql_msg_stream; \ |
| 134 | _ql_msg_stream << message; \ |
| 135 | throw QuantLib::Error(__FILE__,__LINE__, \ |
| 136 | BOOST_CURRENT_FUNCTION,_ql_msg_stream.str()); \ |
| 137 | } \ |
| 138 | QL_MULTILINE_ASSERTION_END |
| 139 | |
| 140 | |
| 141 | #endif |
| 142 | |
| 143 | |