Adding listeners to Options.
[cvc5.git] / src / base / 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 <cstdarg>
23 #include <cstdlib>
24 #include <exception>
25 #include <iosfwd>
26 #include <sstream>
27 #include <stdexcept>
28 #include <string>
29
30 #include "base/tls.h"
31
32 namespace CVC4 {
33
34 class CVC4_PUBLIC Exception : public std::exception {
35 protected:
36 std::string d_msg;
37
38 public:
39 // Constructors
40 Exception() throw() : d_msg("Unknown exception") {}
41 Exception(const std::string& msg) throw() : d_msg(msg) {}
42 Exception(const char* msg) throw() : d_msg(msg) {}
43
44 // Destructor
45 virtual ~Exception() throw() {}
46
47 // NON-VIRTUAL METHOD for setting and printing the error message
48 void setMessage(const std::string& msg) throw() { d_msg = msg; }
49 std::string getMessage() const throw() { return d_msg; }
50
51 // overridden from base class std::exception
52 virtual const char* what() const throw() { return d_msg.c_str(); }
53
54 /**
55 * Get this exception as a string. Note that
56 * cout << ex.toString();
57 * is subtly different from
58 * cout << ex;
59 * which is equivalent to
60 * ex.toStream(cout);
61 * That is because with the latter two, the output language (and
62 * other preferences) for exprs on the stream is respected. In
63 * toString(), there is no stream, so the parameters are default
64 * and you'll get exprs and types printed using the AST language.
65 */
66 std::string toString() const throw() {
67 std::stringstream ss;
68 toStream(ss);
69 return ss.str();
70 }
71
72 /**
73 * Printing: feel free to redefine toStream(). When overridden in
74 * a derived class, it's recommended that this method print the
75 * type of exception before the actual message.
76 */
77 virtual void toStream(std::ostream& os) const throw() { os << d_msg; }
78
79 };/* class Exception */
80
81 class CVC4_PUBLIC IllegalArgumentException : public Exception {
82 protected:
83 IllegalArgumentException() : Exception() {}
84
85 void construct(const char* header, const char* extra,
86 const char* function, const char* tail);
87
88 void construct(const char* header, const char* extra,
89 const char* function);
90
91 static std::string format_extra(const char* condStr, const char* argDesc);
92
93 static const char* s_header;
94
95 public:
96
97 IllegalArgumentException(const char* condStr, const char* argDesc,
98 const char* function, const char* tail) :
99 Exception() {
100 construct(s_header, format_extra(condStr, argDesc).c_str(), function, tail);
101 }
102
103 IllegalArgumentException(const char* condStr, const char* argDesc,
104 const char* function) :
105 Exception() {
106 construct(s_header, format_extra(condStr, argDesc).c_str(), function);
107 }
108
109 /**
110 * This is a convenience function for building usages that are variadic.
111 *
112 * Having IllegalArgumentException itself be variadic is problematic for
113 * making sure calls to IllegalArgumentException clean up memory.
114 */
115 static std::string formatVariadic();
116 static std::string formatVariadic(const char* format, ...);
117 };/* class IllegalArgumentException */
118
119 inline std::ostream& operator<<(std::ostream& os, const Exception& e) throw() CVC4_PUBLIC;
120 inline std::ostream& operator<<(std::ostream& os, const Exception& e) throw() {
121 e.toStream(os);
122 return os;
123 }
124
125 template <class T> inline void CheckArgument(bool cond, const T& arg,
126 const char* tail) CVC4_PUBLIC;
127 template <class T> inline void CheckArgument(bool cond, const T& arg,
128 const char* tail) {
129 if(__builtin_expect( ( !cond ), false )) { \
130 throw ::CVC4::IllegalArgumentException("", "", ""); \
131 } \
132 }
133 template <class T> inline void CheckArgument(bool cond, const T& arg)
134 CVC4_PUBLIC;
135 template <class T> inline void CheckArgument(bool cond, const T& arg) {
136 if(__builtin_expect( ( !cond ), false )) { \
137 throw ::CVC4::IllegalArgumentException("", "", ""); \
138 } \
139 }
140
141 class CVC4_PUBLIC LastExceptionBuffer {
142 public:
143 LastExceptionBuffer();
144 ~LastExceptionBuffer();
145
146 void setContents(const char* string);
147 const char* getContents() const { return d_contents; }
148
149 static LastExceptionBuffer* getCurrent() { return s_currentBuffer; }
150 static void setCurrent(LastExceptionBuffer* buffer) { s_currentBuffer = buffer; }
151
152 static const char* currentContents() {
153 return (getCurrent() == NULL) ? NULL : getCurrent()->getContents();
154 }
155
156 private:
157 /* Disallow copies */
158 LastExceptionBuffer(const LastExceptionBuffer&) CVC4_UNDEFINED;
159 LastExceptionBuffer& operator=(const LastExceptionBuffer&) CVC4_UNDEFINED;
160
161 char* d_contents;
162
163 static CVC4_THREADLOCAL(LastExceptionBuffer*) s_currentBuffer;
164 }; /* class LastExceptionBuffer */
165
166 }/* CVC4 namespace */
167
168 #endif /* __CVC4__EXCEPTION_H */