32 #ifndef CLASS_LOADER__CLASS_LOADER_HPP_ 33 #define CLASS_LOADER__CLASS_LOADER_HPP_ 35 #include <boost/bind.hpp> 36 #include <boost/shared_ptr.hpp> 37 #include <boost/thread/recursive_mutex.hpp> 41 #include "console_bridge/console.h" 47 #if __cplusplus >= 201103L 69 #if __cplusplus >= 201103L 70 template<
typename Base>
71 using DeleterType = std::function<void(Base *)>;
73 template<
typename Base>
74 using UniquePtr = std::unique_ptr<Base, DeleterType<Base>>;
82 ClassLoader(
const std::string & library_path,
bool ondemand_load_unload =
false);
96 return class_loader::class_loader_private::getAvailableClasses<Base>(
this);
116 return boost::shared_ptr<Base>(
117 createRawInstance<Base>(derived_class_name,
true),
118 boost::bind(&ClassLoader::onPluginDeletion<Base>,
this, _1));
121 #if __cplusplus >= 201103L 135 UniquePtr<Base> createUniqueInstance(
const std::string & derived_class_name)
137 Base * raw = createRawInstance<Base>(derived_class_name,
true);
138 return std::unique_ptr<Base, DeleterType<Base>>(
140 boost::bind(&ClassLoader::onPluginDeletion<Base>,
this, _1));
159 return createRawInstance<Base>(derived_class_name,
false);
171 std::vector<std::string> available_classes = getAvailableClasses<Base>();
173 available_classes.begin(), available_classes.end(), class_name) != available_classes.end();
215 "class_loader::ClassLoader: Calling onPluginDeletion() for obj ptr = %p.\n", obj);
226 "class_loader::ClassLoader: " 227 "Cannot unload library %s even though last shared pointer went out of scope. " 228 "This is because createUnmanagedInstance was used within the scope of this process," 229 " perhaps by a different ClassLoader. Library will NOT be closed.",
259 "class_loader::ClassLoader: " 260 "An attempt is being made to create a managed plugin instance (i.e. boost::shared_ptr), " 261 "however an unmanaged instance was created within this process address space. " 262 "This means libraries for the managed instances will not be shutdown automatically on " 263 "final plugin destruction if on demand (lazy) loading/unloading mode is used." 271 class_loader::class_loader_private::createInstance<Base>(derived_class_name,
this);
307 #endif // CLASS_LOADER__CLASS_LOADER_HPP_ #define CONSOLE_BRIDGE_logWarn(fmt,...)
std::vector< std::string > getAvailableClasses()
Indicates which classes (i.e. class_loader) that can be loaded by this object.
void onPluginDeletion(Base *obj)
Callback method when a plugin created by this class loader is destroyed.
bool isLibraryLoaded()
Indicates if a library is loaded within the scope of this ClassLoader. Note that the library may alre...
boost::recursive_mutex plugin_ref_count_mutex_
std::string library_path_
Base * createUnmanagedInstance(const std::string &derived_class_name)
Generates an instance of loadable classes (i.e. class_loader).
Base * createRawInstance(const std::string &derived_class_name, bool managed)
Generates an instance of loadable classes (i.e. class_loader).
#define CONSOLE_BRIDGE_logDebug(fmt,...)
bool ondemand_load_unload_
std::string systemLibrarySuffix()
ClassLoader(const std::string &library_path, bool ondemand_load_unload=false)
Constructor for ClassLoader.
This class allows loading and unloading of dynamically linked libraries which contain class definitio...
int unloadLibraryInternal(bool lock_plugin_ref_count)
As the library may be unloaded in "on-demand load/unload" mode, unload maybe called from createInstan...
bool isLibraryLoadedByAnyClassloader()
Indicates if a library is loaded by some entity in the plugin system (another ClassLoader), but not necessarily loaded by this ClassLoader.
bool isOnDemandLoadUnloadEnabled()
Indicates if the library is to be loaded/unloaded on demand...meaning that only to load a lib when th...
bool isClassAvailable(const std::string &class_name)
Indicates if a plugin class is available.
int unloadLibrary()
Attempts to unload a library loaded within scope of the ClassLoader. If the library is not opened...
virtual ~ClassLoader()
Destructor for ClassLoader. All libraries opened by this ClassLoader are unloaded automatically...
static bool has_unmananged_instance_been_created_
boost::shared_ptr< Base > createInstance(const std::string &derived_class_name)
Generates an instance of loadable classes (i.e. class_loader).
std::string getLibraryPath()
Gets the full-qualified path and name of the library associated with this class loader.
void loadLibrary()
Attempts to load a library on behalf of the ClassLoader. If the library is already opened...
static bool hasUnmanagedInstanceBeenCreated()
Getter for if an unmanaged (i.e. unsafe) instance has been created flag.
boost::recursive_mutex load_ref_count_mutex_
#define CONSOLE_BRIDGE_logInform(fmt,...)