composite_plugin_provider.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011, Dirk Thomas, TU Darmstadt
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  *   * Redistributions of source code must retain the above copyright
00010  *     notice, this list of conditions and the following disclaimer.
00011  *   * Redistributions in binary form must reproduce the above
00012  *     copyright notice, this list of conditions and the following
00013  *     disclaimer in the documentation and/or other materials provided
00014  *     with the distribution.
00015  *   * Neither the name of the TU Darmstadt nor the names of its
00016  *     contributors may be used to endorse or promote products derived
00017  *     from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00022  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00023  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00024  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00025  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00026  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00027  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00028  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00029  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00030  * POSSIBILITY OF SUCH DAMAGE.
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   // discover plugins from all providers
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       // TODO: add name of plugin provider to error message
00071       qCritical("CompositePluginProvider::discover() could not discover plugins from provider - runtime_error:\n%s", e.what());
00072       continue;
00073     }
00074     catch (...)
00075     {
00076       // TODO: add name of plugin provider to error message
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   // dispatch load to appropriate provider
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   // dispatch load to appropriate provider
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   // dispatch unload to appropriate provider
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 } // namespace


qt_gui_cpp
Author(s): Dirk Thomas
autogenerated on Fri Jan 3 2014 11:44:04