Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <qt_gui_cpp/composite_plugin_provider.h>
00034
00035 #include <stdexcept>
00036
00037 namespace qt_gui_cpp {
00038
00039 CompositePluginProvider::CompositePluginProvider(const QList<PluginProvider*>& plugin_providers)
00040 : PluginProvider()
00041 , plugin_providers_(plugin_providers)
00042 {}
00043
00044 CompositePluginProvider::~CompositePluginProvider()
00045 {
00046 for (QList<PluginProvider*>::iterator it = plugin_providers_.begin(); it != plugin_providers_.end(); it++)
00047 {
00048 delete *it;
00049 }
00050 }
00051
00052 void CompositePluginProvider::set_plugin_providers(const QList<PluginProvider*>& plugin_providers)
00053 {
00054
00055 for (QList<PluginProvider*>::iterator it = plugin_providers_.begin(); it != plugin_providers_.end(); it++)
00056 {
00057 delete *it;
00058 }
00059 plugin_providers_ = plugin_providers;
00060 }
00061
00062 QList<PluginDescriptor*> CompositePluginProvider::discover_descriptors(QObject* discovery_data)
00063 {
00064
00065 QList<PluginDescriptor*> descriptors;
00066 for (QList<PluginProvider*>::iterator it = plugin_providers_.begin(); it != plugin_providers_.end(); it++)
00067 {
00068 QList<PluginDescriptor*> sub_descriptors;
00069 try
00070 {
00071 sub_descriptors = (*it)->discover_descriptors(discovery_data);
00072 }
00073 catch (std::runtime_error e)
00074 {
00075
00076 qCritical("CompositePluginProvider::discover() could not discover plugins from provider - runtime_error:\n%s", e.what());
00077 continue;
00078 }
00079 catch (...)
00080 {
00081
00082 qCritical("CompositePluginProvider::discover() could not discover plugins from provider");
00083 continue;
00084 }
00085
00086 QSet<QString> plugin_ids;
00087 for (QList<PluginDescriptor*>::iterator jt = sub_descriptors.begin(); jt != sub_descriptors.end(); jt++)
00088 {
00089 PluginDescriptor* descriptor = *jt;
00090 descriptors.append(descriptor);
00091 plugin_ids.insert(descriptor->pluginId());
00092 }
00093 discovered_plugins_[*it] = plugin_ids;
00094 }
00095 return descriptors;
00096 }
00097
00098 void* CompositePluginProvider::load(const QString& plugin_id, PluginContext* plugin_context)
00099 {
00100
00101 for (QMap<PluginProvider*, QSet<QString> >::iterator it = discovered_plugins_.begin(); it != discovered_plugins_.end(); it++)
00102 {
00103 if (it.value().contains(plugin_id))
00104 {
00105 PluginProvider* plugin_provider = it.key();
00106 try
00107 {
00108 void* instance = plugin_provider->load(plugin_id, plugin_context);
00109 running_plugins_[instance] = plugin_provider;
00110 return instance;
00111 }
00112 catch (std::exception& e)
00113 {
00114 qWarning("CompositePluginProvider::load(%s) failed loading plugin (%s)", plugin_id.toStdString().c_str(), e.what());
00115 return 0;
00116 }
00117 }
00118 }
00119 return 0;
00120 }
00121
00122 Plugin* CompositePluginProvider::load_plugin(const QString& plugin_id, PluginContext* plugin_context)
00123 {
00124
00125 for (QMap<PluginProvider*, QSet<QString> >::iterator it = discovered_plugins_.begin(); it != discovered_plugins_.end(); it++)
00126 {
00127 if (it.value().contains(plugin_id))
00128 {
00129 PluginProvider* plugin_provider = it.key();
00130 try
00131 {
00132 Plugin* instance = plugin_provider->load_plugin(plugin_id, plugin_context);
00133 running_plugins_[instance] = plugin_provider;
00134 return instance;
00135 }
00136 catch (std::exception& e)
00137 {
00138
00139 }
00140 }
00141 }
00142 return 0;
00143 }
00144
00145 void CompositePluginProvider::unload(void* plugin_instance)
00146 {
00147
00148 QMap<void*, PluginProvider*>::iterator it = running_plugins_.find(plugin_instance);
00149 if (it != running_plugins_.end())
00150 {
00151 (*it)->unload(plugin_instance);
00152 running_plugins_.remove(it);
00153 return;
00154 }
00155 throw std::runtime_error("plugin_instance not found");
00156 }
00157
00158 void CompositePluginProvider::shutdown()
00159 {
00160 for (QList<PluginProvider*>::iterator it = plugin_providers_.begin(); it != plugin_providers_.end(); it++)
00161 {
00162 (*it)->shutdown();
00163 }
00164 }
00165
00166 }