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 "IteratorX.h"
16#include <functional>
17#include <map>
18#include <memory>
19#include <vector>
20#include <wx/string.h>
21
22#include "Identifier.h"
23
24class wxArrayString;
25class wxDynamicLibrary;
27class PluginProvider;
28class wxWindow;
29using PluginID = wxString;
31
32//
33// Module Manager
34//
35// wxPluginManager would be MUCH better, but it's an "undocumented" framework.
36//
37
38#include "ModuleConstants.h"
39
41
42class Module
43{
44public:
45 Module(const FilePath & name);
46 virtual ~Module();
47
48 void ShowLoadFailureError(const wxString &Error);
49 bool Load(wxString &deferredErrorMessage);
50 void Unload();
51 bool HasDispatch() { return mDispatch != NULL; };
53 void * GetSymbol(const wxString &name);
54 const FilePath &GetName() const { return mName; }
55
56private:
58 std::unique_ptr<wxDynamicLibrary> mLib;
60};
61
62class MODULE_MANAGER_API PluginProviderUniqueHandle final
63{
64 std::unique_ptr<PluginProvider> mPtr;
65public:
67 explicit PluginProviderUniqueHandle(std::unique_ptr<PluginProvider> ptr) : mPtr(std::move(ptr)) { }
69
72
75
76 PluginProvider* get() noexcept { return mPtr.get(); }
77 const PluginProvider* get() const noexcept { return mPtr.get(); }
78
79 PluginProvider* operator->() noexcept { return mPtr.get(); }
80 const PluginProvider* operator->() const noexcept { return mPtr.get(); }
81};
82
83using PluginProviderHandlesMap = std::map<wxString, PluginProviderUniqueHandle>;
84
85class MODULE_MANAGER_API ModuleManager final
86{
87public:
88
89 // -------------------------------------------------------------------------
90 // ModuleManager implementation
91 // -------------------------------------------------------------------------
92
93 static ModuleManager & Get();
94
95 // This string persists in configuration files
96 // So config compatibility will break if it is changed across Audacity versions
97 static wxString GetPluginTypeString();
98
99 static PluginID GetID(const PluginProvider *provider);
100
101private:
102 static void FindModules(FilePaths &files);
104 std::vector< std::pair< std::unique_ptr<Module>, wxString > >;
105 static void TryLoadModules(
106 const FilePaths &files, FilePaths &decided, DelayedErrors &errors);
107
108public:
109 void Initialize();
111
112 // PluginManager use
113 // Can be called before Initialize()
114 bool DiscoverProviders();
115
116 // Supports range-for iteration
117 auto Providers() const
118 { return make_iterator_range(mProviders.cbegin(), mProviders.cend()); }
119
121 { return make_iterator_range(mProviders.begin(), mProviders.end()); }
122
123 bool RegisterEffectPlugin(const PluginID & provider, const PluginPath & path,
124 TranslatableString &errMsg);
125
126 PluginProvider *CreateProviderInstance(
127 const PluginID & provider, const PluginPath & path);
128 std::unique_ptr<ComponentInterface>
129 LoadPlugin(const PluginID & provider, const PluginPath & path);
130
131 bool IsProviderValid(const PluginID & provider, const PluginPath & path);
132 bool CheckPluginExist(const PluginID& providerId, const PluginPath& path);
133
134private:
135 // I'm a singleton class
138 ModuleManager(const ModuleManager&) = delete;
140
141 void InitializeBuiltins();
142
143 friend std::unique_ptr<ModuleManager> std::make_unique<ModuleManager>();
144
145 friend std::default_delete<ModuleManager>;
146 static std::unique_ptr<ModuleManager> mInstance;
147
148 // Providers can each report availability of any number of Plug-Ins
149 // identified by "paths", and are also factories of ComponentInterface
150 // objects for each path
152
153 // Other libraries that receive notifications of events described by
154 // ModuleDispatchTypes:
155 std::vector<std::unique_ptr<Module>> mModules;
156};
157
158// ----------------------------------------------------------------------------
159// A factory of PluginProvider objects
160// ----------------------------------------------------------------------------
161using PluginProviderFactory = std::unique_ptr<PluginProvider> (*)();
162
163MODULE_MANAGER_API
165MODULE_MANAGER_API
167
168// Guarantee the registry exists before any registrations, so it will
169// be destroyed only after the un-registrations
170static struct Init{
172
173#endif /* __AUDACITY_MODULEMANAGER_H__ */
static RegisteredToolbarFactory factory
wxString PluginID
static PluginID GetID(EffectPlugin &effect)
Definition: EffectUI.cpp:39
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: IteratorX.h:210
ModuleDispatchTypes
std::map< wxString, PluginProviderUniqueHandle > PluginProviderHandlesMap
Definition: ModuleManager.h:83
std::unique_ptr< PluginProvider >(*)() PluginProviderFactory
MODULE_MANAGER_API void UnregisterProviderFactory(PluginProviderFactory factory)
static struct Init sInitBuiltinModules
int(* fnModuleDispatch)(ModuleDispatchTypes type)
Definition: ModuleManager.h:40
MODULE_MANAGER_API void RegisterProviderFactory(PluginProviderFactory factory)
wxString FilePath
Definition: Project.h:21
wxString name
Definition: TagsEditor.cpp:166
ComponentInterface provides name / vendor / version functions to identify plugins....
void ShowLoadFailureError(const wxString &Error)
fnModuleDispatch mDispatch
Definition: ModuleManager.h:59
bool HasDispatch()
Definition: ModuleManager.h:51
std::unique_ptr< wxDynamicLibrary > mLib
Definition: ModuleManager.h:58
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:57
void Unload()
const FilePath & GetName() const
Definition: ModuleManager.h:54
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:64
PluginProviderUniqueHandle(PluginProviderUniqueHandle &&)=default
PluginProviderUniqueHandle(std::unique_ptr< PluginProvider > ptr)
Definition: ModuleManager.h:67
PluginProvider * get() noexcept
Definition: ModuleManager.h:76
PluginProvider * operator->() noexcept
Definition: ModuleManager.h:79
PluginProviderUniqueHandle & operator=(const PluginProviderUniqueHandle &)=delete
PluginProviderUniqueHandle & operator=(PluginProviderUniqueHandle &&)=default
const PluginProvider * operator->() const noexcept
Definition: ModuleManager.h:80
PluginProviderUniqueHandle()=default
PluginProviderUniqueHandle(const PluginProviderUniqueHandle &)=delete
const PluginProvider * get() const noexcept
Definition: ModuleManager.h:77
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:202
auto Dispatch(Head_t< Types > &object, const TupleLike &functions, Args &&... args) -> std::enable_if_t< TypeListCheck_v< Types >, R >
Definition: TypeSwitch.h:313
STL namespace.