multi_library_class_loader.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2012, 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 #ifndef CLASS_LOADER_MULTI_LIBRARY_CLASS_LOADER_H_DEFINED
00031 #define CLASS_LOADER_MULTI_LIBRARY_CLASS_LOADER_H_DEFINED
00032 
00033 #include "class_loader.h"
00034 #include <boost/thread.hpp>
00035 
00036 namespace class_loader
00037 {
00038 
00039 typedef std::string LibraryPath;
00040 typedef std::map<LibraryPath, class_loader::ClassLoader*> LibraryToClassLoaderMap;
00041 typedef std::vector<ClassLoader*> ClassLoaderVector;
00042 
00047 class MultiLibraryClassLoader
00048 {
00049   public:
00054     MultiLibraryClassLoader(bool enable_ondemand_loadunload);
00055 
00059     virtual ~MultiLibraryClassLoader();
00060 
00068     template <class Base>
00069     boost::shared_ptr<Base> createInstance(const std::string& class_name)
00070     {
00071       logDebug("class_loader::MultiLibraryClassLoader: Attempting to create instance of class type %s.", class_name.c_str());
00072       ClassLoaderVector active_loaders = getAllAvailableClassLoaders();
00073       for(unsigned int c = 0; c < active_loaders.size(); c++)
00074       {
00075         ClassLoader* current = active_loaders.at(c);
00076         if(current->isClassAvailable<Base>(class_name))
00077           return(current->createInstance<Base>(class_name));
00078       }
00079 
00080       throw(class_loader::CreateClassException("MultiLibraryClassLoader: Could not create object of class type " + class_name + " as no factory exists for it. Make sure that the library exists and was explicitly loaded through MultiLibraryClassLoader::loadLibrary()"));
00081     }
00082 
00091     template <class Base>
00092     boost::shared_ptr<Base> createInstance(const std::string& class_name, const std::string& library_path)
00093     {   
00094       ClassLoader* loader = getClassLoaderForLibrary(library_path);
00095       if(loader)
00096         return(loader->createInstance<Base>(class_name));
00097       else
00098         throw class_loader::NoClassLoaderExistsException("Could not create instance as there is no ClassLoader in MultiLibraryClassLoader bound to library " + library_path + " Ensure you called MultiLibraryClassLoader::loadLibrary()");
00099     }
00100 
00109     template <class Base>
00110     Base* createUnmanagedInstance(const std::string& class_name)
00111     {
00112       ClassLoaderVector active_loaders = getAllAvailableClassLoaders();
00113       for(unsigned int c = 0; c < active_loaders.size(); c++)
00114       {
00115         ClassLoader* current = active_loaders.at(c);
00116         if(current->isClassAvailable<Base>(class_name))
00117           return(current->createUnmanagedInstance<Base>(class_name));
00118       }
00119 
00120       throw(class_loader::CreateClassException("MultiLibraryClassLoader: Could not create class of type " + class_name));
00121     }
00122 
00131     template <class Base>
00132     Base* createUnmanagedInstance(const std::string& class_name, const std::string& library_path)
00133     {
00134       ClassLoader* loader = getClassLoaderForLibrary(library_path);
00135       if(loader)
00136         return(loader->createUnmanagedInstance<Base>(class_name));
00137       else
00138         throw class_loader::NoClassLoaderExistsException("Could not create instance as there is no ClassLoader in MultiLibraryClassLoader bound to library " + library_path + " Ensure you called MultiLibraryClassLoader::loadLibrary()");
00139     }
00140 
00147     template <class Base>
00148     bool isClassAvailable(const std::string& class_name)
00149     {
00150       std::vector<std::string> available_classes = getAvailableClasses<Base>();
00151       return(available_classes.end() != std::find(available_classes.begin(), available_classes.end(), class_name));
00152     }
00153 
00159     bool isLibraryAvailable(const std::string& library_path);
00160 
00166     template <class Base>
00167     std::vector<std::string> getAvailableClasses()
00168     {
00169       std::vector<std::string> available_classes;
00170       ClassLoaderVector loaders = getAllAvailableClassLoaders();
00171       for(unsigned int c = 0; c < loaders.size(); c++)
00172       {
00173         ClassLoader* current = loaders.at(c);
00174         std::vector<std::string> loader_classes = current->getAvailableClasses<Base>();
00175         available_classes.insert(available_classes.end(), loader_classes.begin(), loader_classes.end());
00176       }
00177       return(available_classes);
00178     }
00179 
00185     template <class Base>
00186     std::vector<std::string> getAvailableClassesForLibrary(const std::string& library_path)
00187     {
00188       ClassLoader* loader = getClassLoaderForLibrary(library_path);
00189       std::vector<std::string> available_classes;
00190       if(loader)
00191       {
00192         available_classes = loader->getAvailableClasses<Base>();
00193         return(available_classes);
00194       }
00195       else
00196         throw class_loader::NoClassLoaderExistsException("There is no ClassLoader in MultiLibraryClassLoader bound to library " + library_path + " Ensure you called MultiLibraryClassLoader::loadLibrary()");
00197     }
00198 
00203     std::vector<std::string> getRegisteredLibraries();
00204 
00209     void loadLibrary(const std::string& library_path);
00210 
00215     int unloadLibrary(const std::string& library_path);
00216 
00217 
00218   private:
00222     bool isOnDemandLoadUnloadEnabled(){return(enable_ondemand_loadunload_);}
00223 
00229     ClassLoader* getClassLoaderForLibrary(const std::string& library_path);
00230 
00234     ClassLoaderVector getAllAvailableClassLoaders();
00235 
00239     void shutdownAllClassLoaders();
00240 
00241   private:
00242     bool enable_ondemand_loadunload_;
00243     LibraryToClassLoaderMap active_class_loaders_;
00244     boost::mutex loader_mutex_;
00245 };
00246 
00247 
00248 }
00249 #endif


class_loader
Author(s): Mirza Shah
autogenerated on Sun Oct 5 2014 22:50:57