/src/wxwidgets/include/wx/private/safecall.h
Line | Count | Source |
1 | | /////////////////////////////////////////////////////////////////////////////// |
2 | | // Name: wx/private/safecall.h |
3 | | // Purpose: Call a function "safely", i.e. potentially catching exceptions. |
4 | | // Author: Vadim Zeitlin |
5 | | // Created: 2025-03-20 |
6 | | // Copyright: (c) 2025 Vadim Zeitlin <vadim@wxwidgets.org> |
7 | | // Licence: wxWindows licence |
8 | | /////////////////////////////////////////////////////////////////////////////// |
9 | | |
10 | | #ifndef _WX_PRIVATE_SAFECALL_H_ |
11 | | #define _WX_PRIVATE_SAFECALL_H_ |
12 | | |
13 | | #include "wx/app.h" |
14 | | |
15 | | #if wxUSE_EXCEPTIONS |
16 | | |
17 | | // Returns true if a special system option disabling catching unhandled |
18 | | // exceptions is set. |
19 | | // |
20 | | // This function is implemented in sysopt.cpp. |
21 | | extern bool WXDLLIMPEXP_BASE wxIsCatchUnhandledExceptionsDisabled(); |
22 | | |
23 | | // General version calls the given function or function-like object and |
24 | | // executes the provided handler if an exception is thrown. |
25 | | // |
26 | | // Both the function and the handler must return the value of the same type R, |
27 | | // possibly void. |
28 | | template <typename R, typename T1, typename T2> |
29 | | inline R wxSafeCall(const T1& func, const T2& handler) |
30 | 0 | { |
31 | 0 | #if wxUSE_SYSTEM_OPTIONS |
32 | 0 | if ( wxIsCatchUnhandledExceptionsDisabled() ) |
33 | 0 | { |
34 | 0 | return func(); |
35 | 0 | } |
36 | 0 | #endif // wxUSE_SYSTEM_OPTIONS |
37 | | |
38 | 0 | try |
39 | 0 | { |
40 | 0 | return func(); |
41 | 0 | } |
42 | 0 | catch ( ... ) |
43 | 0 | { |
44 | 0 | return handler(); |
45 | 0 | } |
46 | 0 | } Unexecuted instantiation: appbase.cpp:void wxSafeCall<void, wxAppConsoleBase::CallOnUnhandledException()::$_0, wxAppConsoleBase::CallOnUnhandledException()::$_1>(wxAppConsoleBase::CallOnUnhandledException()::$_0 const&, wxAppConsoleBase::CallOnUnhandledException()::$_1 const&) Unexecuted instantiation: evtloopcmn.cpp:void wxSafeCall<void, wxEventLoopManual::DoRun()::$_0, wxEventLoopManual::DoRun()::$_1>(wxEventLoopManual::DoRun()::$_0 const&, wxEventLoopManual::DoRun()::$_1 const&) Unexecuted instantiation: threadpsx.cpp:void wxSafeCall<void, wxThreadInternal::PthreadStart(wxThread*)::$_0, void ()>(wxThreadInternal::PthreadStart(wxThread*)::$_0 const&, void ( const&)()) Unexecuted instantiation: threadpsx.cpp:void wxSafeCall<void, wxThread::Exit(void*)::$_0, void ()>(wxThread::Exit(void*)::$_0 const&, void ( const&)()) Unexecuted instantiation: event.cpp:bool wxSafeCall<bool, wxEvtHandler::SafelyProcessEvent(wxEvent&)::$_0, wxEvtHandler::SafelyProcessEvent(wxEvent&)::$_1>(wxEvtHandler::SafelyProcessEvent(wxEvent&)::$_0 const&, wxEvtHandler::SafelyProcessEvent(wxEvent&)::$_1 const&) |
47 | | |
48 | | // Simplified version for the common case when the function doesn't return |
49 | | // anything and we just want to call wxApp::OnUnhandledException() if it |
50 | | // throws. |
51 | | template <typename T> |
52 | | inline void wxSafeCall(const T& func) |
53 | 0 | { |
54 | 0 | wxSafeCall<void>(func, wxApp::CallOnUnhandledException); |
55 | 0 | } Unexecuted instantiation: threadpsx.cpp:void wxSafeCall<wxThreadInternal::PthreadStart(wxThread*)::$_0>(wxThreadInternal::PthreadStart(wxThread*)::$_0 const&) Unexecuted instantiation: threadpsx.cpp:void wxSafeCall<wxThread::Exit(void*)::$_0>(wxThread::Exit(void*)::$_0 const&) |
56 | | |
57 | | #else // !wxUSE_EXCEPTIONS |
58 | | |
59 | | template <typename R, typename T1, typename T2> |
60 | | inline R wxSafeCall(const T1& func, const T2& WXUNUSED(handler)) |
61 | | { |
62 | | return func(); |
63 | | } |
64 | | |
65 | | template <typename T> |
66 | | inline void wxSafeCall(const T& func) |
67 | | { |
68 | | func(); |
69 | | } |
70 | | |
71 | | #endif // wxUSE_EXCEPTIONS/!wxUSE_EXCEPTIONS |
72 | | |
73 | | #endif // _WX_PRIVATE_SAFECALL_H_ |