loader.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2009, Willow Garage, Inc.
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 are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include <ros/types.h>
00031 
00032 #include "rve_pluginloader/loader.h"
00033 #include "rve_pluginloader/plugin.h"
00034 
00035 #include <ros/assert.h>
00036 #include <ros/package.h>
00037 
00038 namespace rve_pluginloader
00039 {
00040 
00041 static L_Plugin g_plugins;
00042 static int32_t g_init_count = 0;
00043 
00044 void init()
00045 {
00046   ++g_init_count;
00047 }
00048 
00049 void shutdown()
00050 {
00051   if (--g_init_count == 0)
00052   {
00053     g_plugins.clear();
00054   }
00055 }
00056 
00057 const L_Plugin& getPlugins()
00058 {
00059   return g_plugins;
00060 }
00061 
00062 void loadDescriptions(const std::string& registry_package)
00063 {
00064   ros::package::V_string plugins;
00065   ros::package::getPlugins(registry_package, "plugin", plugins);
00066 
00067   ros::package::V_string::iterator it = plugins.begin();
00068   ros::package::V_string::iterator end = plugins.end();
00069   for (; it != end; ++it)
00070   {
00071     const std::string& plugin = *it;
00072     loadDescription(plugin);
00073   }
00074 }
00075 
00076 void loadAllPlugins(const std::string& registry_package)
00077 {
00078   loadDescriptions(registry_package);
00079   L_Plugin::iterator it = g_plugins.begin();
00080   L_Plugin::iterator end = g_plugins.end();
00081   for (; it != end; ++it)
00082   {
00083     const PluginPtr& plugin = *it;
00084     try
00085     {
00086       plugin->load();
00087     }
00088     catch (std::runtime_error& e)
00089     {
00090       ROS_ERROR("Error loading plugin [%s]: %s", plugin->getDescriptionPath().c_str(), e.what());
00091     }
00092   }
00093 }
00094 
00095 PluginPtr getPlugin(const std::string& description_file)
00096 {
00097   L_Plugin::iterator it = g_plugins.begin();
00098   L_Plugin::iterator end = g_plugins.end();
00099   for (; it != end; ++it)
00100   {
00101     const PluginPtr& plugin = *it;
00102 
00103     if (plugin->getDescriptionPath() == description_file)
00104     {
00105       return plugin;
00106     }
00107   }
00108 
00109   return PluginPtr();
00110 }
00111 
00112 PluginPtr getPluginByPackage(const std::string& package)
00113 {
00114   L_Plugin::iterator it = g_plugins.begin();
00115   L_Plugin::iterator end = g_plugins.end();
00116   for (; it != end; ++it)
00117   {
00118     const PluginPtr& plugin = *it;
00119 
00120     if (plugin->getPackageName() == package)
00121     {
00122       return plugin;
00123     }
00124   }
00125 
00126   return PluginPtr();
00127 }
00128 
00129 PluginPtr loadDescription(const std::string& description_path)
00130 {
00131   PluginPtr plugin = getPlugin(description_path);
00132   if (plugin)
00133   {
00134     return plugin;
00135   }
00136 
00137   plugin.reset(new Plugin);
00138 
00139   try
00140   {
00141     plugin->loadDescription(description_path);
00142   }
00143   catch (std::runtime_error& e)
00144   {
00145     ROS_ERROR("Error loading plugin description [%s]: %s", description_path.c_str(), e.what());
00146   }
00147 
00148   g_plugins.push_back(plugin);
00149 
00150   return plugin;
00151 }
00152 
00153 PluginPtr loadPlugin(const std::string& description_file)
00154 {
00155   PluginPtr plugin = loadDescription(description_file);
00156   plugin->load();
00157   return plugin;
00158 }
00159 
00160 PluginPtr getPluginForClass(const std::string& base_class, const std::string& derived_class)
00161 {
00162   L_Plugin::iterator it = g_plugins.begin();
00163   L_Plugin::iterator end = g_plugins.end();
00164   for (; it != end; ++it)
00165   {
00166     const PluginPtr& plugin = *it;
00167     if (plugin->hasClass(base_class, derived_class))
00168     {
00169       return plugin;
00170     }
00171   }
00172 
00173   return PluginPtr();
00174 }
00175 
00176 void* create(const std::string& base_class, const std::string& derived_class)
00177 {
00178   PluginPtr plugin = getPluginForClass(base_class, derived_class);
00179   if (!plugin)
00180   {
00181     return 0;
00182   }
00183 
00184   return plugin->create(base_class, derived_class);
00185 }
00186 
00187 boost::shared_ptr<void> createShared(const std::string& base_class, const std::string& derived_class)
00188 {
00189   PluginPtr plugin = getPluginForClass(base_class, derived_class);
00190   if (!plugin)
00191   {
00192     return boost::shared_ptr<void>();
00193   }
00194 
00195   return plugin->createShared(base_class, derived_class);
00196 }
00197 
00198 void destroy(void* mem)
00199 {
00200   L_Plugin::iterator it = g_plugins.begin();
00201   L_Plugin::iterator end = g_plugins.end();
00202   for (; it != end; ++it)
00203   {
00204     const PluginPtr& plugin = *it;
00205     if (plugin->ownsAllocation(mem))
00206     {
00207       plugin->destroy(mem);
00208       return;
00209     }
00210   }
00211 
00212   ROS_BREAK();
00213 }
00214 
00215 }


rve_pluginloader
Author(s): Josh Faust
autogenerated on Wed Dec 11 2013 14:31:46