1 |
/*************************************************************************** |
2 |
* * |
3 |
* Copyright (C) 2007 - 2022 Christian Schoenebeck * |
4 |
* * |
5 |
* This program is free software; you can redistribute it and/or modify * |
6 |
* it under the terms of the GNU General Public License as published by * |
7 |
* the Free Software Foundation; either version 2 of the License, or * |
8 |
* (at your option) any later version. * |
9 |
* * |
10 |
* This program is distributed in the hope that it will be useful, * |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 |
* GNU General Public License for more details. * |
14 |
* * |
15 |
* You should have received a copy of the GNU General Public License * |
16 |
* along with this program; if not, write to the Free Software * |
17 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
18 |
* MA 02111-1307 USA * |
19 |
***************************************************************************/ |
20 |
|
21 |
#ifndef LS_INSTRUMENT_EDITOR_FACTORY_H |
22 |
#define LS_INSTRUMENT_EDITOR_FACTORY_H |
23 |
|
24 |
#include <map> |
25 |
#include <vector> |
26 |
#include <list> |
27 |
|
28 |
#include "../common/Exception.h" |
29 |
#include "InstrumentEditor.h" |
30 |
|
31 |
#if defined(WIN32) |
32 |
# define REGISTER_INSTRUMENT_EDITOR(PluginClass) \ |
33 |
extern "C" __declspec(dllexport) void* \ |
34 |
createInstrumentEditorInnerFactory() { \ |
35 |
return new LinuxSampler::InstrumentEditorFactory::InnerFactoryTemplate<PluginClass>(); \ |
36 |
} |
37 |
//FIXME: missing a destruction function for Windows |
38 |
|
39 |
//NOTE: using Windows' DllMain() API callback function is probably not a |
40 |
// viable alternative, as that callback is too limited of what is allowed |
41 |
// to do (e.g. Windows prohibits any thread synchronization mechanisms) |
42 |
#else |
43 |
# define REGISTER_INSTRUMENT_EDITOR(PluginClass) \ |
44 |
LinuxSampler::InstrumentEditorFactory::InnerFactoryRegistrator<PluginClass> \ |
45 |
__auto_register_instrument_editor__##PluginClass; |
46 |
#endif |
47 |
|
48 |
namespace LinuxSampler { |
49 |
|
50 |
class InstrumentEditorFactory { |
51 |
public: |
52 |
class InnerFactory { |
53 |
public: |
54 |
virtual ~InnerFactory() {} |
55 |
virtual InstrumentEditor* Create() = 0; |
56 |
virtual void Destroy(InstrumentEditor* pEditor) = 0; |
57 |
}; |
58 |
|
59 |
template<class PluginClass_T> |
60 |
class InnerFactoryTemplate : public InnerFactory { |
61 |
public: |
62 |
virtual InstrumentEditor* Create() { |
63 |
return new PluginClass_T(); |
64 |
} |
65 |
|
66 |
virtual void Destroy(InstrumentEditor* pEditor) { |
67 |
delete pEditor; |
68 |
} |
69 |
}; |
70 |
|
71 |
template<class PluginClass_T> |
72 |
class InnerFactoryRegistrator { |
73 |
public: |
74 |
InnerFactoryRegistrator() { |
75 |
InnerFactoryTemplate<PluginClass_T>* pInnerFactory = |
76 |
new InnerFactoryTemplate<PluginClass_T>(); |
77 |
InstrumentEditor* pEditor = pInnerFactory->Create(); |
78 |
if (InnerFactories.count(pEditor->Name())) { |
79 |
pInnerFactory->Destroy(pEditor); |
80 |
delete pInnerFactory; |
81 |
} else { |
82 |
InnerFactories[pEditor->Name()] = pInnerFactory; |
83 |
pInnerFactory->Destroy(pEditor); |
84 |
} |
85 |
} |
86 |
|
87 |
~InnerFactoryRegistrator() { |
88 |
//FIXME: potential race with ClosePlugins() |
89 |
// (see comments in the latter for details) |
90 |
InnerFactoryTemplate<PluginClass_T> innerFactory; |
91 |
InstrumentEditor* pEditor = innerFactory.Create(); |
92 |
if (InnerFactories.count(pEditor->Name())) { |
93 |
InnerFactory* pZombie = |
94 |
InnerFactories[pEditor->Name()]; |
95 |
InnerFactories.erase(pEditor->Name()); |
96 |
if (pZombie) delete pZombie; |
97 |
} |
98 |
innerFactory.Destroy(pEditor); |
99 |
} |
100 |
}; |
101 |
|
102 |
static InstrumentEditor* Create(String InstrumentEditorName) throw (Exception); |
103 |
static void Destroy(InstrumentEditor* pInstrumentEditor) throw (Exception); |
104 |
static std::vector<String> PluginDirs(); |
105 |
static String PluginDirsAsString(); |
106 |
static std::vector<String> AvailableEditors(); |
107 |
static String AvailableEditorsAsString(); |
108 |
static std::vector<String> MatchingEditors(String sTypeName, String sTypeVersion); |
109 |
static void LoadPlugins(); |
110 |
static void ClosePlugins(); |
111 |
|
112 |
protected: |
113 |
static std::map<String, InnerFactory*> InnerFactories; |
114 |
static bool bPluginsLoaded; |
115 |
static std::list<void*> LoadedDLLs; |
116 |
|
117 |
private: |
118 |
static bool LoadPlugins(String plugindir); |
119 |
}; |
120 |
|
121 |
} // namespace LinuxSampler |
122 |
|
123 |
#endif // LS_INSTRUMENT_EDITOR_FACTORY_H |