Audacity  2.2.2
AudacityException.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  AudacityException
6 
7  Paul Licameli
8 
9 ********************************************************************//********************************************************************/
21 
22 #include "Audacity.h"
23 #include "AudacityException.h"
24 
25 #include <wx/atomic.h>
26 
27 #include "widgets/ErrorDialog.h"
28 
30 {
31 }
32 
33 wxAtomicInt sOutstandingMessages {};
34 
35 MessageBoxException::MessageBoxException( const wxString &caption_ )
36  : caption{ caption_ }
37 {
38  if (!caption.empty())
39  wxAtomicInc( sOutstandingMessages );
40  else
41  // invalidate me
42  moved = true;
43 }
44 
45 // The class needs a copy constructor to be throwable
46 // (or will need it, by C++14 rules). But the copy
47 // needs to act like a move constructor. There must be unique responsibility
48 // for each exception thrown to decrease the global count when it is handled.
50 {
51  caption = that.caption;
52  moved = that.moved;
53  that.moved = true;
54 }
55 
57 {
58  caption = that.caption;
59  if ( this != &that ) {
60  AudacityException::operator=( std::move(that) );
61  if (!moved)
62  wxAtomicDec( sOutstandingMessages );
63 
64  moved = that.moved;
65  that.moved = true;
66  }
67 
68  return *this;
69 }
70 
72 {
73  if (!moved)
74  // If exceptions are used properly, you should never reach this,
75  // because moved should become true earlier in the object's lifetime.
76  wxAtomicDec( sOutstandingMessages );
77 }
78 
80 {
81 }
82 
84 {
85  return message;
86 }
87 
88 // This is meant to be invoked via wxEvtHandler::CallAfter
90 {
91  if (!moved) {
92  // This test prevents accumulation of multiple messages between idle
93  // times of the main even loop. Only the last queued exception
94  // displays its message. We assume that multiple messages have a
95  // common cause such as exhaustion of disk space so that the others
96  // give the user no useful added information.
97  if ( wxAtomicDec( sOutstandingMessages ) == 0 )
99  ErrorMessage(),
101  wxICON_ERROR
102  );
103  moved = true;
104  }
105 }
wxString AudacityMessageBoxCaptionStr()
void DelayedHandlerAction() override
int AudacityMessageBox(const wxString &message, const wxString &caption=AudacityMessageBoxCaptionStr(), long style=wxOK|wxCENTRE, wxWindow *parent=NULL, int x=wxDefaultCoord, int y=wxDefaultCoord)
Definition: ErrorDialog.h:92
MessageBoxException(const wxString &caption=wxString{})
virtual ~AudacityException()=0
virtual wxString ErrorMessage() const override
MessageBoxException & operator=(MessageBoxException &&)
AudacityException & operator=(AudacityException &&)
virtual wxString ErrorMessage() const =0
wxAtomicInt sOutstandingMessages
an AudacityException that pops up a single message box even if there were multiple exceptions of the ...