Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-12-24 02:18:55

0001 #ifndef FWCore_PluginManager_PluginManager_h
0002 #define FWCore_PluginManager_PluginManager_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     PluginManager
0006 // Class  :     PluginManager
0007 //
0008 /**\class PluginManager PluginManager.h FWCore/PluginManager/interface/PluginManager.h
0009 
0010  Description: Manages the loading of shared objects
0011 
0012  Usage:
0013     <usage>
0014 
0015 */
0016 //
0017 // Original Author:  Chris Jones
0018 //         Created:  Wed Apr  4 14:28:48 EDT 2007
0019 //
0020 
0021 // system include files
0022 #include <filesystem>
0023 #include <map>
0024 #include <memory>
0025 #include <mutex>
0026 #include <string>
0027 #include <vector>
0028 
0029 #include "oneapi/tbb/concurrent_unordered_map.h"
0030 
0031 // user include files
0032 #include "FWCore/Utilities/interface/Signal.h"
0033 #include "FWCore/PluginManager/interface/SharedLibrary.h"
0034 #include "FWCore/PluginManager/interface/PluginInfo.h"
0035 
0036 // forward declarations
0037 namespace edmplugin {
0038   class DummyFriend;
0039   class PluginFactoryBase;
0040 
0041   struct PluginManagerPathHasher {
0042     size_t operator()(std::filesystem::path const& iPath) const { return std::hash<std::string>{}(iPath.native()); }
0043   };
0044 
0045   class PluginManager {
0046     friend class DummyFriend;
0047 
0048   public:
0049     typedef std::vector<std::string> SearchPath;
0050     typedef std::vector<PluginInfo> Infos;
0051     typedef std::map<std::string, Infos> CategoryToInfos;
0052 
0053     class Config {
0054     public:
0055       Config() {}
0056       Config& searchPath(const SearchPath& iPath) {
0057         m_path = iPath;
0058         return *this;
0059       }
0060       const SearchPath& searchPath() const { return m_path; }
0061       void allowNoCache() { m_mustHaveCache = false; }
0062 
0063       bool mustHaveCache() const { return m_mustHaveCache; }
0064 
0065     private:
0066       SearchPath m_path;
0067       bool m_mustHaveCache = true;
0068     };
0069 
0070     PluginManager(const PluginManager&) = delete;                   // stop default
0071     const PluginManager& operator=(const PluginManager&) = delete;  // stop default
0072     ~PluginManager();
0073 
0074     // ---------- const member functions ---------------------
0075     const SharedLibrary& load(const std::string& iCategory, const std::string& iPlugin);
0076 
0077     const std::filesystem::path& loadableFor(const std::string& iCategory, const std::string& iPlugin);
0078 
0079     /**The container is ordered by category, then plugin name and then by precidence order of the plugin files.
0080         Therefore the first match on category and plugin name will be the proper file to load
0081         */
0082     const CategoryToInfos& categoryToInfos() const { return categoryToInfos_; }
0083 
0084     //If can not find iPlugin in category iCategory return null pointer, any other failure will cause a throw
0085     const SharedLibrary* tryToLoad(const std::string& iCategory, const std::string& iPlugin);
0086 
0087     // ---------- static member functions --------------------
0088     ///file name of the shared object being loaded
0089     static const std::string& loadingFile() { return loadingLibraryNamed_(); }
0090 
0091     ///if the value returned from loadingFile matches this string then the file is statically linked
0092     static const std::string& staticallyLinkedLoadingFileName();
0093 
0094     static PluginManager* get();
0095     static PluginManager& configure(const Config&);
0096 
0097     static bool isAvailable();
0098 
0099     // ---------- member functions ---------------------------
0100     edm::signalslot::Signal<void(const std::filesystem::path&)> goingToLoad_;
0101     edm::signalslot::Signal<void(const SharedLibrary&)> justLoaded_;
0102     edm::signalslot::Signal<void(const std::string&, const std::string&)> askedToLoadCategoryWithPlugin_;
0103 
0104   private:
0105     PluginManager(const Config&);
0106 
0107     void newFactory(const PluginFactoryBase*);
0108     static std::string& loadingLibraryNamed_();
0109     static PluginManager*& singleton();
0110 
0111     std::recursive_mutex& pluginLoadMutex() { return pluginLoadMutex_; }
0112 
0113     const std::filesystem::path& loadableFor_(const std::string& iCategory,
0114                                               const std::string& iPlugin,
0115                                               bool& ioThrowIfFailElseSucceedStatus);
0116     // ---------- member data --------------------------------
0117     SearchPath searchPath_;
0118     oneapi::tbb::concurrent_unordered_map<std::filesystem::path, std::shared_ptr<SharedLibrary>, PluginManagerPathHasher>
0119         loadables_;
0120 
0121     CategoryToInfos categoryToInfos_;
0122     std::recursive_mutex pluginLoadMutex_;
0123   };
0124 
0125 }  // namespace edmplugin
0126 #endif