Robowflex  v0.1
Making MoveIt Easy
robowflex::IO::PluginManager Class Reference

A singleton class for dynamic loading classes through pluginlib. More...

#include <plugin.h>

Public Member Functions

 PluginManager (PluginManager const &)=delete
 
void operator= (PluginManager const &)=delete
 
template<typename T >
std::shared_ptr< T > loadPlugin (const std::string &package, const std::string &plugin)
 Load a plugin named plugin of type T. More...
 

Static Public Member Functions

static PluginManagergetInstance ()
 Get the singleton instance of PluginManager. More...
 
template<typename T >
static std::shared_ptr< T > load (const std::string &package, const std::string &plugin)
 Load a plugin named plugin of type T using the singleton instance. More...
 

Private Types

template<typename T >
using Loader = pluginlib::ClassLoader< T >
 A typed class loader. More...
 
template<typename T >
using LoaderPtr = std::shared_ptr< Loader< T > >
 A shared pointer to a typed class loader. More...
 
using BaseLoader = pluginlib::ClassLoaderBase
 The base class of the class loader. More...
 
using BaseLoaderPtr = std::shared_ptr< BaseLoader >
 A shared pointer to the base class of the class loader. More...
 

Private Member Functions

 PluginManager ()
 Constructor. More...
 
template<typename T >
LoaderPtr< T > getLoader (const std::string &package)
 Gets the plugin loader for a plugin type T. Grabs the loader from cached loaders if available, otherwise creates the plugin loader and caches it. More...
 

Private Attributes

std::mutex mutex_
 Class loading mutex. More...
 
std::map< std::pair< std::string, std::string >, BaseLoaderPtrloaders_
 Cached loaders. More...
 

Detailed Description

A singleton class for dynamic loading classes through pluginlib.

Definition at line 23 of file plugin.h.

Member Typedef Documentation

◆ BaseLoader

using robowflex::IO::PluginManager::BaseLoader = pluginlib::ClassLoaderBase
private

The base class of the class loader.

Definition at line 95 of file plugin.h.

◆ BaseLoaderPtr

A shared pointer to the base class of the class loader.

Definition at line 99 of file plugin.h.

◆ Loader

template<typename T >
using robowflex::IO::PluginManager::Loader = pluginlib::ClassLoader<T>
private

A typed class loader.

Template Parameters
TThe type of class to load via the plugin loader.

Definition at line 85 of file plugin.h.

◆ LoaderPtr

template<typename T >
using robowflex::IO::PluginManager::LoaderPtr = std::shared_ptr<Loader<T> >
private

A shared pointer to a typed class loader.

Template Parameters
TThe type of class to load via the plugin loader.

Definition at line 91 of file plugin.h.

Constructor & Destructor Documentation

◆ PluginManager() [1/2]

robowflex::IO::PluginManager::PluginManager ( PluginManager const &  )
delete

◆ PluginManager() [2/2]

robowflex::IO::PluginManager::PluginManager ( )
inlineprivate

Constructor.

Definition at line 103 of file plugin.h.

104  {
105  }

Member Function Documentation

◆ getInstance()

static PluginManager& robowflex::IO::PluginManager::getInstance ( )
inlinestatic

Get the singleton instance of PluginManager.

Returns
The singleton PluginManager.

Definition at line 33 of file plugin.h.

34  {
35  static PluginManager instance;
36  return instance;
37  }
PluginManager()
Constructor.
Definition: plugin.h:103

◆ getLoader()

template<typename T >
LoaderPtr<T> robowflex::IO::PluginManager::getLoader ( const std::string package)
inlineprivate

Gets the plugin loader for a plugin type T. Grabs the loader from cached loaders if available, otherwise creates the plugin loader and caches it.

Parameters
[in]packageROS package that exports class T.
Template Parameters
TThe type of plugin loader to get.
Returns
A plugin loader that loads plugins of type T.

Definition at line 115 of file plugin.h.

116  {
118 
119  // Need to possibly demangle type name...
120  const std::string &type = ROBOWFLEX_DEMANGLE(typeid(T).name());
121  auto key = std::make_pair(package, type);
122 
123  LoaderPtr<T> loader;
124 
125  auto cached = loaders_.find(key);
126  if (cached != loaders_.end())
127  loader = std::dynamic_pointer_cast<Loader<T>>(cached->second);
128  else
129  {
130  RBX_INFO("Creating Class Loader for type `%s` from package `%s`!", type, package);
131 
132  loader.reset(new pluginlib::ClassLoader<T>(package, type));
133  loaders_[key] = std::static_pointer_cast<BaseLoader>(loader);
134  }
135 
136  return loader;
137  }
std::map< std::pair< std::string, std::string >, BaseLoaderPtr > loaders_
Cached loaders.
Definition: plugin.h:140
std::mutex mutex_
Class loading mutex.
Definition: plugin.h:139
T lock(T... args)
#define RBX_INFO(fmt,...)
Output a info logging message.
Definition: log.h:118
#define ROBOWFLEX_DEMANGLE(x)
Definition: macros.h:114
T make_pair(T... args)

◆ load()

template<typename T >
static std::shared_ptr<T> robowflex::IO::PluginManager::load ( const std::string package,
const std::string plugin 
)
inlinestatic

Load a plugin named plugin of type T using the singleton instance.

Parameters
[in]packageROS package that exports plugin's base class T.
[in]pluginName of the plugin to load.
Template Parameters
TThe type of the plugin to load.
Returns
A shared pointer to the loaded plugin on success, nullptr on failure.

Definition at line 46 of file plugin.h.

47  {
48  return getInstance().loadPlugin<T>(package, plugin);
49  }
std::shared_ptr< T > loadPlugin(const std::string &package, const std::string &plugin)
Load a plugin named plugin of type T.
Definition: plugin.h:58
static PluginManager & getInstance()
Get the singleton instance of PluginManager.
Definition: plugin.h:33

◆ loadPlugin()

template<typename T >
std::shared_ptr<T> robowflex::IO::PluginManager::loadPlugin ( const std::string package,
const std::string plugin 
)
inline

Load a plugin named plugin of type T.

Parameters
[in]packageROS package that exports plugin's base class T.
[in]pluginName of the plugin to load.
Template Parameters
TThe type of the plugin to load.
Returns
A shared pointer to the loaded plugin on success, nullptr on failure.

Definition at line 58 of file plugin.h.

59  {
60  if (ros::package::getPath(package).empty())
61  {
62  RBX_ERROR("Package `%s` does not exist.", package);
63  return nullptr;
64  }
65 
66  auto loader = getLoader<T>(package);
67 
68  try
69  {
70  std::shared_ptr<T> loaded(loader->createUnmanagedInstance(plugin));
71  return loaded;
72  }
73  catch (pluginlib::LibraryLoadException &e)
74  {
75  RBX_ERROR("Failed to library: %s", e.what());
76  return nullptr;
77  }
78  }
#define RBX_ERROR(fmt,...)
Output a error logging message.
Definition: log.h:102

◆ operator=()

void robowflex::IO::PluginManager::operator= ( PluginManager const &  )
delete

Member Data Documentation

◆ loaders_

std::map<std::pair<std::string, std::string>, BaseLoaderPtr> robowflex::IO::PluginManager::loaders_
private

Cached loaders.

Definition at line 140 of file plugin.h.

◆ mutex_

std::mutex robowflex::IO::PluginManager::mutex_
private

Class loading mutex.

Definition at line 139 of file plugin.h.


The documentation for this class was generated from the following file: