32 #ifndef CLASS_LOADER__CLASS_LOADER_CORE_HPP_ 33 #define CLASS_LOADER__CLASS_LOADER_CORE_HPP_ 35 #include <boost/thread/recursive_mutex.hpp> 70 typedef std::map<ClassName, impl::AbstractMetaObjectBase *>
FactoryMap;
72 typedef std::pair<LibraryPath, Poco::SharedLibrary *>
LibraryPair;
137 template<
typename Base>
175 template<
typename Derived,
typename Base>
176 void registerPlugin(
const std::string & class_name,
const std::string & base_class_name)
181 CONSOLE_BRIDGE_logDebug(
182 "class_loader.impl: " 183 "Registering plugin factory for class = %s, ClassLoader* = %p and library name %s.",
188 CONSOLE_BRIDGE_logDebug(
"%s",
189 "class_loader.impl: ALERT!!! " 190 "A library containing plugins has been opened through a means other than through the " 191 "class_loader or pluginlib package. " 192 "This can happen if you build plugin libraries that contain more than just plugins " 193 "(i.e. normal code your app links against). " 194 "This inherently will trigger a dlopen() prior to main() and cause problems as class_loader " 195 "is not aware of plugin factories that autoregister under the hood. " 196 "The class_loader package can compensate, but you may run into namespace collision problems " 197 "(e.g. if you have the same plugin class in two different libraries and you load them both " 198 "at the same time). " 199 "The biggest problem is that library can now no longer be safely unloaded as the " 200 "ClassLoader does not know when non-plugin code is still in use. " 201 "In fact, no ClassLoader instance in your application will be unable to unload any library " 202 "once a non-pure one has been opened. " 203 "Please refactor your code to isolate plugins into their own libraries.");
216 FactoryMap & factoryMap = getFactoryMapForBaseClass<Base>();
217 if (factoryMap.find(class_name) != factoryMap.end()) {
218 CONSOLE_BRIDGE_logWarn(
219 "class_loader.impl: SEVERE WARNING!!! " 220 "A namespace collision has occurred with plugin factory for class %s. " 221 "New factory will OVERWRITE existing one. " 222 "This situation occurs when libraries containing plugins are directly linked against an " 223 "executable (the one running right now generating this message). " 224 "Please separate plugins out into their own library or just don't link against the library " 225 "and use either class_loader::ClassLoader/MultiLibraryClassLoader to open.",
228 factoryMap[class_name] = new_factory;
231 CONSOLE_BRIDGE_logDebug(
232 "class_loader.impl: " 233 "Registration of %s complete (Metaobject Address = %p)",
234 class_name.c_str(),
reinterpret_cast<void *
>(new_factory));
243 template<
typename Base>
249 FactoryMap & factoryMap = getFactoryMapForBaseClass<Base>();
250 if (factoryMap.find(derived_class_name) != factoryMap.end()) {
253 CONSOLE_BRIDGE_logError(
254 "class_loader.impl: No metaobject exists for class type %s.", derived_class_name.c_str());
258 Base * obj =
nullptr;
259 if (factory !=
nullptr && factory->
isOwnedBy(loader)) {
263 if (
nullptr == obj) {
264 if (factory && factory->
isOwnedBy(
nullptr)) {
265 CONSOLE_BRIDGE_logDebug(
"%s",
266 "class_loader.impl: ALERT!!! " 267 "A metaobject (i.e. factory) exists for desired class, but has no owner. " 268 "This implies that the library containing the class was dlopen()ed by means other than " 269 "through the class_loader interface. " 270 "This can happen if you build plugin libraries that contain more than just plugins " 271 "(i.e. normal code your app links against) -- that intrinsically will trigger a dlopen() " 273 "You should isolate your plugins into their own library, otherwise it will not be " 274 "possible to shutdown the library!");
279 "Could not create instance of type " + derived_class_name);
283 CONSOLE_BRIDGE_logDebug(
284 "class_loader.impl: Created instance of type %s and object pointer = %p",
285 (
typeid(obj).name()), reinterpret_cast<void *>(obj));
295 template<
typename Base>
300 FactoryMap & factory_map = getFactoryMapForBaseClass<Base>();
301 std::vector<std::string> classes;
302 std::vector<std::string> classes_with_no_owner;
304 for (
auto & it : factory_map) {
307 classes.push_back(it.first);
308 }
else if (factory->
isOwnedBy(
nullptr)) {
309 classes_with_no_owner.push_back(it.first);
315 classes.insert(classes.end(), classes_with_no_owner.begin(), classes_with_no_owner.end());
363 #endif // CLASS_LOADER__CLASS_LOADER_CORE_HPP_
CLASS_LOADER_PUBLIC void loadLibrary(const std::string &library_path, ClassLoader *loader)
Loads a library into memory if it has not already been done so. Attempting to load an already loaded ...
CLASS_LOADER_PUBLIC void hasANonPurePluginLibraryBeenOpened(bool hasIt)
Sets a flag indicating if a library containing more than just plugins has been opened by the running ...
CLASS_LOADER_PUBLIC ClassLoader * getCurrentlyActiveClassLoader()
Gets the ClassLoader currently in scope which used when a library is being loaded.
CLASS_LOADER_PUBLIC LibraryVector & getLoadedLibraryVector()
Gets a handle to a list of open libraries in the form of LibraryPairs which encode the library path+n...
CLASS_LOADER_PUBLIC std::string getCurrentlyLoadingLibraryName()
When a library is being loaded, in order for factories to know which library they are being associate...
CLASS_LOADER_PUBLIC void unloadLibrary(const std::string &library_path, ClassLoader *loader)
Unloads a library if it loaded in memory and cleans up its corresponding class factories. If it is not loaded, the function has no effect.
An exception class thrown when class_loader is unable to create a plugin.
This class allows loading and unloading of dynamically linked libraries which contain class definitio...
CLASS_LOADER_PUBLIC boost::recursive_mutex & getLoadedLibraryVectorMutex()
To provide thread safety, all exposed plugin functions can only be run serially by multiple threads...
std::pair< LibraryPath, Poco::SharedLibrary * > LibraryPair
CLASS_LOADER_PUBLIC boost::recursive_mutex & getPluginBaseToFactoryMapMapMutex()
std::string BaseClassName
CLASS_LOADER_PUBLIC std::vector< std::string > getAllLibrariesUsedByClassLoader(const ClassLoader *loader)
This function returns the names of all libraries in use by a given class loader.
CLASS_LOADER_PUBLIC bool isLibraryLoaded(const std::string &library_path, ClassLoader *loader)
Indicates if passed library loaded within scope of a ClassLoader. The library maybe loaded in memory...
std::map< ClassName, impl::AbstractMetaObjectBase * > FactoryMap
FactoryMap & getFactoryMapForBaseClass()
Same as above but uses a type parameter instead of string for more safety if info is available...
CLASS_LOADER_PUBLIC BaseToFactoryMapMap & getGlobalPluginBaseToFactoryMapMap()
Gets a handle to a global data structure that holds a map of base class names (Base class describes p...
std::vector< LibraryPair > LibraryVector
#define CLASS_LOADER_PUBLIC
std::map< BaseClassName, FactoryMap > BaseToFactoryMapMap
void registerPlugin(const std::string &class_name, const std::string &base_class_name)
This function is called by the CLASS_LOADER_REGISTER_CLASS macro in plugin_register_macro.h to register factories. Classes that use that macro will cause this function to be invoked when the library is loaded. The function will create a MetaObject (i.e. factory) for the corresponding Derived class and insert it into the appropriate FactoryMap in the global Base-to-FactoryMap map. Note that the passed class_name is the literal class name and not the mangled version.
std::vector< AbstractMetaObjectBase * > MetaObjectVector
Base * createInstance(const std::string &derived_class_name, ClassLoader *loader)
This function creates an instance of a plugin class given the derived name of the class and returns a...
CLASS_LOADER_PUBLIC void setCurrentlyLoadingLibraryName(const std::string &library_name)
When a library is being loaded, in order for factories to know which library they are being associate...
CLASS_LOADER_PUBLIC void setCurrentlyActiveClassLoader(ClassLoader *loader)
Sets the ClassLoader currently in scope which used when a library is being loaded.
CLASS_LOADER_PUBLIC bool isLibraryLoadedByAnybody(const std::string &library_path)
Indicates if passed library has been loaded by ANY ClassLoader.
std::vector< std::string > getAvailableClasses(ClassLoader *loader)
This function returns all the available class_loader in the plugin system that are derived from Base ...
CLASS_LOADER_PUBLIC void printDebugInfoToScreen()