Qore Programming Language - C/C++ Library  0.8.13.2
ExceptionSink.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  ExceptionSink.h
4 
5  Qore Programming Language ExceptionSink class definition
6 
7  Copyright (C) 2003 - 2016 Qore Technologies, s.r.o.
8 
9  Permission is hereby granted, free of charge, to any person obtaining a
10  copy of this software and associated documentation files (the "Software"),
11  to deal in the Software without restriction, including without limitation
12  the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  and/or sell copies of the Software, and to permit persons to whom the
14  Software is furnished to do so, subject to the following conditions:
15 
16  The above copyright notice and this permission notice shall be included in
17  all copies or substantial portions of the Software.
18 
19  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  DEALINGS IN THE SOFTWARE.
26 
27  Note that the Qore library is released under a choice of three open-source
28  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
29  information.
30 */
31 
32 #ifndef _QORE_EXCEPTIONSINK_H
33 
34 #define _QORE_EXCEPTIONSINK_H
35 
36 #include <stdarg.h>
37 #include <stdio.h>
38 
39 #include <string>
40 #include <vector>
41 
42 class QoreException;
43 struct QoreProgramLocation;
44 struct QoreCallStack;
45 
48  friend struct qore_es_private;
49 
50 private:
52  struct qore_es_private *priv;
53 
55  DLLLOCAL ExceptionSink(const ExceptionSink&);
56 
58  DLLLOCAL ExceptionSink& operator=(const ExceptionSink&);
59 
60 public:
62  DLLEXPORT ExceptionSink();
63 
65  DLLEXPORT ~ExceptionSink();
66 
68  DLLEXPORT void handleExceptions();
69 
71  DLLEXPORT void handleWarnings();
72 
74  DLLEXPORT bool isEvent() const;
75 
77  DLLEXPORT bool isThreadExit() const;
78 
80  DLLEXPORT bool isException() const;
81 
83 
89  DLLEXPORT operator bool () const;
90 
92 
97  DLLEXPORT AbstractQoreNode* raiseException(const char *err, const char *fmt, ...);
98 
100 
106  DLLEXPORT AbstractQoreNode* raiseErrnoException(const char *err, int en, const char *fmt, ...);
107 
109 
115  DLLEXPORT AbstractQoreNode* raiseErrnoException(const char *err, int en, QoreStringNode* desc);
116 
118 
124  DLLEXPORT AbstractQoreNode* raiseExceptionArg(const char* err, AbstractQoreNode* arg, const char* fmt, ...);
125 
127 
133  DLLEXPORT AbstractQoreNode* raiseExceptionArg(const char* err, AbstractQoreNode* arg, QoreStringNode* desc);
134 
136 
146  DLLEXPORT AbstractQoreNode* raiseExceptionArg(const char* err, AbstractQoreNode* arg, QoreStringNode* desc, const QoreCallStack& stack);
147 
149 
154  DLLEXPORT AbstractQoreNode* raiseException(const char *err, QoreStringNode* desc);
155 
157 
163 
165  DLLEXPORT void raiseThreadExit();
166 
168  DLLEXPORT void assimilate(ExceptionSink *xs);
169 
171  DLLEXPORT void assimilate(ExceptionSink &xs);
172 
174  DLLEXPORT void outOfMemory();
175 
177  DLLEXPORT void clear();
178 
180  DLLEXPORT const AbstractQoreNode* getExceptionErr();
181 
183  DLLEXPORT const AbstractQoreNode* getExceptionDesc();
184 
186  DLLEXPORT const AbstractQoreNode* getExceptionArg();
187 
188  DLLLOCAL void raiseException(QoreException* e);
189  DLLLOCAL void raiseException(const QoreListNode* n);
190  DLLLOCAL void raiseException(const QoreProgramLocation& loc, const char* err, AbstractQoreNode* arg, AbstractQoreNode* desc);
191  DLLLOCAL void raiseException(const QoreProgramLocation& loc, const char* err, AbstractQoreNode* arg, const char* fmt, ...);
192  DLLLOCAL QoreException* catchException();
193  DLLLOCAL QoreException* getException();
194  DLLLOCAL void overrideLocation(const QoreProgramLocation& loc);
195  DLLLOCAL void rethrow(QoreException* old);
196 
197  DLLLOCAL static void defaultExceptionHandler(QoreException* e);
198  DLLLOCAL static void defaultWarningHandler(QoreException* e);
199 };
200 
202 enum qore_call_t {
203  CT_UNUSED = -1,
204  CT_USER = 0,
205  CT_BUILTIN = 1,
206  CT_NEWTHREAD = 2,
207  CT_RETHROW = 3
208 };
209 
211 
214  std::string label;
216  end_line;
217  std::string source;
218  unsigned offset = 0;
219  std::string code;
220 
221  DLLLOCAL QoreSourceLocation(const char* n_label, int start, int end, const char* n_code) :
222  label(n_label), start_line(start), end_line(end), code(n_code) {
223  }
224 
225  DLLLOCAL QoreSourceLocation(const char* n_label, int start, int end, const char* n_source, unsigned n_offset, const char* n_code) :
226  label(n_label), start_line(start), end_line(end), source(n_source), offset(n_offset), code(n_code) {
227  }
228 
229 };
230 
232 
235  qore_call_t type;
236 
237  DLLLOCAL QoreCallStackElement(qore_call_t n_type, const char* n_label, int start, int end, const char* n_code) :
238  QoreSourceLocation(n_label, start, end, n_code), type(n_type) {
239  }
240 
241  DLLLOCAL QoreCallStackElement(qore_call_t n_type, const char* n_label, int start, int end, const char* n_source, unsigned n_offset, const char* n_code) :
242  QoreSourceLocation(n_label, start, end, n_source, n_offset, n_code), type(n_type) {
243  }
244 };
245 
246 typedef std::vector<QoreCallStackElement> callstack_vec_t;
247 
249 
251 struct QoreCallStack : public callstack_vec_t {
252  DLLLOCAL void add(qore_call_t n_type, const char* n_label, int start, int end, const char* n_code) {
253  push_back(QoreCallStackElement(n_type, n_label, start, end, n_code));
254  }
255 
256  DLLLOCAL void add(qore_call_t n_type, const char* n_label, int start, int end, const char* n_source, unsigned n_offset, const char* n_code) {
257  push_back(QoreCallStackElement(n_type, n_label, start, end, n_source, n_offset, n_code));
258  }
259 };
260 
261 static inline void alreadyDeleted(ExceptionSink *xsink, const char *cmeth) {
262  xsink->raiseException("OBJECT-ALREADY-DELETED", "the method %s() cannot be executed because the object has already been deleted", cmeth);
263 }
264 
265 static inline void makeAccessDeletedObjectException(ExceptionSink *xsink, const char *mem, const char *cname) {
266  xsink->raiseException("OBJECT-ALREADY-DELETED", "attempt to access member '%s' of an already-deleted object of class '%s'", mem, cname);
267 }
268 
269 static inline void makeAccessDeletedObjectException(ExceptionSink *xsink, const char *cname) {
270  xsink->raiseException("OBJECT-ALREADY-DELETED", "attempt to access an already-deleted object of class '%s'", cname);
271 }
272 
273 #endif
DLLEXPORT void handleWarnings()
calls ExceptionSink::defaultWarningHandler() on all exceptions still present in the object and then d...
DLLEXPORT void outOfMemory()
intended to be used to handle out of memory errors FIXME: not yet fully implemented ...
DLLEXPORT ~ExceptionSink()
calls ExceptionSink::defaultExceptionHandler() on all exceptions still present in the object and then...
DLLEXPORT void handleExceptions()
calls ExceptionSink::defaultExceptionHandler() on all exceptions still present in the object and then...
The base class for all value and parse types in Qore expression trees.
Definition: AbstractQoreNode.h:54
std::string label
the code label name (source file if source not present)
Definition: ExceptionSink.h:214
DLLEXPORT AbstractQoreNode * raiseException(const char *err, const char *fmt,...)
appends a Qore-language exception to the list
DLLEXPORT AbstractQoreNode * raiseErrnoException(const char *err, int en, const char *fmt,...)
appends a Qore-language exception to the list and appends the result of strerror(errno) to the descri...
Qore&#39;s string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:50
DLLEXPORT const AbstractQoreNode * getExceptionArg()
returns the argument of the top exception
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition: QoreListNode.h:52
std::string code
the function or method call name; method calls in format class::name
Definition: ExceptionSink.h:219
DLLEXPORT const AbstractQoreNode * getExceptionErr()
returns the error of the top exception
DLLEXPORT AbstractQoreNode * raiseExceptionArg(const char *err, AbstractQoreNode *arg, const char *fmt,...)
appends a Qore-language exception to the list, and sets the &#39;arg&#39; member (this object takes over the ...
DLLEXPORT void clear()
deletes the exception list immediately
int start_line
the start line
Definition: ExceptionSink.h:215
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:47
DLLEXPORT const AbstractQoreNode * getExceptionDesc()
returns the description of the top exception
qore_call_t type
the call stack element type
Definition: ExceptionSink.h:235
DLLEXPORT bool isThreadExit() const
returns true if thread_exit has been triggered
DLLEXPORT void raiseThreadExit()
sets the "thread_exit" flag; will cause the current thread to terminate
DLLEXPORT ExceptionSink()
creates an empty ExceptionSink object
Qore source location; strings must be in the default encoding for the Qore process.
Definition: ExceptionSink.h:213
std::string source
optional additional source file
Definition: ExceptionSink.h:217
DLLEXPORT bool isException() const
returns true if at least one exception is present
Qore call stack.
Definition: ExceptionSink.h:251
DLLEXPORT void assimilate(ExceptionSink *xs)
assimilates all entries of the "xs" argument by appending them to the internal list and deletes the "...
DLLEXPORT bool isEvent() const
returns true if at least one exception is present or thread_exit has been triggered ...
call stack element; strings must be in the default encoding for the Qore process
Definition: ExceptionSink.h:234