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         /*! the automatically generated destructor would
49             not have the throw specifier.
50         */
~Error()51         ~Error() throw() {}
52         //! returns the error message.
53         const char* what() const throw ();
54       private:
55         ext::shared_ptr<std::string> message_;
56     };
57 
58 }
59 
60 /* Fix C4127: conditional expression is constant when wrapping macros
61    with do { ... } while(false); on MSVC
62 */
63 #define MULTILINE_MACRO_BEGIN do {
64 
65 #if defined(BOOST_MSVC) && BOOST_MSVC >= 1500
66     /* __pragma is available from VC++9 */
67     #define MULTILINE_MACRO_END \
68         __pragma(warning(push)) \
69         __pragma(warning(disable:4127)) \
70         } while(false) \
71         __pragma(warning(pop))
72 #else
73     #define MULTILINE_MACRO_END } while(false)
74 #endif
75 
76 
77 /*! \def QL_FAIL
78     \brief throw an error (possibly with file and line information)
79 */
80 #define QL_FAIL(message) \
81 MULTILINE_MACRO_BEGIN \
82     std::ostringstream _ql_msg_stream; \
83     _ql_msg_stream << message; \
84     throw QuantLib::Error(__FILE__,__LINE__, \
85                           BOOST_CURRENT_FUNCTION,_ql_msg_stream.str()); \
86 MULTILINE_MACRO_END
87 
88 
89 /*! \def QL_ASSERT
90     \brief throw an error if the given condition is not verified
91 */
92 #define QL_ASSERT(condition,message) \
93 if (!(condition)) { \
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  } else
99 
100 
101 /*! \def QL_REQUIRE
102     \brief throw an error if the given pre-condition is not verified
103 */
104 #define QL_REQUIRE(condition,message) \
105 if (!(condition)) { \
106     std::ostringstream _ql_msg_stream; \
107     _ql_msg_stream << message; \
108     throw QuantLib::Error(__FILE__,__LINE__, \
109                           BOOST_CURRENT_FUNCTION,_ql_msg_stream.str()); \
110  } else
111 
112 
113 /*! \def QL_ENSURE
114     \brief throw an error if the given post-condition is not verified
115 */
116 #define QL_ENSURE(condition,message) \
117 if (!(condition)) { \
118     std::ostringstream _ql_msg_stream; \
119     _ql_msg_stream << message; \
120     throw QuantLib::Error(__FILE__,__LINE__, \
121                           BOOST_CURRENT_FUNCTION,_ql_msg_stream.str()); \
122  } else
123 
124 
125 #endif
126 
127