CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/FWCore/ServiceRegistry/interface/ServiceRegistry.h

Go to the documentation of this file.
00001 #ifndef ServiceRegistry_ServiceRegistry_h
00002 #define ServiceRegistry_ServiceRegistry_h
00003 // -*- C++ -*-
00004 //
00005 // Package:     ServiceRegistry
00006 // Class  :     ServiceRegistry
00007 // 
00016 //
00017 // Original Author:  Chris Jones
00018 //         Created:  Mon Sep  5 13:33:00 EDT 2005
00019 //
00020 
00021 // system include files
00022 #include <vector>
00023 
00024 // user include files
00025 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00026 #include "FWCore/ServiceRegistry/interface/ServiceToken.h"
00027 #include "FWCore/ServiceRegistry/interface/ServiceLegacy.h"
00028 #include "FWCore/ServiceRegistry/interface/ServicesManager.h"
00029 
00030 // forward declarations
00031 namespace edm {
00032    class FwkImpl;
00033    namespace serviceregistry {
00034       template< typename T> class ServiceWrapper;
00035    }
00036    
00037    class ServiceRegistry
00038    {
00039 
00040    public:
00041       
00042       class Operate {
00043         public:
00044          Operate(const ServiceToken& iToken) : 
00045          oldToken_(ServiceRegistry::instance().setContext(iToken))
00046          {}
00047          ~Operate() {
00048             ServiceRegistry::instance().unsetContext(oldToken_);
00049          }
00050          
00051          //override operator new to stop use on heap?
00052         private:
00053          Operate(const Operate&); //stop default
00054          const Operate& operator=(const Operate&); //stop default
00055          ServiceToken oldToken_;
00056       };
00057       
00058       friend class edm::FwkImpl;
00059       friend int main(int argc, char* argv[]);
00060       friend class Operate;
00061 
00062       virtual ~ServiceRegistry();
00063 
00064       // ---------- const member functions ---------------------
00065       template<class T>
00066          T& get() const {
00067             if(0 == manager_.get()) {
00068                Exception::throwThis(errors::NotFound,
00069                 "Service"
00070                 " no ServiceRegistry has been set for this thread");
00071             }
00072             return manager_-> template get<T>();
00073          }
00074       
00075       template<class T>
00076          bool isAvailable() const {
00077             if(0 == manager_.get()) {
00078                Exception::throwThis(errors::NotFound,
00079                 "Service"
00080                 " no ServiceRegistry has been set for this thread");
00081             }
00082             return manager_-> template isAvailable<T>();
00083          }
00087       ServiceToken presentToken() const;
00088       // ---------- static member functions --------------------
00089       static ServiceRegistry& instance();
00090       
00091       // ---------- member functions ---------------------------
00092       
00093    
00094       static ServiceToken createServicesFromConfig(const std::string& config);
00095 
00096    public: // Made public (temporarily) at the request of Emilio Meschi.
00097       static ServiceToken createSet(std::vector<ParameterSet>&);
00098       static ServiceToken createSet(std::vector<ParameterSet>&,
00099                                     ServiceToken,
00100                                     serviceregistry::ServiceLegacy);
00102       template<class T>
00103          static ServiceToken createContaining(std::auto_ptr<T> iService){
00104             std::vector<edm::ParameterSet> config;
00105             boost::shared_ptr<serviceregistry::ServicesManager> manager( new serviceregistry::ServicesManager(config) );
00106             boost::shared_ptr<serviceregistry::ServiceWrapper<T> >
00107                wrapper(new serviceregistry::ServiceWrapper<T>(iService));
00108             manager->put(wrapper);
00109             return manager;
00110          }
00111       template<class T>
00112          static ServiceToken createContaining(std::auto_ptr<T> iService,
00113                                               ServiceToken iToken,
00114                                               serviceregistry::ServiceLegacy iLegacy){
00115             std::vector<edm::ParameterSet> config;
00116             boost::shared_ptr<serviceregistry::ServicesManager> manager( new serviceregistry::ServicesManager(iToken,
00117                                                                                                               iLegacy,
00118                                                                                                               config) );
00119             boost::shared_ptr<serviceregistry::ServiceWrapper<T> >
00120             wrapper(new serviceregistry::ServiceWrapper<T>(iService));
00121             manager->put(wrapper);
00122             return manager;
00123          }
00125       template<class T>
00126          static ServiceToken createContaining(boost::shared_ptr<serviceregistry::ServiceWrapper<T> > iWrapper) {
00127             std::vector<edm::ParameterSet> config;
00128             boost::shared_ptr<serviceregistry::ServicesManager> manager( new serviceregistry::ServicesManager(config) );
00129             manager->put(iWrapper);
00130             return manager;
00131          }
00132       template<class T>
00133          static ServiceToken createContaining(boost::shared_ptr<serviceregistry::ServiceWrapper<T> > iWrapper,
00134                                               ServiceToken iToken,
00135                                               serviceregistry::ServiceLegacy iLegacy){
00136             std::vector<edm::ParameterSet> config;
00137             boost::shared_ptr<serviceregistry::ServicesManager> manager( new serviceregistry::ServicesManager(iToken,
00138                                                                                                               iLegacy,
00139                                                                                                               config) );
00140             manager->put(iWrapper);
00141             return manager;
00142          }
00143       
00144 private:
00145       
00146       //returns old token
00147       ServiceToken setContext(const ServiceToken& iNewToken);
00148       void unsetContext(const ServiceToken& iOldToken);
00149       
00150       ServiceRegistry();
00151       ServiceRegistry(const ServiceRegistry&); // stop default
00152 
00153       const ServiceRegistry& operator=(const ServiceRegistry&); // stop default
00154 
00155       // ---------- member data --------------------------------
00156       boost::shared_ptr<serviceregistry::ServicesManager> manager_;
00157    };
00158 }
00159 
00160 #endif