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> 44 #include "console_bridge/console.h" 81 template<
typename Base>
84 template<
typename Base>
85 using UniquePtr = std::unique_ptr<Base, DeleterType<Base>>;
93 explicit ClassLoader(
const std::string & library_path,
bool ondemand_load_unload =
false);
108 return class_loader::impl::getAvailableClasses<Base>(
this);
129 return std::shared_ptr<Base>(
130 createRawInstance<Base>(derived_class_name,
true),
131 boost::bind(&ClassLoader::onPluginDeletion<Base>,
this, _1));
142 return boost::shared_ptr<Base>(
143 createRawInstance<Base>(derived_class_name,
true),
144 boost::bind(&ClassLoader::onPluginDeletion<Base>,
this, _1));
162 Base * raw = createRawInstance<Base>(derived_class_name,
true);
163 return std::unique_ptr<Base, DeleterType<Base>>(
165 boost::bind(&ClassLoader::onPluginDeletion<Base>,
this, _1));
183 return createRawInstance<Base>(derived_class_name,
false);
195 std::vector<std::string> available_classes = getAvailableClasses<Base>();
197 available_classes.begin(), available_classes.end(), class_name) != available_classes.end();
243 CONSOLE_BRIDGE_logDebug(
244 "class_loader::ClassLoader: Calling onPluginDeletion() for obj ptr = %p.\n",
245 reinterpret_cast<void *>(obj));
246 if (
nullptr == obj) {
257 CONSOLE_BRIDGE_logWarn(
258 "class_loader::ClassLoader: " 259 "Cannot unload library %s even though last shared pointer went out of scope. " 260 "This is because createUnmanagedInstance was used within the scope of this process," 261 " perhaps by a different ClassLoader. Library will NOT be closed.",
289 CONSOLE_BRIDGE_logInform(
"%s",
290 "class_loader::ClassLoader: " 291 "An attempt is being made to create a managed plugin instance (i.e. boost::shared_ptr), " 292 "however an unmanaged instance was created within this process address space. " 293 "This means libraries for the managed instances will not be shutdown automatically on " 294 "final plugin destruction if on demand (lazy) loading/unloading mode is used." 301 Base * obj = class_loader::impl::createInstance<Base>(derived_class_name,
this);
302 assert(obj !=
nullptr);
341 #endif // CLASS_LOADER__CLASS_LOADER_HPP_
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.
UniquePtr< Base > createUniqueInstance(const std::string &derived_class_name)
Generates an instance of loadable classes (i.e. class_loader).
CLASS_LOADER_PUBLIC 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_
CLASS_LOADER_PUBLIC std::string systemLibraryFormat(const std::string &library_name)
Returns a platform specific version of a basic library name.
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).
bool ondemand_load_unload_
CLASS_LOADER_PUBLIC bool isOnDemandLoadUnloadEnabled()
Indicates if the library is to be loaded/unloaded on demand...meaning that only to load a lib when th...
CLASS_LOADER_PUBLIC std::string systemLibrarySuffix()
Returns runtime library extension for native os.
CLASS_LOADER_PUBLIC 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...
CLASS_LOADER_PUBLIC int unloadLibraryInternal(bool lock_plugin_ref_count)
As the library may be unloaded in "on-demand load/unload" mode, unload maybe called from createInstan...
std::shared_ptr< Base > createSharedInstance(const std::string &derived_class_name)
Generates an instance of loadable classes (i.e. class_loader).
CLASS_LOADER_PUBLIC 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 isClassAvailable(const std::string &class_name)
Indicates if a plugin class is available.
CLASS_LOADER_PUBLIC int unloadLibrary()
Attempts to unload a library loaded within scope of the ClassLoader. If the library is not opened...
std::unique_ptr< Base, DeleterType< Base > > UniquePtr
std::function< void(Base *)> DeleterType
CLASS_LOADER_PUBLIC std::string getLibraryPath()
Gets the full-qualified path and name of the library associated with this class loader.
virtual CLASS_LOADER_PUBLIC ~ClassLoader()
Destructor for ClassLoader. All libraries opened by this ClassLoader are unloaded automatically...
#define CLASS_LOADER_PUBLIC
static CLASS_LOADER_PUBLIC 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).
CLASS_LOADER_PUBLIC std::string systemLibraryPrefix()
Returns the default library prefix for the native os.
CLASS_LOADER_PUBLIC void loadLibrary()
Attempts to load a library on behalf of the ClassLoader. If the library is already opened...
static CLASS_LOADER_PUBLIC bool hasUnmanagedInstanceBeenCreated()
Getter for if an unmanaged (i.e. unsafe) instance has been created flag.
boost::recursive_mutex load_ref_count_mutex_