Audacity 3.2.0
ModuleManager.h
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 ModuleManager.h
6
7 Dominic Mazzoni
8 James Crook
9
10**********************************************************************/
11
12#ifndef __AUDACITY_MODULEMANAGER_H__
13#define __AUDACITY_MODULEMANAGER_H__
14
15#include "MemoryX.h"
16#include <functional>
17#include <map>
18#include <vector>
19#include <wx/string.h>
20
21#include "Identifier.h"
22
23class wxArrayString;
24class wxDynamicLibrary;
26class PluginProvider;
27class wxWindow;
28using PluginID = wxString;
30
31//
32// Module Manager
33//
34// wxPluginManager would be MUCH better, but it's an "undocumented" framework.
35//
36
37#include "ModuleConstants.h"
38
40
41class Module
42{
43public:
44 Module(const FilePath & name);
45 virtual ~Module();
46
47 void ShowLoadFailureError(const wxString &Error);
48 bool Load(wxString &deferredErrorMessage);
49 void Unload();
50 bool HasDispatch() { return mDispatch != NULL; };
52 void * GetSymbol(const wxString &name);
53 const FilePath &GetName() const { return mName; }
54
55private:
57 std::unique_ptr<wxDynamicLibrary> mLib;
59};
60
61class MODULE_MANAGER_API PluginProviderUniqueHandle final
62{
63 std::unique_ptr<PluginProvider> mPtr;
64public:
66 explicit PluginProviderUniqueHandle(std::unique_ptr<PluginProvider> ptr) : mPtr(std::move(ptr)) { }
68
71
74
75 PluginProvider* get() noexcept { return mPtr.get(); }
76 const PluginProvider* get() const noexcept { return mPtr.get(); }
77
78 PluginProvider* operator->() noexcept { return mPtr.get(); }
79 const PluginProvider* operator->() const noexcept { return mPtr.get(); }
80};
81
82using PluginProviderHandlesMap = std::map<wxString, PluginProviderUniqueHandle>;
83
84class MODULE_MANAGER_API ModuleManager final
85{
86public:
87
88 // -------------------------------------------------------------------------
89 // ModuleManager implementation
90 // -------------------------------------------------------------------------
91
92 static ModuleManager & Get();
93
94 // This string persists in configuration files
95 // So config compatibility will break if it is changed across Audacity versions
96 static wxString GetPluginTypeString();
97
98 static PluginID GetID(PluginProvider *provider);
99
100private:
101 static void FindModules(FilePaths &files);
103 std::vector< std::pair< std::unique_ptr<Module>, wxString > >;
104 static void TryLoadModules(
105 const FilePaths &files, FilePaths &decided, DelayedErrors &errors);
106
107public:
108 void Initialize();
110
111 // PluginManager use
112 // Can be called before Initialize()
113 bool DiscoverProviders();
114
115 // Supports range-for iteration
116 auto Providers() const
117 { return make_iterator_range(mProviders.cbegin(), mProviders.cend()); }
118
120 { return make_iterator_range(mProviders.begin(), mProviders.end()); }
121
122 bool RegisterEffectPlugin(const PluginID & provider, const PluginPath & path,
123 TranslatableString &errMsg);
124
125 PluginProvider *CreateProviderInstance(
126 const PluginID & provider, const PluginPath & path);
127 std::unique_ptr<ComponentInterface>
128 LoadPlugin(const PluginID & provider, const PluginPath & path);
129
130 bool IsProviderValid(const PluginID & provider, const PluginPath & path);
131 bool CheckPluginExist(const PluginID& providerId, const PluginPath& path);
132
133private:
134 // I'm a singleton class
137 ModuleManager(const ModuleManager&) = delete;
139
140 void InitializeBuiltins();
141
142 friend std::unique_ptr<ModuleManager> std::make_unique<ModuleManager>();
143
144 friend std::default_delete<ModuleManager>;
145 static std::unique_ptr<ModuleManager> mInstance;
146
147 // Providers can each report availability of any number of Plug-Ins
148 // identified by "paths", and are also factories of ComponentInterface
149 // objects for each path
151
152 // Other libraries that receive notifications of events described by
153 // ModuleDispatchTypes:
154 std::vector<std::unique_ptr<Module>> mModules;
155};
156
157// ----------------------------------------------------------------------------
158// A factory of PluginProvider objects
159// ----------------------------------------------------------------------------
160using PluginProviderFactory = std::unique_ptr<PluginProvider> (*)();
161
162MODULE_MANAGER_API
164MODULE_MANAGER_API
166
167// Guarantee the registry exists before any registrations, so it will
168// be destroyed only after the un-registrations
169static struct Init{
171
172#endif /* __AUDACITY_MODULEMANAGER_H__ */
const TranslatableString name
Definition: Distortion.cpp:82
wxString PluginID
Definition: EffectManager.h:30
static PluginID GetID(EffectPlugin &effect)
Definition: EffectUI.cpp:31
wxString PluginPath
type alias for identifying a Plugin supplied by a module, each module defining its own interpretation...
Definition: Identifier.h:214
IteratorRange< Iterator > make_iterator_range(const Iterator &i1, const Iterator &i2)
Definition: MemoryX.h:431
ModuleDispatchTypes
std::map< wxString, PluginProviderUniqueHandle > PluginProviderHandlesMap
Definition: ModuleManager.h:82
std::unique_ptr< PluginProvider >(*)() PluginProviderFactory
MODULE_MANAGER_API void UnregisterProviderFactory(PluginProviderFactory factory)
static struct Init sInitBuiltinModules
int(* fnModuleDispatch)(ModuleDispatchTypes type)
Definition: ModuleManager.h:39
MODULE_MANAGER_API void RegisterProviderFactory(PluginProviderFactory factory)
wxString FilePath
Definition: Project.h:20
ComponentInterface provides name / vendor / version functions to identify plugins....
void ShowLoadFailureError(const wxString &Error)
fnModuleDispatch mDispatch
Definition: ModuleManager.h:58
bool HasDispatch()
Definition: ModuleManager.h:50
std::unique_ptr< wxDynamicLibrary > mLib
Definition: ModuleManager.h:57
Module(const FilePath &name)
void * GetSymbol(const wxString &name)
virtual ~Module()
bool Load(wxString &deferredErrorMessage)
int Dispatch(ModuleDispatchTypes type)
const FilePath mName
Definition: ModuleManager.h:56
void Unload()
const FilePath & GetName() const
Definition: ModuleManager.h:53
std::vector< std::pair< std::unique_ptr< Module >, wxString > > DelayedErrors
static std::unique_ptr< ModuleManager > mInstance
PluginProviderHandlesMap mProviders
ModuleManager(const ModuleManager &)=delete
auto Providers() const
std::vector< std::unique_ptr< Module > > mModules
ModuleManager & operator=(const ModuleManager &)=delete
std::unique_ptr< PluginProvider > mPtr
Definition: ModuleManager.h:63
PluginProviderUniqueHandle(PluginProviderUniqueHandle &&)=default
PluginProviderUniqueHandle(std::unique_ptr< PluginProvider > ptr)
Definition: ModuleManager.h:66
PluginProvider * get() noexcept
Definition: ModuleManager.h:75
PluginProvider * operator->() noexcept
Definition: ModuleManager.h:78
PluginProviderUniqueHandle & operator=(const PluginProviderUniqueHandle &)=delete
PluginProviderUniqueHandle & operator=(PluginProviderUniqueHandle &&)=default
const PluginProvider * operator->() const noexcept
Definition: ModuleManager.h:79
PluginProviderUniqueHandle()=default
PluginProviderUniqueHandle(const PluginProviderUniqueHandle &)=delete
const PluginProvider * get() const noexcept
Definition: ModuleManager.h:76
Holds a msgid for the translation catalog; may also bind format arguments.
Extend wxArrayString with move operations and construction and insertion fromstd::initializer_list.
Services * Get()
Fetch the global instance, or nullptr if none is yet installed.
Definition: BasicUI.cpp:194
bool Dispatch()
Definition: Journal.cpp:291
static RegisteredToolbarFactory factory
STL namespace.