Audacity  3.0.3
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 
20 #include "audacity/Types.h"
21 #include "Identifier.h"
22 
23 class wxArrayString;
24 class wxDynamicLibrary;
25 class ComponentInterface;
26 class ModuleInterface;
27 class wxWindow;
28 
29 //
30 // Module Manager
31 //
32 // wxPluginManager would be MUCH better, but it's an "undocumented" framework.
33 //
34 
35 #include "ModuleConstants.h"
36 
38 
39 class Module
40 {
41 public:
42  Module(const FilePath & name);
43  virtual ~Module();
44 
45  void ShowLoadFailureError(const wxString &Error);
46  bool Load(wxString &deferredErrorMessage);
47  void Unload();
48  bool HasDispatch() { return mDispatch != NULL; };
49  int Dispatch(ModuleDispatchTypes type);
50  void * GetSymbol(const wxString &name);
51  const FilePath &GetName() const { return mName; }
52 
53 private:
54  const FilePath mName;
55  std::unique_ptr<wxDynamicLibrary> mLib;
57 };
58 
60  void operator ()(ModuleInterface *pInterface) const;
61 };
62 
63 using ModuleInterfaceHandle = std::unique_ptr<
65 >;
66 
67 typedef std::map<wxString, ModuleInterfaceHandle> ModuleMap;
68 typedef std::map<ModuleInterface *, std::unique_ptr<wxDynamicLibrary>> LibraryMap;
69 
70 class AUDACITY_DLL_API ModuleManager final
71 {
72 public:
73 
74  // -------------------------------------------------------------------------
75  // ModuleManager implementation
76  // -------------------------------------------------------------------------
77 
78  static ModuleManager & Get();
79 
80  // This string persists in configuration files
81  // So config compatibility will break if it is changed across Audacity versions
82  static wxString GetPluginTypeString();
83 
84  static PluginID GetID(ModuleInterface *module);
85 
86 private:
87  static void FindModules(FilePaths &files);
88  using DelayedErrors =
89  std::vector< std::pair< std::unique_ptr<Module>, wxString > >;
90  static void TryLoadModules(
91  const FilePaths &files, FilePaths &decided, DelayedErrors &errors);
92 
93 public:
94  void Initialize();
95  int Dispatch(ModuleDispatchTypes type);
96 
97  // PluginManager use
98  // Can be called before Initialize()
99  bool DiscoverProviders();
100 
101  // Supports range-for iteration
102  auto Providers() const
103  { return make_iterator_range(mDynModules.cbegin(), mDynModules.cend()); }
104 
105  bool RegisterEffectPlugin(const PluginID & provider, const PluginPath & path,
106  TranslatableString &errMsg);
107 
108  ModuleInterface *CreateProviderInstance(
109  const PluginID & provider, const PluginPath & path);
110  std::unique_ptr<ComponentInterface>
111  CreateInstance(const PluginID & provider, const PluginPath & path);
112 
113  bool IsProviderValid(const PluginID & provider, const PluginPath & path);
114  bool IsPluginValid(const PluginID & provider, const PluginPath & path, bool bFast);
115 
116 private:
117  // I'm a singleton class
118  ModuleManager();
119  ~ModuleManager();
120  ModuleManager(const ModuleManager&) PROHIBITED;
121  ModuleManager &operator=(const ModuleManager&) PROHIBITED;
122 
123  void InitializeBuiltins();
124 
125 private:
127  friend std::default_delete<ModuleManager>;
128  static std::unique_ptr<ModuleManager> mInstance;
129 
130  // Module objects, also called Providers, can each report availability of any
131  // number of Plug-Ins identified by "paths", and are also factories of
132  // ComponentInterface objects for each path:
134 
135  // Other libraries that receive notifications of events described by
136  // ModuleDispatchTypes:
137  std::vector<std::unique_ptr<Module>> mModules;
138 };
139 
140 // ----------------------------------------------------------------------------
141 // The module entry point prototype (a factory of ModuleInterface objects)
142 // ----------------------------------------------------------------------------
144 
145 AUDACITY_DLL_API
146 void RegisterProvider(ModuleMain rtn);
147 AUDACITY_DLL_API
149 
150 // Guarantee the registry exists before any registrations, so it will
151 // be destroyed only after the un-registrations
152 static struct Init{
154 
155 #endif /* __AUDACITY_MODULEMANAGER_H__ */
Init
Definition: ModuleManager.h:152
TranslatableString
Holds a msgid for the translation catalog; may also bind format arguments.
Definition: TranslatableString.h:32
ModuleManager::mDynModules
ModuleMap mDynModules
Definition: ModuleManager.h:133
fnModuleDispatch
int(* fnModuleDispatch)(ModuleDispatchTypes type)
Definition: ModuleManager.h:37
make_iterator_range
IteratorRange< Iterator > make_iterator_range(const Iterator &i1, const Iterator &i2)
Definition: MemoryX.h:549
Module::mName
const FilePath mName
Definition: ModuleManager.h:54
ModuleManager::mModules
std::vector< std::unique_ptr< Module > > mModules
Definition: ModuleManager.h:137
Module::GetSymbol
void * GetSymbol(const wxString &name)
Definition: ModuleManager.cpp:161
ModuleMap
std::map< wxString, ModuleInterfaceHandle > ModuleMap
Definition: ModuleManager.h:67
PluginPath
wxString PluginPath
type alias for identifying a Plugin supplied by a module, each module defining its own interpretation...
Definition: Identifier.h:214
Module::~Module
virtual ~Module()
Definition: ModuleManager.cpp:59
Module::mDispatch
fnModuleDispatch mDispatch
Definition: ModuleManager.h:56
Module::ShowLoadFailureError
void ShowLoadFailureError(const wxString &Error)
Definition: ModuleManager.cpp:63
Journal::Dispatch
bool Dispatch()
Definition: Journal.cpp:203
ModuleManager::ModuleInterfaceDeleter
friend ModuleInterfaceDeleter
Definition: ModuleManager.h:126
LibraryMap
std::map< ModuleInterface *, std::unique_ptr< wxDynamicLibrary > > LibraryMap
Definition: ModuleManager.h:68
wxArrayStringEx
Extend wxArrayString with move operations and construction and insertion fromstd::initializer_list.
Definition: wxArrayStringEx.h:18
sInitBuiltinModules
static struct Init sInitBuiltinModules
ModuleInterface
Definition: ModuleInterface.h:70
ModuleManager::DelayedErrors
std::vector< std::pair< std::unique_ptr< Module >, wxString > > DelayedErrors
Definition: ModuleManager.h:89
ModuleManager
Definition: ModuleManager.h:71
FilePath
wxString FilePath
Definition: Project.h:20
BasicUI::Get
Services * Get()
Fetch the global instance, or nullptr if none is yet installed.
Definition: BasicUI.cpp:26
Module::Unload
void Unload()
Definition: ModuleManager.cpp:142
PluginID
wxString PluginID
Definition: EffectManager.h:30
name
const TranslatableString name
Definition: Distortion.cpp:98
Identifier.h
Module::Module
Module(const FilePath &name)
Definition: ModuleManager.cpp:52
Journal::Events::Initialize
void Initialize()
Definition: JournalEvents.cpp:421
ModuleConstants.h
ModuleDispatchTypes
ModuleDispatchTypes
Definition: ModuleConstants.h:27
Types.h
Module::mLib
std::unique_ptr< wxDynamicLibrary > mLib
Definition: ModuleManager.h:55
Init::Init
Init()
Definition: ModuleManager.h:153
ModuleMain
ModuleInterface *(*)() ModuleMain
Definition: ModuleManager.h:143
MemoryX.h
ModuleManager::mInstance
static std::unique_ptr< ModuleManager > mInstance
Definition: ModuleManager.h:128
Module
Definition: ModuleManager.h:40
ModuleInterfaceDeleter::operator()
void operator()(ModuleInterface *pInterface) const
Definition: ModuleManager.cpp:481
Module::Dispatch
int Dispatch(ModuleDispatchTypes type)
Definition: ModuleManager.cpp:152
ModuleInterfaceHandle
std::unique_ptr< ModuleInterface, ModuleInterfaceDeleter > ModuleInterfaceHandle
Definition: ModuleManager.h:65
RegisterProvider
AUDACITY_DLL_API void RegisterProvider(ModuleMain rtn)
Definition: ModuleManager.cpp:185
Module::HasDispatch
bool HasDispatch()
Definition: ModuleManager.h:48
ComponentInterface
ComponentInterface provides name / vendor / version functions to identify plugins....
Definition: ComponentInterface.h:62
ModuleManager::operator=
ModuleManager & operator=(const ModuleManager &) PROHIBITED
ModuleInterfaceDeleter
Definition: ModuleManager.h:59
UnregisterProvider
AUDACITY_DLL_API void UnregisterProvider(ModuleMain rtn)
Definition: ModuleManager.cpp:194
ModuleManager::ModuleManager
ModuleManager(const ModuleManager &) PROHIBITED
Module::GetName
const FilePath & GetName() const
Definition: ModuleManager.h:51
ModuleManager::Providers
auto Providers() const
Definition: ModuleManager.h:102
Module::Load
bool Load(wxString &deferredErrorMessage)
Definition: ModuleManager.cpp:71