Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:12:59

0001 // -*- C++ -*-
0002 //
0003 // Package:     PluginManager
0004 // Class  :     PluginCapabilities
0005 //
0006 // Implementation:
0007 //     <Notes on implementation>
0008 //
0009 // Original Author:  Chris Jones
0010 //         Created:  Fri Apr  6 12:36:24 EDT 2007
0011 //
0012 
0013 // system include files
0014 
0015 // user include files
0016 #include "FWCore/PluginManager/interface/PluginCapabilities.h"
0017 #include "FWCore/PluginManager/interface/SharedLibrary.h"
0018 #include "FWCore/PluginManager/interface/PluginManager.h"
0019 #include "FWCore/Utilities/interface/Exception.h"
0020 #include "FWCore/Utilities/interface/thread_safety_macros.h"
0021 
0022 namespace edmplugin {
0023   //
0024   // constants, enums and typedefs
0025   //
0026 
0027   //
0028   // static data member definitions
0029   //
0030 
0031   //
0032   // constructors and destructor
0033   //
0034   PluginCapabilities::PluginCapabilities() { finishedConstruction(); }
0035 
0036   // PluginCapabilities::PluginCapabilities(const PluginCapabilities& rhs)
0037   // {
0038   //    // do actual copying here;
0039   // }
0040 
0041   PluginCapabilities::~PluginCapabilities() {}
0042 
0043   //
0044   // assignment operators
0045   //
0046   // const PluginCapabilities& PluginCapabilities::operator=(const PluginCapabilities& rhs)
0047   // {
0048   //   //An exception safe implementation is
0049   //   PluginCapabilities temp(rhs);
0050   //   swap(rhs);
0051   //
0052   //   return *this;
0053   // }
0054 
0055   //
0056   // member functions
0057   //
0058   bool PluginCapabilities::tryToFind(const SharedLibrary& iLoadable) {
0059     void* sym;
0060     if (not iLoadable.symbol("SEAL_CAPABILITIES", sym)) {
0061       return false;
0062     }
0063 
0064     const char** names;
0065     int size;
0066     //reinterpret_cast<void (*)(const char**&,int&)>(sym)(names,size);
0067     reinterpret_cast<void (*)(const char**&, int&)>(reinterpret_cast<unsigned long>(sym))(names, size);
0068 
0069     PluginInfo info;
0070     for (int i = 0; i < size; ++i) {
0071       std::string name(names[i]);
0072       classToLoadable_[name] = iLoadable.path();
0073 
0074       //NOTE: can't use newPlugin(name) to do the work since it assumes
0075       //  we haven't yet returned from PluginManager::load method
0076       info.name_ = name;
0077       info.loadable_ = iLoadable.path();
0078       this->newPluginAdded_(category(), info);
0079     }
0080     return true;
0081   }
0082 
0083   void PluginCapabilities::load(const std::string& iName) {
0084     if (classToLoadable_.end() == classToLoadable_.find(iName)) {
0085       const SharedLibrary& lib = PluginManager::get()->load(category(), iName);
0086       //read the items from the 'capabilities' symbol
0087       if (not tryToFind(lib)) {
0088         throw cms::Exception("PluginNotFound")
0089             << "The dictionary for class '" << iName << "' is supposed to be in file\n '" << lib.path().string()
0090             << "'\n but no dictionaries are in that file.\n"
0091                "It appears like the cache is wrong.  Please do 'EdmPluginRefresh "
0092             << lib.path().string() << "'.";
0093       }
0094 
0095       if (classToLoadable_.end() == classToLoadable_.find(iName)) {
0096         throw cms::Exception("PluginNotFound")
0097             << "The dictionary for class '" << iName << "' is supposed to be in file\n '" << lib.path().string()
0098             << "'\n but was not found.\n"
0099                "It appears like the cache is wrong.  Please do 'EdmPluginRefresh "
0100             << lib.path().string() << "'.";
0101       }
0102     }
0103   }
0104 
0105   bool PluginCapabilities::tryToLoad(const std::string& iName) {
0106     if (classToLoadable_.end() == classToLoadable_.find(iName)) {
0107       const SharedLibrary* lib = PluginManager::get()->tryToLoad(category(), iName);
0108       if (nullptr == lib) {
0109         return false;
0110       }
0111       //read the items from the 'capabilities' symbol
0112       if (not tryToFind(*lib)) {
0113         throw cms::Exception("PluginNotFound")
0114             << "The dictionary for class '" << iName << "' is supposed to be in file\n '" << lib->path().string()
0115             << "'\n but no dictionaries are in that file.\n"
0116                "It appears like the cache is wrong.  Please do 'EdmPluginRefresh "
0117             << lib->path().string() << "'.";
0118       }
0119 
0120       if (classToLoadable_.end() == classToLoadable_.find(iName)) {
0121         throw cms::Exception("PluginNotFound")
0122             << "The dictionary for class '" << iName << "' is supposed to be in file\n '" << lib->path().string()
0123             << "'\n but was not found.\n"
0124                "It appears like the cache is wrong.  Please do 'EdmPluginRefresh "
0125             << lib->path().string() << "'.";
0126       }
0127     }
0128     return true;
0129   }
0130   //
0131   // const member functions
0132   //
0133   std::vector<PluginInfo> PluginCapabilities::available() const {
0134     PluginInfo info;
0135     std::vector<PluginInfo> infos;
0136     infos.reserve(classToLoadable_.size());
0137 
0138     for (std::map<std::string, std::filesystem::path>::const_iterator it = classToLoadable_.begin();
0139          it != classToLoadable_.end();
0140          ++it) {
0141       info.name_ = it->first;
0142       info.loadable_ = it->second;
0143       infos.push_back(info);
0144     }
0145     return infos;
0146   }
0147 
0148   const std::string& PluginCapabilities::category() const {
0149     static const std::string s_cat("Capability");
0150     return s_cat;
0151   }
0152 
0153   //
0154   // static member functions
0155   //
0156   PluginCapabilities* PluginCapabilities::get() {
0157     CMS_THREAD_SAFE static PluginCapabilities s_instance;
0158     return &s_instance;
0159   }
0160 
0161 }  // namespace edmplugin