Miscellaneous fixes
[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 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* tail);
85
86 void construct(const char* header, const char* extra,
87 const char* function);
88
89 static std::string format_extra(const char* condStr, const char* argDesc);
90
91 static char* s_header;
92
93 public:
94
95 IllegalArgumentException(const char* condStr, const char* argDesc,
96 const char* function, const char* tail) :
97 Exception() {
98 construct(s_header, format_extra(condStr, argDesc).c_str(), function, tail);
99 }
100
101 IllegalArgumentException(const char* condStr, const char* argDesc,
102 const char* function) :
103 Exception() {
104 construct(s_header, format_extra(condStr, argDesc).c_str(), function);
105 }
106
107 /**
108 * This is a convenience function for building usages that are variadic.
109 *
110 * Having IllegalArgumentException itself be variadic is problematic for
111 * making sure calls to IllegalArgumentException clean up memory.
112 */
113 static std::string formatVariadic();
114 static std::string formatVariadic(const char* format, ...);
115 };/* class IllegalArgumentException */
116
117 inline std::ostream& operator<<(std::ostream& os, const Exception& e) throw() CVC4_PUBLIC;
118 inline std::ostream& operator<<(std::ostream& os, const Exception& e) throw() {
119 e.toStream(os);
120 return os;
121 }
122
123 template <class T> inline void CheckArgument(bool cond, const T& arg,
124 const char* tail) CVC4_PUBLIC;
125 template <class T> inline void CheckArgument(bool cond, const T& arg,
126 const char* tail) {
127 if(__builtin_expect( ( !cond ), false )) { \
128 throw ::CVC4::IllegalArgumentException("", "", ""); \
129 } \
130 }
131 template <class T> inline void CheckArgument(bool cond, const T& arg)
132 CVC4_PUBLIC;
133 template <class T> inline void CheckArgument(bool cond, const T& arg) {
134 if(__builtin_expect( ( !cond ), false )) { \
135 throw ::CVC4::IllegalArgumentException("", "", ""); \
136 } \
137 }
138
139
140 }/* CVC4 namespace */
141
142 #endif /* __CVC4__EXCEPTION_H */