nodelet_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 "nodelet_plugin_provider.h"
00034 
00035 #include "roscpp_plugin_provider.h"
00036 
00037 #include <stdexcept>
00038 #include <boost/bind.hpp>
00039 
00040 namespace rqt_gui_cpp {
00041 
00042 NodeletPluginProvider::NodeletPluginProvider(const QString& export_tag, const QString& base_class_type)
00043   : qt_gui_cpp::RosPluginlibPluginProvider<rqt_gui_cpp::Plugin>(export_tag, base_class_type)
00044   , loader_(0)
00045 {}
00046 
00047 NodeletPluginProvider::~NodeletPluginProvider()
00048 {
00049   if (loader_)
00050   {
00051     delete loader_;
00052   }
00053 }
00054 
00055 void NodeletPluginProvider::unload(void* instance)
00056 {
00057   qDebug("NodeletPluginProvider::unload()");
00058   if (!instances_.contains(instance))
00059   {
00060     qCritical("NodeletPluginProvider::unload() instance not found");
00061     return;
00062   }
00063 
00064   QString nodelet_name = instances_[instance];
00065   bool unloaded = loader_->unload(nodelet_name.toStdString());
00066   if (!unloaded)
00067   {
00068     qCritical("NodeletPluginProvider::unload() '%s' failed", nodelet_name.toStdString().c_str());
00069   }
00070 
00071   qt_gui_cpp::RosPluginlibPluginProvider<rqt_gui_cpp::Plugin>::unload(instance);
00072 }
00073 
00074 void NodeletPluginProvider::init_loader()
00075 {
00076   // initialize nodelet Loader once
00077   if (loader_ == 0)
00078   {
00079     loader_ = new nodelet::Loader(boost::bind(&NodeletPluginProvider::create_instance, this, _1));
00080   }
00081 }
00082 
00083 boost::shared_ptr<Plugin> NodeletPluginProvider::create_plugin(const std::string& lookup_name, qt_gui_cpp::PluginContext* plugin_context)
00084 {
00085   init_loader();
00086 
00087   nodelet::M_string remappings;
00088   nodelet::V_string my_argv;
00089   std::string nodelet_name = lookup_name + "_" + QString::number(plugin_context->serialNumber()).toStdString();
00090   instance_.reset();
00091   qDebug("NodeletPluginProvider::create_plugin() load %s", lookup_name.c_str());
00092   bool loaded = loader_->load(nodelet_name, lookup_name, remappings, my_argv);
00093   if (loaded)
00094   {
00095     qDebug("NodeletPluginProvider::create_plugin() loaded");
00096     instances_[&*instance_] = nodelet_name.c_str();
00097   }
00098   boost::shared_ptr<rqt_gui_cpp::Plugin> instance = instance_;
00099   instance_.reset();
00100   return instance;
00101 }
00102 
00103 boost::shared_ptr<nodelet::Nodelet> NodeletPluginProvider::create_instance(const std::string& lookup_name)
00104 {
00105   instance_ = qt_gui_cpp::RosPluginlibPluginProvider<rqt_gui_cpp::Plugin>::create_plugin(lookup_name);
00106   return instance_;
00107 }
00108 
00109 void NodeletPluginProvider::init_plugin(const QString& plugin_id, qt_gui_cpp::PluginContext* plugin_context, qt_gui_cpp::Plugin* plugin)
00110 {
00111   qDebug("NodeletPluginProvider::init_plugin()");
00112 
00113   rqt_gui_cpp::Plugin* nodelet = dynamic_cast<rqt_gui_cpp::Plugin*>(plugin);
00114   if (!nodelet)
00115   {
00116     throw std::runtime_error("plugin is not a nodelet");
00117   }
00118 
00119   qt_gui_cpp::RosPluginlibPluginProvider<rqt_gui_cpp::Plugin>::init_plugin(plugin_id, plugin_context, plugin);
00120 }
00121 
00122 }


rqt_gui_cpp
Author(s): Dirk Thomas
autogenerated on Fri Jan 3 2014 11:54:08