00001 /*************************************************************************** 00002 * * 00003 * Copyright (C) 2007 - 2009 Christian Schoenebeck * 00004 * * 00005 * This program is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU General Public License as published by * 00007 * the Free Software Foundation; either version 2 of the License, or * 00008 * (at your option) any later version. * 00009 * * 00010 * This program is distributed in the hope that it will be useful, * 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00013 * GNU General Public License for more details. * 00014 * * 00015 * You should have received a copy of the GNU General Public License * 00016 * along with this program; if not, write to the Free Software * 00017 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, * 00018 * MA 02111-1307 USA * 00019 ***************************************************************************/ 00020 00021 #ifndef LS_INSTRUMENT_EDITOR_FACTORY_H 00022 #define LS_INSTRUMENT_EDITOR_FACTORY_H 00023 00024 #include <map> 00025 #include <vector> 00026 #include <list> 00027 00028 #include "../common/Exception.h" 00029 #include "InstrumentEditor.h" 00030 00031 #if defined(WIN32) 00032 # define REGISTER_INSTRUMENT_EDITOR(PluginClass) \ 00033 extern "C" __declspec(dllexport) void* \ 00034 createInstrumentEditorInnerFactory() { \ 00035 return new LinuxSampler::InstrumentEditorFactory::InnerFactoryTemplate<PluginClass>(); \ 00036 } 00037 #else 00038 # define REGISTER_INSTRUMENT_EDITOR(PluginClass) \ 00039 LinuxSampler::InstrumentEditorFactory::InnerFactoryRegistrator<PluginClass> \ 00040 __auto_register_instrument_editor__##PluginClass; 00041 #endif 00042 00043 namespace LinuxSampler { 00044 00045 class InstrumentEditorFactory { 00046 public: 00047 class InnerFactory { 00048 public: 00049 virtual InstrumentEditor* Create() = 0; 00050 virtual void Destroy(InstrumentEditor* pEditor) = 0; 00051 }; 00052 00053 template<class PluginClass_T> 00054 class InnerFactoryTemplate : public InnerFactory { 00055 public: 00056 virtual InstrumentEditor* Create() { 00057 return new PluginClass_T(); 00058 } 00059 00060 virtual void Destroy(InstrumentEditor* pEditor) { 00061 delete pEditor; 00062 } 00063 }; 00064 00065 template<class PluginClass_T> 00066 class InnerFactoryRegistrator { 00067 public: 00068 InnerFactoryRegistrator() { 00069 InnerFactoryTemplate<PluginClass_T>* pInnerFactory = 00070 new InnerFactoryTemplate<PluginClass_T>(); 00071 InstrumentEditor* pEditor = pInnerFactory->Create(); 00072 if (InnerFactories.count(pEditor->Name())) { 00073 pInnerFactory->Destroy(pEditor); 00074 delete pInnerFactory; 00075 } else { 00076 InnerFactories[pEditor->Name()] = pInnerFactory; 00077 pInnerFactory->Destroy(pEditor); 00078 } 00079 } 00080 00081 ~InnerFactoryRegistrator() { 00082 InnerFactoryTemplate<PluginClass_T> innerFactory; 00083 InstrumentEditor* pEditor = innerFactory.Create(); 00084 if (InnerFactories.count(pEditor->Name())) { 00085 InnerFactory* pZombie = 00086 InnerFactories[pEditor->Name()]; 00087 InnerFactories.erase(pEditor->Name()); 00088 if (pZombie) delete pZombie; 00089 } 00090 innerFactory.Destroy(pEditor); 00091 } 00092 }; 00093 00094 static InstrumentEditor* Create(String InstrumentEditorName) throw (Exception); 00095 static void Destroy(InstrumentEditor* pInstrumentEditor) throw (Exception); 00096 static std::vector<String> AvailableEditors(); 00097 static String AvailableEditorsAsString(); 00098 static std::vector<String> MatchingEditors(String sTypeName, String sTypeVersion); 00099 static void LoadPlugins(); 00100 static void ClosePlugins(); 00101 00102 protected: 00103 static std::map<String, InnerFactory*> InnerFactories; 00104 static bool bPluginsLoaded; 00105 static std::list<void*> LoadedDLLs; 00106 00107 private: 00108 static bool LoadPlugins(String plugindir); 00109 }; 00110 00111 } // namespace LinuxSampler 00112 00113 #endif // LS_INSTRUMENT_EDITOR_FACTORY_H