File: AudacityException.cpp

package info (click to toggle)
audacity 3.7.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 125,252 kB
  • sloc: cpp: 358,238; ansic: 75,458; lisp: 7,761; sh: 3,410; python: 1,503; xml: 1,385; perl: 854; makefile: 122
file content (116 lines) | stat: -rw-r--r-- 3,430 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*!********************************************************************

  Audacity: A Digital Audio Editor

  @file AudacityException.cpp
  @brief Implements AudacityException and related

  Paul Licameli

***********************************************************************/

#include "AudacityException.h"

#include <wx/atomic.h>

#include "BasicUI.h"

AudacityException::~AudacityException()
{
}

void AudacityException::EnqueueAction(
   std::exception_ptr pException,
   std::function<void(AudacityException*)> delayedHandler)
{
   BasicUI::CallAfter( [
      pException = std::move(pException), delayedHandler = std::move(delayedHandler)
   ] {
      try {
         std::rethrow_exception(pException);
      }
      catch( AudacityException &e )
         { delayedHandler( &e ); }
   } );
}

wxAtomicInt sOutstandingMessages {};

MessageBoxException::MessageBoxException(
   ExceptionType exceptionType_, const TranslatableString& caption_)
    : caption { caption_ }
    , exceptionType { exceptionType_ }
{
   if (!caption.empty())
      wxAtomicInc( sOutstandingMessages );
   else
      // invalidate me
      moved = true;
}

// The class needs a copy constructor to be throwable
// (or will need it, by C++14 rules).  But the copy
// needs to act like a move constructor.  There must be unique responsibility
// for each exception thrown to decrease the global count when it is handled.
MessageBoxException::MessageBoxException( const MessageBoxException& that )
{
   caption = that.caption;
   moved = that.moved;
   helpUrl = that.helpUrl;
   exceptionType = that.exceptionType;
   that.moved = true;
}

MessageBoxException::~MessageBoxException()
{
   if (!moved)
      // If exceptions are used properly, you should never reach this,
      // because moved should become true earlier in the object's lifetime.
      wxAtomicDec( sOutstandingMessages );
}

SimpleMessageBoxException::~SimpleMessageBoxException()
{
}

TranslatableString SimpleMessageBoxException::ErrorMessage() const
{
   return message;
}

// This is meant to be invoked via wxEvtHandler::CallAfter
void MessageBoxException::DelayedHandlerAction()
{
   if (!moved) {
      // This test prevents accumulation of multiple messages between idle
      // times of the main even loop.  Only the last queued exception
      // displays its message.  We assume that multiple messages have a
      // common cause such as exhaustion of disk space so that the others
      // give the user no useful added information.

      using namespace BasicUI;
      if ( wxAtomicDec( sOutstandingMessages ) == 0 ) {
         if (exceptionType != ExceptionType::Internal
             && ErrorHelpUrl().IsEmpty()) {
            // We show BadEnvironment and BadUserAction in a similar way
            ShowMessageBox(
               ErrorMessage(),
               MessageBoxOptions{}
                  .Caption(caption.empty() ? DefaultCaption() : caption)
                  .IconStyle(Icon::Error) );
         }
         else {
            using namespace BasicUI;
            auto type = exceptionType == ExceptionType::Internal
               ? ErrorDialogType::ModalErrorReport : ErrorDialogType::ModalError;
            ShowErrorDialog( {},
               (caption.empty() ? DefaultCaption() : caption),
               ErrorMessage(),
               ErrorHelpUrl(),
               ErrorDialogOptions{ type } );
         }
      }

      moved = true;
   }
}