class_loader
master
The class_loader package is a ROS-independent package for loading plugins during runtime.
|
Go to the documentation of this file.
32 #ifndef CLASS_LOADER__CLASS_LOADER_HPP_
33 #define CLASS_LOADER__CLASS_LOADER_HPP_
47 # pragma clang diagnostic push
48 # pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
50 #include "console_bridge/console.h"
52 # pragma clang diagnostic pop
82 template<
typename Base>
85 template<
typename Base>
112 return class_loader::impl::getAvailableClasses<Base>(
this);
128 createRawInstance<Base>(derived_class_name,
true),
129 std::bind(&ClassLoader::onPluginDeletion<Base>,
this, std::placeholders::_1)
149 Base * raw = createRawInstance<Base>(derived_class_name,
true);
152 std::bind(&ClassLoader::onPluginDeletion<Base>,
this, std::placeholders::_1)
172 return createRawInstance<Base>(derived_class_name,
false);
187 available_classes.
begin(), available_classes.
end(), class_name) != available_classes.
end();
262 void onPluginDeletion(Base * obj)
264 CONSOLE_BRIDGE_logDebug(
265 "class_loader::ClassLoader: Calling onPluginDeletion() for obj ptr = %p.\n",
266 reinterpret_cast<void *
>(obj));
267 if (
nullptr == obj) {
272 assert(plugin_ref_count_ > 0);
275 if (!ClassLoader::hasUnmanagedInstanceBeenCreated()) {
276 unloadLibraryInternal(
false);
278 CONSOLE_BRIDGE_logWarn(
279 "class_loader::ClassLoader: "
280 "Cannot unload library %s even though last shared pointer went out of scope. "
281 "This is because createUnmanagedInstance was used within the scope of this process, "
282 "perhaps by a different ClassLoader. "
283 "Library will NOT be closed.",
303 Base * createRawInstance(
const std::string & derived_class_name,
bool managed)
306 this->setUnmanagedInstanceBeenCreated(
true);
311 ClassLoader::hasUnmanagedInstanceBeenCreated() &&
314 CONSOLE_BRIDGE_logInform(
316 "class_loader::ClassLoader: "
317 "An attempt is being made to create a managed plugin instance (i.e. boost::shared_ptr), "
318 "however an unmanaged instance was created within this process address space. "
319 "This means libraries for the managed instances will not be shutdown automatically on "
320 "final plugin destruction if on demand (lazy) loading/unloading mode is used."
327 Base * obj = class_loader::impl::createInstance<Base>(derived_class_name,
this);
342 static bool hasUnmanagedInstanceBeenCreated();
345 static void setUnmanagedInstanceBeenCreated(
bool state);
358 int unloadLibraryInternal(
bool lock_plugin_ref_count);
361 bool ondemand_load_unload_;
365 int plugin_ref_count_;
367 static bool has_unmananged_instance_been_created_;
373 #endif // CLASS_LOADER__CLASS_LOADER_HPP_
virtual ~ClassLoader()
Destructor for ClassLoader. All libraries opened by this ClassLoader are unloaded automatically.
bool isOnDemandLoadUnloadEnabled() const
Indicates if the library is to be loaded/unloaded on demand...meaning that only to load a lib when th...
Definition: class_loader.hpp:59
Base * createUnmanagedInstance(const std::string &derived_class_name)
Generates an instance of loadable classes (i.e. class_loader).
Definition: class_loader.hpp:170
std::string systemLibraryFormat(const std::string &library_name)
Returns a platform specific version of a basic library name.
std::shared_ptr< Base > createInstance(const std::string &derived_class_name)
Generates an instance of loadable classes (i.e. class_loader).
Definition: class_loader.hpp:125
bool isLibraryLoadedByAnyClassloader() const
Indicates if a library is loaded by some entity in the plugin system (another ClassLoader),...
const std::string & getLibraryPath() const
Gets the full-qualified path and name of the library associated with this class loader.
std::vector< std::string > getAvailableClasses() const
Indicates which classes (i.e. class_loader) that can be loaded by this object.
Definition: class_loader.hpp:110
bool isClassAvailable(const std::string &class_name) const
Indicates if a plugin class is available.
Definition: class_loader.hpp:183
void loadLibrary()
Attempts to load a library on behalf of the ClassLoader. If the library is already opened,...
int unloadLibrary()
Attempts to unload a library loaded within scope of the ClassLoader. If the library is not opened,...
This class allows loading and unloading of dynamically linked libraries which contain class definitio...
Definition: class_loader.hpp:79
#define CLASS_LOADER_PUBLIC
Definition: visibility_control.hpp:58
UniquePtr< Base > createUniqueInstance(const std::string &derived_class_name)
Generates an instance of loadable classes (i.e. class_loader).
Definition: class_loader.hpp:147
ClassLoader(const std::string &library_path, bool ondemand_load_unload=false)
Constructor for ClassLoader.
bool isLibraryLoaded() const
Indicates if a library is loaded within the scope of this ClassLoader.