1 |
/*************************************************************************** |
2 |
* * |
3 |
* Copyright (C) 2007 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 |
#define REGISTER_INSTRUMENT_EDITOR(PluginClass) \ |
32 |
LinuxSampler::InstrumentEditorFactory::InnerFactoryRegistrator<PluginClass> \ |
33 |
__auto_register_instrument_editor__##PluginClass; |
34 |
|
35 |
namespace LinuxSampler { |
36 |
|
37 |
class InstrumentEditorFactory { |
38 |
public: |
39 |
class InnerFactory { |
40 |
public: |
41 |
virtual InstrumentEditor* Create() = 0; |
42 |
virtual void Destroy(InstrumentEditor* pEditor) = 0; |
43 |
}; |
44 |
|
45 |
template<class PluginClass_T> |
46 |
class InnerFactoryTemplate : public InnerFactory { |
47 |
public: |
48 |
virtual InstrumentEditor* Create() { |
49 |
return new PluginClass_T(); |
50 |
} |
51 |
|
52 |
virtual void Destroy(InstrumentEditor* pEditor) { |
53 |
delete pEditor; |
54 |
} |
55 |
}; |
56 |
|
57 |
template<class PluginClass_T> |
58 |
class InnerFactoryRegistrator { |
59 |
public: |
60 |
InnerFactoryRegistrator() { |
61 |
InnerFactoryTemplate<PluginClass_T>* pInnerFactory = |
62 |
new InnerFactoryTemplate<PluginClass_T>(); |
63 |
InstrumentEditor* pEditor = pInnerFactory->Create(); |
64 |
if (InnerFactories.count(pEditor->Name())) { |
65 |
pInnerFactory->Destroy(pEditor); |
66 |
delete pInnerFactory; |
67 |
} else { |
68 |
InnerFactories[pEditor->Name()] = pInnerFactory; |
69 |
pInnerFactory->Destroy(pEditor); |
70 |
} |
71 |
} |
72 |
|
73 |
~InnerFactoryRegistrator() { |
74 |
InnerFactoryTemplate<PluginClass_T> innerFactory; |
75 |
InstrumentEditor* pEditor = innerFactory.Create(); |
76 |
if (InnerFactories.count(pEditor->Name())) { |
77 |
InnerFactory* pZombie = |
78 |
InnerFactories[pEditor->Name()]; |
79 |
InnerFactories.erase(pEditor->Name()); |
80 |
if (pZombie) delete pZombie; |
81 |
} |
82 |
innerFactory.Destroy(pEditor); |
83 |
} |
84 |
}; |
85 |
|
86 |
static InstrumentEditor* Create(String InstrumentEditorName) throw (Exception); |
87 |
static void Destroy(InstrumentEditor* pInstrumentEditor) throw (Exception); |
88 |
static std::vector<String> AvailableEditors(); |
89 |
static String AvailableEditorsAsString(); |
90 |
static std::vector<String> MatchingEditors(String sTypeName, String sTypeVersion); |
91 |
static void LoadPlugins(); |
92 |
static void ClosePlugins(); |
93 |
|
94 |
protected: |
95 |
static std::map<String, InnerFactory*> InnerFactories; |
96 |
static bool bPluginsLoaded; |
97 |
static std::list<void*> LoadedDLLs; |
98 |
}; |
99 |
|
100 |
} // namespace LinuxSampler |
101 |
|
102 |
#endif // LS_INSTRUMENT_EDITOR_FACTORY_H |