forked from Sneeds-Feed-and-Seed/sneedacity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModuleManager.h
152 lines (115 loc) · 4.43 KB
/
ModuleManager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**********************************************************************
Sneedacity: A Digital Audio Editor
ModuleManager.h
Dominic Mazzoni
James Crook
**********************************************************************/
#ifndef __SNEEDACITY_MODULEMANAGER_H__
#define __SNEEDACITY_MODULEMANAGER_H__
#include <memory>
#include <map>
#include <vector>
#include "sneedacity/Types.h"
#include "Identifier.h"
class wxArrayString;
class wxDynamicLibrary;
class ComponentInterface;
class ModuleInterface;
class wxWindow;
//
// Module Manager
//
// wxPluginManager would be MUCH better, but it's an "undocumented" framework.
//
#include "ModuleConstants.h"
typedef int (*fnModuleDispatch)(ModuleDispatchTypes type);
class Module
{
public:
Module(const FilePath & name);
virtual ~Module();
void ShowLoadFailureError(const wxString &Error);
bool Load(wxString &deferredErrorMessage);
void Unload();
bool HasDispatch() { return mDispatch != NULL; };
int Dispatch(ModuleDispatchTypes type);
void * GetSymbol(const wxString &name);
const FilePath &GetName() const { return mName; }
private:
const FilePath mName;
std::unique_ptr<wxDynamicLibrary> mLib;
fnModuleDispatch mDispatch;
};
struct ModuleInterfaceDeleter {
void operator ()(ModuleInterface *pInterface) const;
};
using ModuleInterfaceHandle = std::unique_ptr<
ModuleInterface, ModuleInterfaceDeleter
>;
typedef std::map<wxString, ModuleInterfaceHandle> ModuleMap;
typedef std::map<ModuleInterface *, std::unique_ptr<wxDynamicLibrary>> LibraryMap;
using PluginIDs = wxArrayString;
class SNEEDACITY_DLL_API ModuleManager final
{
public:
// -------------------------------------------------------------------------
// ModuleManager implementation
// -------------------------------------------------------------------------
static ModuleManager & Get();
// This string persists in configuration files
// So config compatibility will break if it is changed across Sneedacity versions
static wxString GetPluginTypeString();
static PluginID GetID(ModuleInterface *module);
private:
static void FindModules(FilePaths &files);
using DelayedErrors =
std::vector< std::pair< std::unique_ptr<Module>, wxString > >;
static void TryLoadModules(
const FilePaths &files, FilePaths &decided, DelayedErrors &errors);
public:
void Initialize();
int Dispatch(ModuleDispatchTypes type);
// PluginManager use
// Can be called before Initialize()
bool DiscoverProviders();
PluginPaths FindPluginsForProvider(const PluginID & provider, const PluginPath & path);
bool RegisterEffectPlugin(const PluginID & provider, const PluginPath & path,
TranslatableString &errMsg);
ModuleInterface *CreateProviderInstance(
const PluginID & provider, const PluginPath & path);
std::unique_ptr<ComponentInterface>
CreateInstance(const PluginID & provider, const PluginPath & path);
bool IsProviderValid(const PluginID & provider, const PluginPath & path);
bool IsPluginValid(const PluginID & provider, const PluginPath & path, bool bFast);
private:
// I'm a singleton class
ModuleManager();
~ModuleManager();
ModuleManager(const ModuleManager&) PROHIBITED;
ModuleManager &operator=(const ModuleManager&) PROHIBITED;
void InitializeBuiltins();
private:
friend ModuleInterfaceDeleter;
friend std::default_delete<ModuleManager>;
static std::unique_ptr<ModuleManager> mInstance;
// Module objects, also called Providers, can each report availability of any
// number of Plug-Ins identified by "paths", and are also factories of
// ComponentInterface objects for each path:
ModuleMap mDynModules;
// Other libraries that receive notifications of events described by
// ModuleDispatchTypes:
std::vector<std::unique_ptr<Module>> mModules;
};
// ----------------------------------------------------------------------------
// The module entry point prototype (a factory of ModuleInterface objects)
// ----------------------------------------------------------------------------
using ModuleMain = ModuleInterface *(*)();
SNEEDACITY_DLL_API
void RegisterProvider(ModuleMain rtn);
SNEEDACITY_DLL_API
void UnregisterProvider(ModuleMain rtn);
// Guarantee the registry exists before any registrations, so it will
// be destroyed only after the un-registrations
static struct Init{
Init() { RegisterProvider(nullptr); } } sInitBuiltinModules;
#endif /* __SNEEDACITY_MODULEMANAGER_H__ */