CMS 3D CMS Logo

PluginFactoryBase.h

Go to the documentation of this file.
00001 #ifndef FWCore_PluginManager_PluginFactoryBase_h
00002 #define FWCore_PluginManager_PluginFactoryBase_h
00003 // -*- C++ -*-
00004 //
00005 // Package:     PluginManager
00006 // Class  :     PluginFactoryBase
00007 // 
00017 //
00018 // Original Author:  Chris Jones
00019 //         Created:  Wed Apr  4 12:24:44 EDT 2007
00020 // $Id: PluginFactoryBase.h,v 1.5 2007/09/28 20:29:25 chrjones Exp $
00021 //
00022 
00023 // system include files
00024 #include <string>
00025 #include <vector>
00026 #include "sigc++/signal.h"
00027 // user include files
00028 #include "FWCore/PluginManager/interface/PluginInfo.h"
00029 #include "FWCore/PluginManager/interface/PluginManager.h"
00030 #include "FWCore/Utilities/interface/Exception.h"
00031 
00032 // forward declarations
00033 namespace edmplugin {
00034 class PluginFactoryBase
00035 {
00036 
00037    public:
00038       PluginFactoryBase() {}
00039       virtual ~PluginFactoryBase();
00040 
00041       // ---------- const member functions ---------------------
00042 
00044       virtual std::vector<PluginInfo> available() const = 0;
00045 
00047       virtual const std::string& category() const = 0;
00048       
00050       mutable sigc::signal<void,const std::string&, const PluginInfo&> newPluginAdded_;
00051 
00052       // ---------- static member functions --------------------
00053 
00054       // ---------- member functions ---------------------------
00055 
00056    protected:
00061       void finishedConstruction();
00062       
00063       void newPlugin(const std::string& iName);
00064 
00065       //since each inheriting class has its own Container type to hold their PMakers
00066       // this function allows them to share the same code when doing the lookup
00067       // this routine will throw an exception if iName is unknown therefore the return value is always valid
00068       template<typename Plugins>
00069       typename Plugins::const_iterator findPMaker(const std::string& iName,
00070                                                    const Plugins& iPlugins) const {
00071         //do we already have it?
00072         typename Plugins::const_iterator itFound = iPlugins.find(iName);
00073         if(itFound == iPlugins.end()) {
00074           std::string lib = PluginManager::get()->load(this->category(),iName).path().native_file_string();
00075           itFound = iPlugins.find(iName);
00076           if(itFound == iPlugins.end()) {
00077             throw cms::Exception("PluginCacheError")<<"The plugin '"<<iName<<"' should have been in loadable\n '"
00078             <<lib<<"'\n but was not there.  This means the plugin cache is incorrect.  Please run 'EdmPluginRefresh "<<lib<<"'";
00079           }
00080         } else {
00081           checkProperLoadable(iName,itFound->second.front().second);
00082         }
00083         return itFound;
00084       }
00085 
00086       //similar to findPMaker but will return 'end()' if iName is known
00087       template<typename Plugins>
00088         typename Plugins::const_iterator tryToFindPMaker(const std::string& iName,
00089                                                          const Plugins& iPlugins) const 
00090       {
00091         //do we already have it?
00092         typename Plugins::const_iterator itFound = iPlugins.find(iName);
00093         if(itFound == iPlugins.end()) {
00094           const SharedLibrary* slib = PluginManager::get()->tryToLoad(this->category(),iName);
00095           if(0!=slib) {
00096             std::string lib = slib->path().native_file_string();
00097             itFound = iPlugins.find(iName);
00098             if(itFound == iPlugins.end()) {
00099               throw cms::Exception("PluginCacheError")<<"The plugin '"<<iName<<"' should have been in loadable\n '"
00100               <<lib<<"'\n but was not there.  This means the plugin cache is incorrect.  Please run 'EdmPluginRefresh "<<lib<<"'";
00101             }
00102           }
00103         } else {
00104           checkProperLoadable(iName,itFound->second.front().second);
00105         }
00106         return itFound;
00107       }
00108       
00109       
00110       template<typename MakersItr>
00111         static void fillInfo(MakersItr iBegin, MakersItr iEnd,
00112                              PluginInfo& iInfo,
00113                              std::vector<PluginInfo>& iReturn ) {
00114           for(MakersItr it = iBegin;
00115               it != iEnd;
00116               ++it) {
00117             iInfo.loadable_ = it->second;
00118             iReturn.push_back(iInfo);
00119           }
00120         }
00121       template<typename PluginsItr>
00122       static void fillAvailable(PluginsItr iBegin,
00123                                 PluginsItr iEnd,
00124                                 std::vector<PluginInfo>& iReturn) {
00125         PluginInfo info;
00126         for( PluginsItr it = iBegin;
00127             it != iEnd;
00128             ++it) {
00129           info.name_ = it->first;
00130           fillInfo(it->second.begin(),it->second.end(),
00131                    info, iReturn);
00132         }
00133       }
00134       
00135       
00136    private:
00137       PluginFactoryBase(const PluginFactoryBase&); // stop default
00138 
00139       const PluginFactoryBase& operator=(const PluginFactoryBase&); // stop default
00140 
00141       void checkProperLoadable(const std::string& iName, const std::string& iLoadedFrom) const {
00142         //should check to see if this is from the proper loadable if it
00143         // was not statically linked
00144         if (iLoadedFrom != PluginManager::staticallyLinkedLoadingFileName() &&
00145             PluginManager::isAvailable()) {
00146           if( iLoadedFrom != PluginManager::get()->loadableFor(category(),iName).native_file_string() ) {
00147             throw cms::Exception("WrongPluginLoaded")<<"The plugin '"<<iName<<"' should have been loaded from\n '"
00148             <<PluginManager::get()->loadableFor(category(),iName).native_file_string()
00149             <<"'\n but instead it was already loaded from\n '"
00150             <<iLoadedFrom<<"'\n because some other plugin was loaded from the latter loadables.\n"
00151             "To work around the problem the plugin '"<<iName<<"' should only be defined in one of these loadables.";
00152           }
00153         }
00154       }
00155       // ---------- member data --------------------------------
00156 
00157 };
00158 
00159 }
00160 #endif

Generated on Tue Jun 9 17:36:32 2009 for CMSSW by  doxygen 1.5.4