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 #ifndef PLUGINLIB_CLASS_LOADER_H 00030 #define PLUGINLIB_CLASS_LOADER_H 00031 00032 #include "ros/console.h" 00033 00034 #include "pluginlib/class_desc.h" 00035 00036 #include "Poco/ClassLoader.h" 00037 #include "ros/package.h" 00038 #include "tinyxml.h" 00039 #include <vector> 00040 #include <map> 00041 00042 00043 #include "boost/algorithm/string.hpp" 00044 #include "pluginlib/boost_fs_wrapper.h" 00045 00046 namespace pluginlib 00047 { 00052 class PluginlibException: public std::runtime_error 00053 { 00054 public: 00055 PluginlibException(const std::string error_desc) : std::runtime_error(error_desc) {} 00056 }; 00057 00062 class LibraryLoadException: public PluginlibException 00063 { 00064 public: 00065 LibraryLoadException(const std::string error_desc) : PluginlibException(error_desc) {} 00066 }; 00067 00072 class LibraryUnloadException: public PluginlibException 00073 { 00074 public: 00075 LibraryUnloadException(const std::string error_desc) : PluginlibException(error_desc) {} 00076 }; 00077 00082 class CreateClassException: public PluginlibException 00083 { 00084 public: 00085 CreateClassException(const std::string error_desc) : PluginlibException(error_desc) {} 00086 }; 00087 00095 class ClassLoaderBase 00096 { 00097 public: 00101 virtual ~ClassLoaderBase() {} 00102 00107 virtual std::vector<std::string> getDeclaredClasses() = 0; 00108 00113 virtual void refreshDeclaredClasses() = 0; 00114 00120 virtual std::string getName(const std::string& lookup_name) = 0; 00121 00127 virtual bool isClassAvailable(const std::string& lookup_name) = 0; 00128 00134 virtual std::string getClassType(const std::string& lookup_name) = 0; 00135 00141 virtual std::string getClassDescription(const std::string& lookup_name) = 0; 00142 00147 virtual std::string getBaseClassType() const = 0; 00148 00154 virtual std::string getClassPackage(const std::string& lookup_name) = 0; 00155 00161 virtual std::string getPluginManifestPath(const std::string& lookup_name) = 0; 00162 00168 virtual bool isClassLoaded(const std::string& lookup_name) = 0; 00169 00175 virtual void loadLibraryForClass(const std::string & lookup_name) = 0; 00176 00183 virtual int unloadLibraryForClass(const std::string& lookup_name) = 0; 00184 00189 virtual std::vector<std::string> getRegisteredLibraries() = 0; 00190 00196 virtual std::string getClassLibraryPath(const std::string& lookup_name) = 0; 00197 }; 00198 00203 template <class T> 00204 class ClassLoader: public ClassLoaderBase 00205 { 00206 private: 00207 typedef std::map<std::string, unsigned int> LibraryCountMap; 00208 00209 public: 00210 typedef typename std::map<std::string, ClassDesc>::iterator ClassMapIterator; 00211 00212 public: 00220 ClassLoader(std::string package, std::string base_class, std::string attrib_name = std::string("plugin")); 00221 00225 ~ClassLoader(); 00226 00231 std::vector<std::string> getDeclaredClasses(); 00232 00237 void refreshDeclaredClasses(); 00238 00244 std::string getName(const std::string& lookup_name); 00245 00251 bool isClassAvailable(const std::string& lookup_name); 00252 00258 std::string getClassType(const std::string& lookup_name); 00259 00265 std::string getClassDescription(const std::string& lookup_name); 00266 00271 std::string getBaseClassType() const; 00272 00278 std::string getClassPackage(const std::string& lookup_name); 00279 00285 std::string getPluginManifestPath(const std::string& lookup_name); 00286 00296 __attribute__((deprecated)) T* createClassInstance(const std::string& lookup_name, bool auto_load = true); 00297 00305 boost::shared_ptr<T> createInstance(const std::string& lookup_name); 00306 00315 T* createUnmanagedInstance(const std::string& lookup_name); 00316 00322 bool isClassLoaded(const std::string& lookup_name); 00323 00329 void loadLibraryForClass(const std::string & lookup_name); 00330 00337 int unloadLibraryForClass(const std::string& lookup_name); 00338 00343 std::vector<std::string> getRegisteredLibraries(); 00344 00350 std::string getClassLibraryPath(const std::string& lookup_name); 00351 00352 private: 00358 void garbageInstance(T* p, const std::string& lookup_name); 00359 00365 bool unloadClassLibrary(const std::string& library_path); 00366 00372 bool loadClassLibrary(const std::string& library_path); 00373 00379 std::vector<std::string> getClassesInLibrary(const std::string & library_path); 00380 00385 std::vector<std::string> getLoadedLibraries(); 00386 00392 void loadClassLibraryInternal(const std::string& library_path, const std::string& list_name_arg = std::string("")); 00393 00399 int unloadClassLibraryInternal(const std::string& library_path); 00400 00406 std::map<std::string, ClassDesc> determineAvailableClasses(); 00407 00413 std::string getErrorStringForUnknownClass(const std::string& lookup_name); 00414 00415 //used for proper unloading of automatically loaded libraries 00416 LibraryCountMap loaded_libraries_; 00417 00418 // map from library to class's descriptions 00419 // This is all available classes found in xml 00420 std::map<std::string, ClassDesc> classes_available_; 00421 std::string package_; 00422 std::string base_class_; 00423 std::string attrib_name_; 00424 00425 Poco::ClassLoader<T> poco_class_loader_; 00426 }; 00427 00428 }; 00429 00430 #include "class_loader_imp.h" 00431 00432 #endif //PLUGINLIB_CLASS_LOADER_H