Audacity  3.0.3
BasicUI.cpp
Go to the documentation of this file.
1 /*!********************************************************************
2 
3 Audacity: A Digital Audio Editor
4 
5 @file BasicUI.cpp
6 
7 Paul Licameli
8 
9 **********************************************************************/
10 #include "BasicUI.h"
11 
12 #include <mutex>
13 #include <vector>
14 
15 namespace BasicUI {
17 
18 Services::~Services() = default;
19 
21 
23 
24 static Services *theInstance = nullptr;
25 
26 Services *Get() { return theInstance; }
27 
29 {
30  auto result = theInstance;
31  theInstance = pInstance;
32  return result;
33 }
34 
35 static std::recursive_mutex sActionsMutex;
36 static std::vector<Action> sActions;
37 
38 void CallAfter(Action action)
39 {
40  if (auto p = Get())
41  p->DoCallAfter(action);
42  else {
43  // No services yet -- but don't lose the action. Put it in a queue
44  auto guard = std::lock_guard{ sActionsMutex };
45  sActions.emplace_back(std::move(action));
46  }
47 }
48 
49 void Yield()
50 {
51  do {
52  // Dispatch anything in the queue, added while there were no Services
53  {
54  auto guard = std::lock_guard{ sActionsMutex };
55  std::vector<Action> actions;
56  actions.swap(sActions);
57  for (auto &action : actions)
58  action();
59  }
60 
61  // Dispatch according to Services, if present
62  if (auto p = Get())
63  p->DoYield();
64  }
65  // Re-test for more actions that might have been enqueued by actions just
66  // dispatched
67  while ( !sActions.empty() );
68 }
69 
71 {
72  return XO("Message");
73 }
74 }
BasicUI::ProgressDialog::~ProgressDialog
virtual ~ProgressDialog()
TranslatableString
Holds a msgid for the translation catalog; may also bind format arguments.
Definition: TranslatableString.h:32
BasicUI::theInstance
static Services * theInstance
Definition: BasicUI.cpp:24
XO
#define XO(s)
Definition: Internat.h:31
BasicUI::CallAfter
void CallAfter(Action action)
Schedule an action to be done later, and in the main thread.
Definition: BasicUI.cpp:38
BasicUI::Action
std::function< void()> Action
Definition: BasicUI.h:24
BasicUI::GenericProgressDialog::~GenericProgressDialog
virtual ~GenericProgressDialog()
BasicUI::Get
Services * Get()
Fetch the global instance, or nullptr if none is yet installed.
Definition: BasicUI.cpp:26
BasicUI::sActions
static std::vector< Action > sActions
Definition: BasicUI.cpp:36
BasicUI.h
Toolkit-neutral facade for basic user interface services.
BasicUI
Definition: Export.h:39
BasicUI::WindowPlacement::~WindowPlacement
virtual ~WindowPlacement()
BasicUI::DefaultCaption
TranslatableString DefaultCaption()
"Message", suitably translated
Definition: BasicUI.cpp:70
BasicUI::Services
Abstract class defines a few user interface services, not mentioning particular toolkits.
Definition: BasicUI.h:180
BasicUI::Yield
void Yield()
Dispatch waiting events, including actions enqueued by CallAfter.
Definition: BasicUI.cpp:49
BasicUI::Install
Services * Install(Services *pInstance)
Install an implementation; return the previously installed instance.
Definition: BasicUI.cpp:28
BasicUI::Services::~Services
virtual ~Services()
BasicUI::sActionsMutex
static std::recursive_mutex sActionsMutex
Definition: BasicUI.cpp:35