Program Listing for File class_loader.hpp

Return to documentation for file (include/pluginlib/class_loader.hpp)

// Copyright 2009, Willow Garage, Inc. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//    * Redistributions of source code must retain the above copyright
//      notice, this list of conditions and the following disclaimer.
//
//    * Redistributions in binary form must reproduce the above copyright
//      notice, this list of conditions and the following disclaimer in the
//      documentation and/or other materials provided with the distribution.
//
//    * Neither the name of the Willow Garage nor the names of its
//      contributors may be used to endorse or promote products derived from
//      this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

#ifndef PLUGINLIB__CLASS_LOADER_HPP_
#define PLUGINLIB__CLASS_LOADER_HPP_

#include <map>
#include <memory>
#include <string>
#include <vector>

#include "class_loader/multi_library_class_loader.hpp"
#include "pluginlib/class_desc.hpp"
#include "pluginlib/class_loader_base.hpp"
#include "pluginlib/exceptions.hpp"
#include "tinyxml2.h"  // NOLINT

namespace pluginlib
{

template<typename T>
using UniquePtr = class_loader::ClassLoader::UniquePtr<T>;

template<class T>
class ClassLoader : public ClassLoaderBase
{
public:
  typedef typename std::map<std::string, ClassDesc>::iterator ClassMapIterator;

public:
  ClassLoader(
    std::string package,
    std::string base_class,
    std::string attrib_name = std::string("plugin"),
    std::vector<std::string> plugin_xml_paths = std::vector<std::string>());

  ~ClassLoader();


  std::shared_ptr<T> createSharedInstance(const std::string & lookup_name);


  UniquePtr<T> createUniqueInstance(const std::string & lookup_name);


  T * createUnmanagedInstance(const std::string & lookup_name);


  std::vector<std::string> getPluginXmlPaths();


  std::vector<std::string> getDeclaredClasses();


  virtual std::string getName(const std::string & lookup_name);


  virtual std::string getBaseClassType() const;


  virtual std::string getClassType(const std::string & lookup_name);


  virtual std::string getClassDescription(const std::string & lookup_name);


  virtual std::string getClassLibraryPath(const std::string & lookup_name);


  virtual std::string getClassPackage(const std::string & lookup_name);


  virtual std::string getPluginManifestPath(const std::string & lookup_name);


  virtual std::vector<std::string> getRegisteredLibraries();


  bool isClassLoaded(const std::string & lookup_name);


  virtual bool isClassAvailable(const std::string & lookup_name);


  virtual void loadLibraryForClass(const std::string & lookup_name);


  virtual void refreshDeclaredClasses();


  virtual int unloadLibraryForClass(const std::string & lookup_name);

private:

  std::vector<std::string> getPluginXmlPaths(
    const std::string & package,
    const std::string & attrib_name);


  std::map<std::string, ClassDesc> determineAvailableClasses(
    const std::vector<std::string> & plugin_xml_paths);


  std::string extractPackageNameFromPackageXML(const std::string & package_xml_path);


  std::vector<std::string> getAllLibraryPathsToTry(
    const std::string & library_name,
    const std::string & exporting_package_name);


  std::string getErrorStringForUnknownClass(const std::string & lookup_name);

  std::string getPathSeparator();

  std::string getPackageFromPluginXMLFilePath(const std::string & path);

  std::string joinPaths(const std::string & path1, const std::string & path2);


  void processSingleXMLPluginFile(
    const std::string & xml_file, std::map<std::string,
    ClassDesc> & class_available);


  std::string stripAllButFileFromPath(const std::string & path);



  int unloadClassLibraryInternal(const std::string & library_path);

private:
  std::vector<std::string> plugin_xml_paths_;
  // Map from library to class's descriptions described in XML.
  std::map<std::string, ClassDesc> classes_available_;
  std::string package_;
  std::string base_class_;
  std::string attrib_name_;
  class_loader::MultiLibraryClassLoader lowlevel_class_loader_;  // The underlying classloader
};

}  // namespace pluginlib

// Note: The implementation of the methods is in a separate file for clarity.
#include "./class_loader_imp.hpp"

#endif  // PLUGINLIB__CLASS_LOADER_HPP_