Audacity  2.2.2
AudacityException.h
Go to the documentation of this file.
1 #ifndef __AUDACITY_EXCEPTION__
2 #define __AUDACITY_EXCEPTION__
3 
4 /**********************************************************************
5 
6  Audacity: A Digital Audio Editor
7 
8  AudacityException.h
9 
10  Paul Licameli
11 
12  Define the root of a hierarchy of classes that are thrown and caught
13  by Audacity.
14 
15  Define some subclasses. Not all subclasses need be defined here.
16 
17  **********************************************************************/
18 
19 #include "MemoryX.h"
20 #include <wx/app.h>
21 #include <exception>
22 
23 #include "Internat.h"
24 
25 class wxString;
26 
27 class AudacityException /* not final */
28 {
29 public:
31  virtual ~AudacityException() = 0;
32 
33  // Action to do in the main thread at idle time of the event loop.
34  virtual void DelayedHandlerAction() = 0;
35 
36 protected:
37  // Make this protected to prevent slicing copies
39  AudacityException( const AudacityException& ) = default;
42 };
43 
44 // A subclass of AudacityException whose delayed handler action displays
45 // a message box. The message is specified by further subclasses.
46 // Not more than one message box will be displayed for each pass through
47 // the main event idle loop.
48 class MessageBoxException /* not final */ : public AudacityException
49 {
50  // Do not allow subclasses to change this behavior further, except
51  // by overriding ErrorMessage()
53  void DelayedHandlerAction() override;
54 
55 protected:
56  // If default-constructed with empty caption, it makes no message box.
57  explicit MessageBoxException( const wxString &caption = wxString{} );
58  ~MessageBoxException() override;
59 
62 
63  // Format a default, internationalized error message for this exception.
64  virtual wxString ErrorMessage() const = 0;
65 
66 private:
67  wxString caption;
68  mutable bool moved { false };
69 };
70 
71 // MessageBoxException that shows a given, unvarying string.
72 class SimpleMessageBoxException /* not final */ : public MessageBoxException
73 {
74 public:
75  explicit SimpleMessageBoxException( const wxString &message_,
76  const wxString &caption = _("Message") )
77  : MessageBoxException{ caption }
78  , message{ message_ }
79  {}
80  ~SimpleMessageBoxException() override;
81 
85 
86  // Format a default, internationalized error message for this exception.
87  virtual wxString ErrorMessage() const override;
88 
89 private:
90  wxString message;
91 };
92 
94 {
95  void operator () (AudacityException *pException) const
96  {
97  if ( pException )
98  pException->DelayedHandlerAction();
99  }
100 };
101 
102 // Classes that can supply the second argument of GuardedCall:
103 // Frequently useful converter of all exceptions to some failure constant
104 template <typename R> struct SimpleGuard
105 {
106  explicit SimpleGuard( R value ) : m_value{ value } {}
107  R operator () ( AudacityException * ) const { return m_value; }
108  const R m_value;
109 };
110 
111 // Simple guard specialization that returns bool, and defines Default
112 template<> struct SimpleGuard<bool>
113 {
114  explicit SimpleGuard( bool value ) : m_value{ value } {}
115  bool operator () ( AudacityException * ) const { return m_value; }
117  { return SimpleGuard{ false }; }
118  const bool m_value;
119 };
120 
121 // Simple guard specialization that returns nothing, and defines Default
122 template<> struct SimpleGuard<void>
123 {
125  void operator () ( AudacityException * ) const {}
126  static SimpleGuard Default() { return {}; }
127 };
128 
129 template < typename R >
131 { return SimpleGuard< R >{ value }; }
132 
133 inline SimpleGuard< void > MakeSimpleGuard() { return {}; }
134 
146 template <
147  typename R = void, // return type
148 
149  typename F1, // function object with signature R()
150 
151  typename F2 = SimpleGuard< R >, // function object
152  // with signature R( AudacityException * )
153 
154  typename F3 =
155  DefaultDelayedHandlerAction // Any( AudacityException * ), ignore return
156 >
157 R GuardedCall
158  ( const F1 &body,
159  const F2 &handler = F2::Default(),
160  const F3 &delayedHandler = {} )
161 {
162  try { return body(); }
163  catch ( AudacityException &e ) {
164 
165  auto end = finally([&]{
166  // At this point, e is the "current" exception, but not "uncaught"
167  // unless it was rethrown by handler. handler might also throw some
168  // other exception object.
169  if (!std::uncaught_exception()) {
170  auto pException = std::current_exception(); // This points to e
171  wxTheApp->CallAfter( [=] { // capture pException by value
172  try { std::rethrow_exception(pException); }
173  catch( AudacityException &e )
174  { delayedHandler( &e ); }
175  } );
176  }
177  });
178 
179  return handler( &e );
180  }
181  catch ( ... ) {
182  return handler( nullptr );
183  }
184 }
185 
186 #endif
#define PROHIBITED
Definition: Audacity.h:224
root of a hierarchy of classes that are thrown and caught by Audacity.
SimpleMessageBoxException(const wxString &message_, const wxString &caption=_("Message"))
virtual void DelayedHandlerAction()=0
AudacityException(AudacityException &&)
SimpleGuard< R > MakeSimpleGuard(R value)
static SimpleGuard Default()
SimpleGuard(R value)
virtual ~AudacityException()=0
R GuardedCall(const F1 &body, const F2 &handler=F2::Default(), const F3 &delayedHandler={})
_("Move Track &Down")+wxT("\t")+(GetActiveProject() -> GetCommandManager() ->GetKeyFromName(wxT("TrackMoveDown")).Raw()), OnMoveTrack) POPUP_MENU_ITEM(OnMoveTopID, _("Move Track to &Top")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveTop")).Raw()), OnMoveTrack) POPUP_MENU_ITEM(OnMoveBottomID, _("Move Track to &Bottom")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveBottom")).Raw()), OnMoveTrack)#define SET_TRACK_NAME_PLUGIN_SYMBOLclass SetTrackNameCommand:public AudacityCommand
AudacityException & operator=(AudacityException &&)
static SimpleGuard Default()
an AudacityException that pops up a single message box even if there were multiple exceptions of the ...