Merge branch '1.4.x'
[cvc5.git] / src / util / exception.h
1 /********************* */
2 /*! \file exception.h
3 ** \verbatim
4 ** Original author: Morgan Deters
5 ** Major contributors: none
6 ** Minor contributors (to current version): none
7 ** This file is part of the CVC4 project.
8 ** Copyright (c) 2009-2014 New York University and The University of Iowa
9 ** See the file COPYING in the top-level source directory for licensing
10 ** information.\endverbatim
11 **
12 ** \brief CVC4's exception base class and some associated utilities
13 **
14 ** CVC4's exception base class and some associated utilities.
15 **/
16
17 #include "cvc4_public.h"
18
19 #ifndef __CVC4__EXCEPTION_H
20 #define __CVC4__EXCEPTION_H
21
22 #include <iostream>
23 #include <string>
24 #include <sstream>
25 #include <stdexcept>
26 #include <exception>
27 #include <cstdlib>
28 #include <cstdarg>
29
30 namespace CVC4 {
31
32 class CVC4_PUBLIC Exception : public std::exception {
33 protected:
34 std::string d_msg;
35
36 public:
37 // Constructors
38 Exception() throw() : d_msg("Unknown exception") {}
39 Exception(const std::string& msg) throw() : d_msg(msg) {}
40 Exception(const char* msg) throw() : d_msg(msg) {}
41
42 // Destructor
43 virtual ~Exception() throw() {}
44
45 // NON-VIRTUAL METHOD for setting and printing the error message
46 void setMessage(const std::string& msg) throw() { d_msg = msg; }
47 std::string getMessage() const throw() { return d_msg; }
48
49 // overridden from base class std::exception
50 virtual const char* what() const throw() { return d_msg.c_str(); }
51
52 /**
53 * Get this exception as a string. Note that
54 * cout << ex.toString();
55 * is subtly different from
56 * cout << ex;
57 * which is equivalent to
58 * ex.toStream(cout);
59 * That is because with the latter two, the output language (and
60 * other preferences) for exprs on the stream is respected. In
61 * toString(), there is no stream, so the parameters are default
62 * and you'll get exprs and types printed using the AST language.
63 */
64 std::string toString() const throw() {
65 std::stringstream ss;
66 toStream(ss);
67 return ss.str();
68 }
69
70 /**
71 * Printing: feel free to redefine toStream(). When overridden in
72 * a derived class, it's recommended that this method print the
73 * type of exception before the actual message.
74 */
75 virtual void toStream(std::ostream& os) const throw() { os << d_msg; }
76
77 };/* class Exception */
78
79 class CVC4_PUBLIC IllegalArgumentException : public Exception {
80 protected:
81 IllegalArgumentException() : Exception() {}
82
83 void construct(const char* header, const char* extra,
84 const char* function, const char* fmt, ...) {
85 va_list args;
86 va_start(args, fmt);
87 construct(header, extra, function, fmt, args);
88 va_end(args);
89 }
90
91 void construct(const char* header, const char* extra,
92 const char* function, const char* fmt, va_list args);
93
94 void construct(const char* header, const char* extra,
95 const char* function);
96
97 public:
98 IllegalArgumentException(const char* condStr, const char* argDesc,
99 const char* function, const char* fmt, ...) :
100 Exception() {
101 va_list args;
102 va_start(args, fmt);
103 construct("Illegal argument detected",
104 ( std::string("`") + argDesc + "' is a bad argument"
105 + (*condStr == '\0' ? std::string() :
106 ( std::string("; expected ") +
107 condStr + " to hold" )) ).c_str(),
108 function, fmt, args);
109 va_end(args);
110 }
111
112 IllegalArgumentException(const char* condStr, const char* argDesc,
113 const char* function) :
114 Exception() {
115 construct("Illegal argument detected",
116 ( std::string("`") + argDesc + "' is a bad argument"
117 + (*condStr == '\0' ? std::string() :
118 ( std::string("; expected ") +
119 condStr + " to hold" )) ).c_str(),
120 function);
121 }
122 };/* class IllegalArgumentException */
123
124 inline std::ostream& operator<<(std::ostream& os, const Exception& e) throw() CVC4_PUBLIC;
125 inline std::ostream& operator<<(std::ostream& os, const Exception& e) throw() {
126 e.toStream(os);
127 return os;
128 }
129
130 }/* CVC4 namespace */
131
132 #if (defined(__BUILDING_CVC4LIB) || defined(__BUILDING_CVC4LIB_UNIT_TEST)) && !defined(__BUILDING_STATISTICS_FOR_EXPORT)
133 # include "util/cvc4_assert.h"
134 #endif /* (__BUILDING_CVC4LIB || __BUILDING_CVC4LIB_UNIT_TEST) && !__BUILDING_STATISTICS_FOR_EXPORT */
135
136 namespace CVC4 {
137
138 #ifndef CheckArgument
139 template <class T> inline void CheckArgument(bool cond, const T& arg, const char* fmt, ...) CVC4_PUBLIC;
140 template <class T> inline void CheckArgument(bool cond, const T& arg, const char* fmt, ...) {
141 if(__builtin_expect( ( !cond ), false )) { \
142 throw ::CVC4::IllegalArgumentException("", "", ""); \
143 } \
144 }
145 template <class T> inline void CheckArgument(bool cond, const T& arg) CVC4_PUBLIC;
146 template <class T> inline void CheckArgument(bool cond, const T& arg) {
147 if(__builtin_expect( ( !cond ), false )) { \
148 throw ::CVC4::IllegalArgumentException("", "", ""); \
149 } \
150 }
151 #endif /* CheckArgument */
152
153 #ifndef DebugCheckArgument
154 template <class T> inline void DebugCheckArgument(bool cond, const T& arg, const char* fmt, ...) CVC4_PUBLIC;
155 template <class T> inline void DebugCheckArgument(bool cond, const T& arg, const char* fmt, ...) {
156 if(__builtin_expect( ( !cond ), false )) { \
157 throw ::CVC4::IllegalArgumentException("", "", ""); \
158 } \
159 }
160 template <class T> inline void DebugCheckArgument(bool cond, const T& arg) CVC4_PUBLIC;
161 template <class T> inline void DebugCheckArgument(bool cond, const T& arg) {
162 if(__builtin_expect( ( !cond ), false )) { \
163 throw ::CVC4::IllegalArgumentException("", "", ""); \
164 } \
165 }
166 #endif /* DebugCheckArgument */
167
168 }/* CVC4 namespace */
169
170 #endif /* __CVC4__EXCEPTION_H */