Audacity  2.2.0
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 
22 class wxString;
23 
24 class AudacityException /* not final */
25 {
26 public:
28  virtual ~AudacityException() = 0;
29 
30  // This is intended as a "polymorphic move copy constructor"
31  // which leaves this "empty".
32  // We would not need this if we had std::exception_ptr
33  virtual std::unique_ptr< AudacityException > Move() = 0;
34 
35  // Action to do in the main thread at idle time of the event loop.
36  virtual void DelayedHandlerAction() = 0;
37 
38 protected:
39  // Make this protected to prevent slicing copies
41  AudacityException( const AudacityException& ) = default;
44 };
45 
46 // A subclass of AudacityException whose delayed handler action displays
47 // a message box. The message is specified by further subclasses.
48 // Not more than one message box will be displayed for each pass through
49 // the main event idle loop.
50 class MessageBoxException /* not final */ : public AudacityException
51 {
52  // Do not allow subclasses to change this behavior further, except
53  // by overriding ErrorMessage()
55  void DelayedHandlerAction() override;
56 
57 protected:
58  // If default-constructed with empty caption, it makes no message box.
59  explicit MessageBoxException( const wxString &caption = wxString{} );
60  ~MessageBoxException() override;
61 
64 
65  // Format a default, internationalized error message for this exception.
66  virtual wxString ErrorMessage() const = 0;
67 
68 private:
69  wxString caption;
70  mutable bool moved { false };
71 };
72 
73 // MessageBoxException that shows a given, unvarying string.
74 class SimpleMessageBoxException /* not final */ : public MessageBoxException
75 {
76 public:
77  explicit SimpleMessageBoxException( const wxString &message_,
78  const wxString &caption = _("Message") )
79  : MessageBoxException{ caption }
80  , message{ message_ }
81  {}
82  ~SimpleMessageBoxException() override;
83 
87 
88  std::unique_ptr< AudacityException > Move() override;
89 
90  // Format a default, internationalized error message for this exception.
91  virtual wxString ErrorMessage() const override;
92 
93 private:
94  wxString message;
95 };
96 
98 {
99  void operator () (AudacityException *pException) const
100  {
101  if ( pException )
102  pException->DelayedHandlerAction();
103  }
104 };
105 
106 // Helpers for defining GuardedCall:
107 
108 // Call one function object,
109 // then another unless the first throws, return result of first
110 template <typename R> struct Sequencer {
111  template <typename F1, typename Argument, typename F2>
112  R operator () (const F1 &f1, Argument &&a, const F2 &f2)
113  {
114  auto result = f1( std::forward<Argument>(a) );
115  f2();
116  return result;
117  }
118 };
119 // template specialization to allow R to be void
120 template <> struct Sequencer<void> {
121  template <typename F1, typename Argument, typename F2>
122  void operator () (const F1 &f1, Argument &&a, const F2 &f2)
123  {
124  f1( std::forward<Argument>(a) );
125  f2();
126  }
127 };
128 
129 // Classes that can supply the second argument of GuardedCall:
130 // Frequently useful converter of all exceptions to some failure constant
131 template <typename R> struct SimpleGuard
132 {
133  explicit SimpleGuard( R value ) : m_value{ value } {}
134  R operator () ( AudacityException * ) const { return m_value; }
135  const R m_value;
136 };
137 
138 // Simple guard specialization that returns bool, and defines Default
139 template<> struct SimpleGuard<bool>
140 {
141  explicit SimpleGuard( bool value ) : m_value{ value } {}
142  bool operator () ( AudacityException * ) const { return m_value; }
144  { return SimpleGuard{ false }; }
145  const bool m_value;
146 };
147 
148 // Simple guard specialization that returns nothing, and defines Default
149 template<> struct SimpleGuard<void>
150 {
152  void operator () ( AudacityException * ) const {}
153  static SimpleGuard Default() { return {}; }
154 };
155 
156 template < typename R >
158 { return SimpleGuard< R >{ value }; }
159 
160 inline SimpleGuard< void > MakeSimpleGuard() { return {}; }
161 
173 template <
174  typename R, // return type
175 
176  typename F1, // function object with signature R()
177 
178  typename F2 = SimpleGuard< R >, // function object
179  // with signature R( AudacityException * )
180 
181  typename F3 =
182  DefaultDelayedHandlerAction // Any( AudacityException * ), ignore return
183 >
184 R GuardedCall
185  ( const F1 &body,
186  const F2 &handler = F2::Default(),
187  const F3 &delayedHandler = {} )
188 {
189  try { return body(); }
190  catch ( AudacityException &e ) {
191  return Sequencer<R>{}( handler, &e,
192  [&] {
193  auto pException =
194  std::shared_ptr< AudacityException > { e.Move().release() };
195  wxTheApp->CallAfter( [=] { // capture pException by value
196  delayedHandler( pException.get() );
197  } );
198  }
199  );
200  }
201  catch ( ... ) {
202  return handler( nullptr );
203  }
204 }
205 
206 #endif
#define PROHIBITED
Definition: Audacity.h:248
root of a hierarchy of classes that are thrown and caught by Audacity.
Definition: AudacityException.h:24
SimpleGuard()
Definition: AudacityException.h:151
SimpleMessageBoxException(const wxString &message_, const wxString &caption=_("Message"))
Definition: AudacityException.h:77
Definition: AudacityException.h:110
virtual void DelayedHandlerAction()=0
AudacityException(AudacityException &&)
Definition: AudacityException.h:40
AudacityException()
Definition: AudacityException.h:27
SimpleGuard< R > MakeSimpleGuard(R value)
Definition: AudacityException.h:157
static SimpleGuard Default()
Definition: AudacityException.h:153
Definition: AudacityException.h:97
Definition: AudacityException.h:131
wxString caption
Definition: AudacityException.h:69
const bool m_value
Definition: AudacityException.h:145
SimpleGuard(R value)
Definition: AudacityException.h:133
virtual ~AudacityException()=0
Definition: AudacityException.cpp:28
R GuardedCall(const F1 &body, const F2 &handler=F2::Default(), const F3 &delayedHandler={})
Definition: AudacityException.h:185
SimpleGuard(bool value)
Definition: AudacityException.h:141
_("Move Track &Down")+wxT("\t")+(GetActiveProject() -> GetCommandManager() ->GetKeyFromName(wxT("TrackMoveDown"))), OnMoveTrack) POPUP_MENU_ITEM(OnMoveTopID, _("Move Track to &Top")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveTop"))), OnMoveTrack) POPUP_MENU_ITEM(OnMoveBottomID, _("Move Track to &Bottom")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveBottom"))), OnMoveTrack) void TrackMenuTable::OnSetName(wxCommandEvent &)
Definition: TrackControls.cpp:137
Definition: AudacityException.h:149
AudacityException & operator=(AudacityException &&)
Definition: AudacityException.h:42
static SimpleGuard Default()
Definition: AudacityException.h:143
const R m_value
Definition: AudacityException.h:135
virtual std::unique_ptr< AudacityException > Move()=0
Definition: AudacityException.h:74
an AudacityException that pops up a single message box even if there were multiple exceptions of the ...
Definition: AudacityException.h:50