Fixed bug (found by cax): when building MatLab MEX files, GMP libraries were not...
[libdai.git] / include / dai / exceptions.h
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4 *
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6 */
7
8
9 /// \file
10 /// \brief Defines the Exception class and macros for throwing exceptions and doing assertions
11
12
13 #ifndef __defined_libdai_exceptions_h
14 #define __defined_libdai_exceptions_h
15
16
17 #include <exception>
18 #include <stdexcept>
19 #include <string>
20 #include <iostream>
21
22
23 /// Used by DAI_THROW
24 #define DAI_QUOTE(x) #x
25
26 /// Used by DAI_THROW
27 #define DAI_TOSTRING(x) DAI_QUOTE(x)
28
29 /// Macro that simplifies throwing an exception with a useful default error message.
30 /** The error message consists of a description of the exception, the source
31 * code file and line number where the exception has been thrown.
32 * \param cod Corresponds to one of the enum values of dai::Exception::Code
33 *
34 * \par Example:
35 * \code
36 * DAI_THROW(NOT_IMPLEMENTED);
37 * \endcode
38 */
39 #define DAI_THROW(cod) throw dai::Exception(dai::Exception::cod, __FILE__, __PRETTY_FUNCTION__, DAI_TOSTRING(__LINE__), "")
40
41 /// Macro that simplifies throwing an exception with a user-defined error message.
42 /** \param cod Corresponds to one of the enum values of dai::Exception::Code
43 * \param msg Detailed error message that will be written to std::cerr.
44 *
45 * \par Example:
46 * \code
47 * DAI_THROWE(NOT_IMPLEMENTED,"Detailed error message");
48 * \endcode
49 */
50 #define DAI_THROWE(cod,msg) throw dai::Exception(dai::Exception::cod, __FILE__, __PRETTY_FUNCTION__, DAI_TOSTRING(__LINE__), msg)
51
52 /// Assertion mechanism, similar to the standard assert() macro. It is always active, even if NDEBUG is defined
53 #define DAI_ASSERT(condition) ((condition) ? ((void)0) : DAI_THROWE(ASSERTION_FAILED, std::string("Assertion \"" #condition "\" failed")))
54
55 // Assertion only if DAI_DEBUG is defined
56 #ifdef DAI_DEBUG
57 /// Assertion mechanism similar to DAI_ASSERT which is only active if DAI_DEBUG is defined
58 #define DAI_DEBASSERT(x) do {DAI_ASSERT(x);} while(0)
59 #else
60 #define DAI_DEBASSERT(x) do {} while(0)
61 #endif
62
63
64 namespace dai {
65
66
67 /// Error handling in libDAI is done by throwing an instance of the Exception class.
68 /** The Exception class inherits from std::runtime_error. It defines several types of exceptions
69 * and corresponding error messages. The recommended way to throw an instance of the Exception
70 * class is by using the #DAI_THROW or #DAI_THROWE macros.
71 */
72 class Exception : public std::runtime_error {
73 public:
74 /// Enumeration of exceptions used in libDAI
75 enum Code {NOT_IMPLEMENTED,
76 ASSERTION_FAILED,
77 IMPOSSIBLE_TYPECAST,
78 OBJECT_NOT_FOUND,
79 BELIEF_NOT_AVAILABLE,
80 UNKNOWN_ENUM_VALUE,
81 UNKNOWN_DAI_ALGORITHM,
82 UNKNOWN_PARAMETER_ESTIMATION_METHOD,
83 UNKNOWN_PROPERTY_TYPE,
84 UNKNOWN_PROPERTY,
85 MALFORMED_PROPERTY,
86 NOT_ALL_PROPERTIES_SPECIFIED,
87 INVALID_ALIAS,
88 CANNOT_READ_FILE,
89 CANNOT_WRITE_FILE,
90 INVALID_FACTORGRAPH_FILE,
91 INVALID_EVIDENCE_FILE,
92 INVALID_EMALG_FILE,
93 NOT_NORMALIZABLE,
94 MULTIPLE_UNDO,
95 FACTORGRAPH_NOT_CONNECTED,
96 INTERNAL_ERROR,
97 RUNTIME_ERROR,
98 OUT_OF_MEMORY,
99 NUM_ERRORS}; // NUM_ERRORS should be the last entry
100
101 /// Constructor
102 Exception( Code code, const char *filename, const char *function, const char *line, const std::string& detailedMsg ) :
103 std::runtime_error(ErrorStrings[code] + (detailedMsg.empty() ? "" : (": " + detailedMsg)) + " [File " + filename + ", line " + line + ", function: " + function + "]"),
104 _errorcode(code), _detailedMsg(detailedMsg), _filename(filename), _function(function), _line(line) {}
105
106 /// Destructor
107 ~Exception() throw () {}
108
109 /// Returns error code of this exception
110 Code getCode() const { return _errorcode; }
111
112 /// Returns error code of this exception
113 /** \deprecated Please use dai::Exceptions::getCode() instead
114 */
115 Code code() const { return getCode(); }
116
117 /// Returns short error message of this exception
118 const std::string& getMsg() const { return ErrorStrings[_errorcode]; }
119
120 /// Returns detailed error message of this exception
121 const std::string& getDetailedMsg() const { return _detailedMsg; }
122
123 /// Returns filename where this exception was thrown
124 const std::string& getFilename() const { return _filename; }
125
126 /// Returns function name in which this exception was thrown
127 const std::string& getFunction() const { return _function; }
128
129 /// Returns line number where this exception was thrown
130 const std::string& getLine() const { return _line; }
131
132 /// Returns error message corresponding to an error code
133 const std::string& message( const Code c ) const { return ErrorStrings[c]; }
134
135 private:
136 /// Contains the error code of this exception
137 Code _errorcode;
138
139 /// Contains the detailed message of this exception, if any
140 std::string _detailedMsg;
141
142 /// Contains the filename where this exception was thrown
143 std::string _filename;
144
145 /// Contains the function name in which this exception was thrown
146 std::string _function;
147
148 /// Contains the line number where this exception was thrown
149 std::string _line;
150
151 /// Error messages corresponding to the exceptions enumerated above
152 static std::string ErrorStrings[NUM_ERRORS];
153 };
154
155
156 }
157
158
159 #endif