Audacity 3.2.0
BasicUI.cpp
Go to the documentation of this file.
1/*!********************************************************************
2
3Audacity: A Digital Audio Editor
4
5@file BasicUI.cpp
6
7Paul Licameli
8
9**********************************************************************/
10#include "BasicUI.h"
11
12#include <mutex>
13#include <vector>
14
15namespace BasicUI {
17
18Services::~Services() = default;
19
21
23
24static Services *theInstance = nullptr;
25
26Services *Get() { return theInstance; }
27
29{
30 auto result = theInstance;
31 theInstance = pInstance;
32 return result;
33}
34
35static std::recursive_mutex sActionsMutex;
36static std::vector<Action> sActions;
37
38void 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
49void 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}
Toolkit-neutral facade for basic user interface services.
#define XO(s)
Definition: Internat.h:31
Abstract class defines a few user interface services, not mentioning particular toolkits.
Definition: BasicUI.h:183
virtual ~Services()
Holds a msgid for the translation catalog; may also bind format arguments.
static Services * theInstance
Definition: BasicUI.cpp:24
TranslatableString DefaultCaption()
"Message", suitably translated
Definition: BasicUI.cpp:70
Services * Install(Services *pInstance)
Install an implementation; return the previously installed instance.
Definition: BasicUI.cpp:28
void CallAfter(Action action)
Schedule an action to be done later, and in the main thread.
Definition: BasicUI.cpp:38
static std::vector< Action > sActions
Definition: BasicUI.cpp:36
Services * Get()
Fetch the global instance, or nullptr if none is yet installed.
Definition: BasicUI.cpp:26
std::function< void()> Action
Definition: BasicUI.h:24
static std::recursive_mutex sActionsMutex
Definition: BasicUI.cpp:35
void Yield()
Dispatch waiting events, including actions enqueued by CallAfter.
Definition: BasicUI.cpp:49