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 plugin_providers_ = plugin_providers;
00055 }
00056
00057 QList<PluginDescriptor*> CompositePluginProvider::discover_descriptors()
00058 {
00059
00060 QList<PluginDescriptor*> descriptors;
00061 for (QList<PluginProvider*>::iterator it = plugin_providers_.begin(); it != plugin_providers_.end(); it++)
00062 {
00063 QList<PluginDescriptor*> sub_descriptors;
00064 try
00065 {
00066 sub_descriptors = (*it)->discover_descriptors();
00067 }
00068 catch (std::runtime_error e)
00069 {
00070
00071 qCritical("CompositePluginProvider::discover() could not discover plugins from provider - runtime_error:\n%s", e.what());
00072 continue;
00073 }
00074 catch (...)
00075 {
00076
00077 qCritical("CompositePluginProvider::discover() could not discover plugins from provider");
00078 continue;
00079 }
00080
00081 QSet<QString> plugin_ids;
00082 for (QList<PluginDescriptor*>::iterator jt = sub_descriptors.begin(); jt != sub_descriptors.end(); jt++)
00083 {
00084 PluginDescriptor* descriptor = *jt;
00085 descriptors.append(descriptor);
00086 plugin_ids.insert(descriptor->pluginId());
00087 }
00088 discovered_plugins_[*it] = plugin_ids;
00089 }
00090 return descriptors;
00091 }
00092
00093 void* CompositePluginProvider::load(const QString& plugin_id, PluginContext* plugin_context)
00094 {
00095
00096 for (QMap<PluginProvider*, QSet<QString> >::iterator it = discovered_plugins_.begin(); it != discovered_plugins_.end(); it++)
00097 {
00098 if (it.value().contains(plugin_id))
00099 {
00100 PluginProvider* plugin_provider = it.key();
00101 try
00102 {
00103 void* instance = plugin_provider->load(plugin_id, plugin_context);
00104 running_plugins_[instance] = plugin_provider;
00105 return instance;
00106 }
00107 catch (std::exception& e)
00108 {
00109 qWarning("CompositePluginProvider::load(%s) failed loading plugin (%s)", plugin_id.toStdString().c_str(), e.what());
00110 return 0;
00111 }
00112 }
00113 }
00114 return 0;
00115 }
00116
00117 Plugin* CompositePluginProvider::load_plugin(const QString& plugin_id, PluginContext* plugin_context)
00118 {
00119
00120 for (QMap<PluginProvider*, QSet<QString> >::iterator it = discovered_plugins_.begin(); it != discovered_plugins_.end(); it++)
00121 {
00122 if (it.value().contains(plugin_id))
00123 {
00124 PluginProvider* plugin_provider = it.key();
00125 try
00126 {
00127 Plugin* instance = plugin_provider->load_plugin(plugin_id, plugin_context);
00128 running_plugins_[instance] = plugin_provider;
00129 return instance;
00130 }
00131 catch (std::exception& e)
00132 {
00133 qWarning("CompositePluginProvider::load_plugin(%s) failed loading plugin (%s)", plugin_id.toStdString().c_str(), e.what());
00134 return 0;
00135 }
00136 }
00137 }
00138 return 0;
00139 }
00140
00141 void CompositePluginProvider::unload(void* plugin_instance)
00142 {
00143
00144 QMap<void*, PluginProvider*>::iterator it = running_plugins_.find(plugin_instance);
00145 if (it != running_plugins_.end())
00146 {
00147 (*it)->unload(plugin_instance);
00148 running_plugins_.remove(it);
00149 return;
00150 }
00151 throw std::runtime_error("plugin_instance not found");
00152 }
00153
00154 }