Audacity  3.0.3
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  @file AudacityException.h
9  @brief Declare abstract class AudacityException, some often-used subclasses, and @ref GuardedCall
10 
11  Paul Licameli
12  **********************************************************************/
13 
14 #include "MemoryX.h"
15 #include <wx/app.h> // used in inline function template
16 #include <exception>
17 
18 #include "Internat.h"
19 
21 
24 class AudacityException /* not final */
25 {
26 public:
28  virtual ~AudacityException() = 0;
29 
31  virtual void DelayedHandlerAction() = 0;
32 
33 protected:
35  AudacityException( const AudacityException& ) = default;
36 
38  // see https://bugzilla.audacityteam.org/show_bug.cgi?id=2442
40 
43 };
44 
46 
48 class MessageBoxException /* not final */ : public AudacityException
49 {
52 
54  void DelayedHandlerAction() final;
55 
56 protected:
58  explicit MessageBoxException(
59  const TranslatableString &caption = {}
60  );
61  ~MessageBoxException() override;
62 
64 
66  virtual TranslatableString ErrorMessage() const = 0;
67  virtual wxString ErrorHelpUrl() const { return helpUrl; };
68 
69 private:
71  mutable bool moved { false };
72 protected:
73  mutable wxString helpUrl{ "" };
74 };
75 
77 class SimpleMessageBoxException /* not final */ : public MessageBoxException
78 {
79 public:
81  const TranslatableString &message_, //<! Message to show
82  const TranslatableString &caption = XO("Message"), //<! Short caption in frame around message
83  const wxString &helpUrl_ = "" // Optional URL for help.
84  )
86  , message{ message_ }
87  {
88  helpUrl = helpUrl_;
89  }
90  ~SimpleMessageBoxException() override;
91 
94  SimpleMessageBoxException && ) PROHIBITED;
95 
96  // Format a default, internationalized error message for this exception.
97  virtual TranslatableString ErrorMessage() const override;
98 
99 private:
101 };
102 
103 
106 {
107  void operator () (AudacityException *pException) const
108  {
109  if ( pException )
110  pException->DelayedHandlerAction();
111  }
112 };
113 
115 
116 template <typename R> struct SimpleGuard
117 {
118  explicit SimpleGuard(
119  R value
120  )
121  : m_value{ value } {}
122  R operator () ( AudacityException * ) const { return m_value; }
123  const R m_value;
124 };
125 
127 template<> struct SimpleGuard<bool>
128 {
129  explicit SimpleGuard(
130  bool value
131  )
132  : m_value{ value } {}
133  bool operator () ( AudacityException * ) const { return m_value; }
135  { return SimpleGuard{ false }; }
136  const bool m_value;
137 };
138 
140 template<> struct SimpleGuard<void>
141 {
143  void operator () ( AudacityException * ) const {}
144  static SimpleGuard Default() { return {}; }
145 };
146 
148 template < typename R >
150 { return SimpleGuard< R >{ value }; }
151 
153 inline SimpleGuard< void > MakeSimpleGuard() { return {}; }
154 
175 template <
176  typename R = void,
177 
178  typename F1, // function object with signature R()
179 
180  typename F2 = SimpleGuard< R >, // function object
181  // with signature R( AudacityException * )
182 
183  typename F3 =
184  DefaultDelayedHandlerAction // Any( AudacityException * ), ignore return
185 >
188  const F1 &body,
189  const F2 &handler = F2::Default(),
190  const F3 &delayedHandler = {}
191 )
192 {
193  try { return body(); }
194  catch ( AudacityException &e ) {
195 
196  auto end = finally([&]{
197  // At this point, e is the "current" exception, but not "uncaught"
198  // unless it was rethrown by handler. handler might also throw some
199  // other exception object.
200  if (!std::uncaught_exception()) {
201  auto pException = std::current_exception(); // This points to e
202  wxTheApp->CallAfter( [=] { // capture pException by value
203  try { std::rethrow_exception(pException); }
204  catch( AudacityException &e )
205  { delayedHandler( &e ); }
206  } );
207  }
208  });
209 
210  return handler( &e );
211  }
212  catch ( ... ) {
213  return handler( nullptr );
214  }
215 }
216 
217 #endif
SimpleGuard< void >::Default
static SimpleGuard Default()
Definition: AudacityException.h:144
TranslatableString
Definition: Types.h:290
MessageBoxException::~MessageBoxException
~MessageBoxException() override
Definition: AudacityException.cpp:48
SimpleMessageBoxException::message
TranslatableString message
Stored message.
Definition: AudacityException.h:100
SimpleGuard
A default template parameter for GuardedCall<R>
Definition: AudacityException.h:117
SimpleMessageBoxException::ErrorMessage
virtual TranslatableString ErrorMessage() const override
Format the error message for this exception.
Definition: AudacityException.cpp:60
AudacityException::operator=
AudacityException & operator=(const AudacityException &)=delete
Disallow assignment.
SimpleMessageBoxException::operator=
SimpleMessageBoxException & operator=(SimpleMessageBoxException &&) PROHIBITED
MakeSimpleGuard
SimpleGuard< R > MakeSimpleGuard(R value)
Convert a value to a handler function returning that value, suitable for GuardedCall<R>
Definition: AudacityException.h:149
XO
#define XO(s)
Definition: Internat.h:32
AudacityException::AudacityException
AudacityException(AudacityException &&)=delete
Don't allow moves of this class or subclasses.
SimpleGuard::m_value
const R m_value
Definition: AudacityException.h:123
SimpleGuard< bool >::m_value
const bool m_value
Definition: AudacityException.h:136
AudacityException::AudacityException
AudacityException(const AudacityException &)=default
Make this protected to prevent slicing copies.
MessageBoxException::ErrorHelpUrl
virtual wxString ErrorHelpUrl() const
Definition: AudacityException.h:67
MessageBoxException::ErrorMessage
virtual TranslatableString ErrorMessage() const =0
Format the error message for this exception.
AudacityException
Base class for exceptions specially processed by the application.
Definition: AudacityException.h:25
MessageBoxException::MessageBoxException
MessageBoxException(const TranslatableString &caption={})
If default-constructed with empty caption, it makes no message box.
Definition: AudacityException.cpp:26
AudacityException::~AudacityException
virtual ~AudacityException()=0
Definition: AudacityException.cpp:20
MessageBoxException::DelayedHandlerAction
void DelayedHandlerAction() final
Do not allow subclasses to change behavior, except by overriding ErrorMessage().
Definition: AudacityException.cpp:66
SimpleGuard::SimpleGuard
SimpleGuard(R value)
Definition: AudacityException.h:118
AudacityException::AudacityException
AudacityException()
Definition: AudacityException.h:27
Internat.h
SimpleMessageBoxException::~SimpleMessageBoxException
~SimpleMessageBoxException() override
Definition: AudacityException.cpp:56
SimpleGuard< void >
Specialization of SimpleGuard, also defining a default value.
Definition: AudacityException.h:141
SimpleGuard< void >::SimpleGuard
SimpleGuard()
Definition: AudacityException.h:142
SimpleGuard< bool >::Default
static SimpleGuard Default()
Definition: AudacityException.h:134
MemoryX.h
GuardedCall
R GuardedCall(const F1 &body, const F2 &handler=F2::Default(), const F3 &delayedHandler={})
Execute some code on any thread; catch any AudacityException; enqueue error report on the main thread...
Definition: AudacityException.h:187
AudacityException::DelayedHandlerAction
virtual void DelayedHandlerAction()=0
Action to do in the main thread at idle time of the event loop.
MessageBoxException
Abstract AudacityException subclass displays a message, specified by further subclass.
Definition: AudacityException.h:49
MessageBoxException::helpUrl
wxString helpUrl
Definition: AudacityException.h:73
DefaultDelayedHandlerAction
A default template parameter for GuardedCall.
Definition: AudacityException.h:106
SimpleMessageBoxException::SimpleMessageBoxException
SimpleMessageBoxException(const SimpleMessageBoxException &)=default
SimpleGuard< bool >::SimpleGuard
SimpleGuard(bool value)
Definition: AudacityException.h:129
SimpleMessageBoxException
A MessageBoxException that shows a given, unvarying string.
Definition: AudacityException.h:78
MessageBoxException::caption
TranslatableString caption
Stored caption.
Definition: AudacityException.h:67
MessageBoxException::moved
bool moved
Whether *this has been the source of a copy.
Definition: AudacityException.h:71
SimpleMessageBoxException::SimpleMessageBoxException
SimpleMessageBoxException(const TranslatableString &message_, const TranslatableString &caption=XO("Message"), const wxString &helpUrl_="")
Definition: AudacityException.h:80