knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
knowrob::PluginLibrary< T > Class Template Reference

#include <PluginLibrary.h>

Inheritance diagram for knowrob::PluginLibrary< T >:
Collaboration diagram for knowrob::PluginLibrary< T >:

Public Member Functions

 PluginLibrary (std::string_view dllPath)
 
 ~PluginLibrary () override
 
 PluginLibrary (const PluginLibrary &)=delete
 
bool isLoaded ()
 
bool loadDLL ()
 
std::shared_ptr< NamedPlugin< T > > create (std::string_view pluginID) override
 
std::string_view name () const override
 
 PluginLibrary (std::string_view dllPath)
 
 ~PluginLibrary () override
 
 PluginLibrary (const PluginLibrary &)=delete
 
bool isLoaded ()
 
bool loadDLL ()
 
std::shared_ptr< NamedPlugin< T > > create (std::string_view pluginID) override
 
std::string_view name () const override
 
- Public Member Functions inherited from knowrob::PluginFactory< T >
virtual ~PluginFactory ()=default
 
virtual ~PluginFactory ()=default
 

Protected Attributes

const std::string dllPath_
 
std::string name_
 
void * handle_
 
std::shared_ptr< T >(* create_ )()
 
char *(* get_name_ )()
 

Detailed Description

template<class T>
class knowrob::PluginLibrary< T >

A factory that uses a shared library for creation of plugin instances.

Definition at line 19 of file PluginLibrary.h.

Constructor & Destructor Documentation

◆ PluginLibrary() [1/4]

template<class T >
knowrob::PluginLibrary< T >::PluginLibrary ( std::string_view  dllPath)
inlineexplicit
Parameters
dllPaththe name or path of the shared library.

Definition at line 24 of file PluginLibrary.h.

25  : dllPath_(dllPath),
26  handle_(nullptr),
27  create_(nullptr),
28  get_name_(nullptr) {
29  }
const std::string dllPath_
Definition: PluginLibrary.h:73
std::shared_ptr< T >(* create_)()
Definition: PluginLibrary.h:82

◆ ~PluginLibrary() [1/2]

template<class T >
knowrob::PluginLibrary< T >::~PluginLibrary ( )
inlineoverride

Definition at line 31 of file PluginLibrary.h.

31  {
32  if (handle_) {
33  dlclose(handle_);
34  handle_ = nullptr;
35  }
36  }

◆ PluginLibrary() [2/4]

template<class T >
knowrob::PluginLibrary< T >::PluginLibrary ( const PluginLibrary< T > &  )
delete

Cannot be copy-assigned.

◆ PluginLibrary() [3/4]

template<class T >
knowrob::PluginLibrary< T >::PluginLibrary ( std::string_view  dllPath)
inlineexplicit
Parameters
dllPaththe name or path of the shared library.

Definition at line 24 of file PluginLibrary.h.

25  : dllPath_(dllPath),
26  handle_(nullptr),
27  create_(nullptr),
28  get_name_(nullptr) {
29  }

◆ ~PluginLibrary() [2/2]

template<class T >
knowrob::PluginLibrary< T >::~PluginLibrary ( )
inlineoverride

Definition at line 31 of file PluginLibrary.h.

31  {
32  if (handle_) {
33  dlclose(handle_);
34  handle_ = nullptr;
35  }
36  }

◆ PluginLibrary() [4/4]

template<class T >
knowrob::PluginLibrary< T >::PluginLibrary ( const PluginLibrary< T > &  )
delete

Cannot be copy-assigned.

Member Function Documentation

◆ create() [1/2]

template<class T >
std::shared_ptr<NamedPlugin<T> > knowrob::PluginLibrary< T >::create ( std::string_view  pluginID)
inlineoverridevirtual

Create a new plugin instance.

Parameters
pluginIDthe ID of the plugin.
Returns
the plugin created.

Implements knowrob::PluginFactory< T >.

Definition at line 68 of file PluginLibrary.h.

68  {
69  return std::make_shared<NamedPlugin<T>>(pluginID, PluginLanguage::CPP, create_());
70  }

◆ create() [2/2]

template<class T >
std::shared_ptr<NamedPlugin<T> > knowrob::PluginLibrary< T >::create ( std::string_view  pluginID)
inlineoverridevirtual

Create a new plugin instance.

Parameters
pluginIDthe ID of the plugin.
Returns
the plugin created.

Implements knowrob::PluginFactory< T >.

Definition at line 68 of file PluginLibrary.h.

68  {
69  return std::make_shared<NamedPlugin<T>>(pluginID, PluginLanguage::CPP, create_());
70  }

◆ isLoaded() [1/2]

template<class T >
bool knowrob::PluginLibrary< T >::isLoaded ( )
inline
Returns
true if the shared library was loaded successfully.

Definition at line 46 of file PluginLibrary.h.

46  {
47  return (create_ != nullptr && get_name_ != nullptr);
48  }

◆ isLoaded() [2/2]

template<class T >
bool knowrob::PluginLibrary< T >::isLoaded ( )
inline
Returns
true if the shared library was loaded successfully.

Definition at line 46 of file PluginLibrary.h.

46  {
47  return (create_ != nullptr && get_name_ != nullptr);
48  }

◆ loadDLL() [1/2]

template<class T >
bool knowrob::PluginLibrary< T >::loadDLL ( )
inline

Try loading the shared library from filesystem. Note that, on unix-based systems, the LD_LIBRARY_PATH environment variable is used to locate the library.

Returns
true on success.

Definition at line 56 of file PluginLibrary.h.

56  {
57  handle_ = dlopen(dllPath_.c_str(), RTLD_LAZY);
58  if (handle_ != nullptr) {
59  create_ = (std::shared_ptr<T> (*)()) dlsym(handle_, "knowrob_createPlugin");
60  get_name_ = (char *(*)()) dlsym(handle_, "knowrob_getPluginName");
61  return isLoaded();
62  } else {
63  return false;
64  }
65  }

◆ loadDLL() [2/2]

template<class T >
bool knowrob::PluginLibrary< T >::loadDLL ( )
inline

Try loading the shared library from filesystem. Note that, on unix-based systems, the LD_LIBRARY_PATH environment variable is used to locate the library.

Returns
true on success.

Definition at line 56 of file PluginLibrary.h.

56  {
57  handle_ = dlopen(dllPath_.c_str(), RTLD_LAZY);
58  if (handle_ != nullptr) {
59  create_ = (std::shared_ptr<T> (*)()) dlsym(handle_, "knowrob_createPlugin");
60  get_name_ = (char *(*)()) dlsym(handle_, "knowrob_getPluginName");
61  return isLoaded();
62  } else {
63  return false;
64  }
65  }

◆ name() [1/2]

template<class T >
std::string_view knowrob::PluginLibrary< T >::name ( ) const
inlineoverridevirtual
Returns
name of the plugin type for which the factory can create instances.

Implements knowrob::PluginFactory< T >.

Definition at line 73 of file PluginLibrary.h.

73 { return name_; };

◆ name() [2/2]

template<class T >
std::string_view knowrob::PluginLibrary< T >::name ( ) const
inlineoverridevirtual
Returns
name of the plugin type for which the factory can create instances.

Implements knowrob::PluginFactory< T >.

Definition at line 73 of file PluginLibrary.h.

73 { return name_; };

Member Data Documentation

◆ create_

template<class T >
std::shared_ptr< T >(* knowrob::PluginLibrary< T >::create_)()
protected

Definition at line 79 of file PluginLibrary.h.

◆ dllPath_

template<class T >
const std::string knowrob::PluginLibrary< T >::dllPath_
protected

Definition at line 76 of file PluginLibrary.h.

◆ get_name_

template<class T >
char *(* knowrob::PluginLibrary< T >::get_name_)()
protected

Definition at line 85 of file PluginLibrary.h.

◆ handle_

template<class T >
void * knowrob::PluginLibrary< T >::handle_
protected

Definition at line 79 of file PluginLibrary.h.

◆ name_

template<class T >
std::string knowrob::PluginLibrary< T >::name_
protected

Definition at line 77 of file PluginLibrary.h.


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