1 /********************* */
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
12 ** \brief CVC4's exception base class and some associated utilities
14 ** CVC4's exception base class and some associated utilities.
17 #include "cvc4_public.h"
19 #ifndef __CVC4__EXCEPTION_H
20 #define __CVC4__EXCEPTION_H
34 class CVC4_PUBLIC Exception
: public std::exception
{
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
) {}
45 virtual ~Exception() throw() {}
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
; }
51 // overridden from base class std::exception
52 virtual const char* what() const throw() { return d_msg
.c_str(); }
55 * Get this exception as a string. Note that
56 * cout << ex.toString();
57 * is subtly different from
59 * which is equivalent to
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.
66 std::string
toString() const throw() {
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.
77 virtual void toStream(std::ostream
& os
) const throw() { os
<< d_msg
; }
79 };/* class Exception */
81 class CVC4_PUBLIC IllegalArgumentException
: public Exception
{
83 IllegalArgumentException() : Exception() {}
85 void construct(const char* header
, const char* extra
,
86 const char* function
, const char* tail
);
88 void construct(const char* header
, const char* extra
,
89 const char* function
);
91 static std::string
format_extra(const char* condStr
, const char* argDesc
);
93 static const char* s_header
;
97 IllegalArgumentException(const char* condStr
, const char* argDesc
,
98 const char* function
, const char* tail
) :
100 construct(s_header
, format_extra(condStr
, argDesc
).c_str(), function
, tail
);
103 IllegalArgumentException(const char* condStr
, const char* argDesc
,
104 const char* function
) :
106 construct(s_header
, format_extra(condStr
, argDesc
).c_str(), function
);
110 * This is a convenience function for building usages that are variadic.
112 * Having IllegalArgumentException itself be variadic is problematic for
113 * making sure calls to IllegalArgumentException clean up memory.
115 static std::string
formatVariadic();
116 static std::string
formatVariadic(const char* format
, ...);
117 };/* class IllegalArgumentException */
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() {
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
,
129 if(__builtin_expect( ( !cond
), false )) { \
130 throw ::CVC4::IllegalArgumentException("", "", ""); \
133 template <class T
> inline void CheckArgument(bool cond
, const T
& arg
)
135 template <class T
> inline void CheckArgument(bool cond
, const T
& arg
) {
136 if(__builtin_expect( ( !cond
), false )) { \
137 throw ::CVC4::IllegalArgumentException("", "", ""); \
141 class CVC4_PUBLIC LastExceptionBuffer
{
143 LastExceptionBuffer();
144 ~LastExceptionBuffer();
146 void setContents(const char* string
);
147 const char* getContents() const { return d_contents
; }
149 static LastExceptionBuffer
* getCurrent() { return s_currentBuffer
; }
150 static void setCurrent(LastExceptionBuffer
* buffer
) { s_currentBuffer
= buffer
; }
152 static const char* currentContents() {
153 return (getCurrent() == NULL
) ? NULL
: getCurrent()->getContents();
157 /* Disallow copies */
158 LastExceptionBuffer(const LastExceptionBuffer
&) CVC4_UNDEFINED
;
159 LastExceptionBuffer
& operator=(const LastExceptionBuffer
&) CVC4_UNDEFINED
;
163 static CVC4_THREADLOCAL(LastExceptionBuffer
*) s_currentBuffer
;
164 }; /* class LastExceptionBuffer */
166 }/* CVC4 namespace */
168 #endif /* __CVC4__EXCEPTION_H */