CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/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 // user include files
00022 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00023 #include "FWCore/ServiceRegistry/interface/ServiceLegacy.h"
00024 #include "FWCore/ServiceRegistry/interface/ServiceToken.h"
00025 #include "FWCore/ServiceRegistry/interface/ServicesManager.h"
00026 
00027 // system include files
00028 #include <vector>
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    public:
00039 
00040       class Operate {
00041       public:
00042          Operate(ServiceToken const& iToken) :
00043          oldToken_(ServiceRegistry::instance().setContext(iToken)) {}
00044          ~Operate() {
00045             ServiceRegistry::instance().unsetContext(oldToken_);
00046          }
00047 
00048          //override operator new to stop use on heap?
00049       private:
00050          Operate(Operate const&); //stop default
00051          Operate const& operator=(Operate const&); //stop default
00052          ServiceToken oldToken_;
00053       };
00054 
00055       friend class edm::FwkImpl;
00056       friend int main(int argc, char* argv[]);
00057       friend class Operate;
00058 
00059       virtual ~ServiceRegistry();
00060 
00061       // ---------- const member functions ---------------------
00062       template<typename T>
00063       T& get() const {
00064          if(0 == manager_.get()) {
00065             Exception::throwThis(errors::NotFound,
00066               "Service"
00067               " no ServiceRegistry has been set for this thread");
00068          }
00069          return manager_-> template get<T>();
00070       }
00071 
00072       template<typename T>
00073       bool isAvailable() const {
00074          if(0 == manager_.get()) {
00075             Exception::throwThis(errors::NotFound,
00076               "Service"
00077               " no ServiceRegistry has been set for this thread");
00078          }
00079          return manager_-> template isAvailable<T>();
00080       }
00084       ServiceToken presentToken() const;
00085       // ---------- static member functions --------------------
00086       static ServiceRegistry& instance();
00087 
00088       // ---------- member functions ---------------------------
00089 
00090       static ServiceToken createServicesFromConfig(std::string const& config);
00091 
00092    public: // Made public (temporarily) at the request of Emilio Meschi.
00093       static ServiceToken createSet(std::vector<ParameterSet>&);
00094       static ServiceToken createSet(std::vector<ParameterSet>&,
00095                                     ServiceToken,
00096                                     serviceregistry::ServiceLegacy,
00097                                     bool associate = true);
00099       template<typename T>
00100       static ServiceToken createContaining(std::auto_ptr<T> iService){
00101          std::vector<edm::ParameterSet> config;
00102          boost::shared_ptr<serviceregistry::ServicesManager> manager(new serviceregistry::ServicesManager(config));
00103          boost::shared_ptr<serviceregistry::ServiceWrapper<T> >
00104             wrapper(new serviceregistry::ServiceWrapper<T>(iService));
00105          manager->put(wrapper);
00106          return manager;
00107       }
00108       template<typename T>
00109       static ServiceToken createContaining(std::auto_ptr<T> iService,
00110                                            ServiceToken iToken,
00111                                            serviceregistry::ServiceLegacy iLegacy){
00112          std::vector<edm::ParameterSet> config;
00113          boost::shared_ptr<serviceregistry::ServicesManager> manager(new serviceregistry::ServicesManager(iToken,
00114                                                                                                           iLegacy,
00115                                                                                                           config));
00116          boost::shared_ptr<serviceregistry::ServiceWrapper<T> >
00117          wrapper(new serviceregistry::ServiceWrapper<T>(iService));
00118          manager->put(wrapper);
00119          return manager;
00120       }
00122       template<typename T>
00123       static ServiceToken createContaining(boost::shared_ptr<serviceregistry::ServiceWrapper<T> > iWrapper) {
00124          std::vector<edm::ParameterSet> config;
00125          boost::shared_ptr<serviceregistry::ServicesManager> manager(new serviceregistry::ServicesManager(config));
00126          manager->put(iWrapper);
00127          return manager;
00128       }
00129       template<typename T>
00130       static ServiceToken createContaining(boost::shared_ptr<serviceregistry::ServiceWrapper<T> > iWrapper,
00131                                            ServiceToken iToken,
00132                                            serviceregistry::ServiceLegacy iLegacy){
00133          std::vector<edm::ParameterSet> config;
00134          boost::shared_ptr<serviceregistry::ServicesManager> manager(new serviceregistry::ServicesManager(iToken,
00135                                                                                                           iLegacy,
00136                                                                                                           config));
00137          manager->put(iWrapper);
00138          return manager;
00139       }
00140 
00141 private:
00142       //returns old token
00143       ServiceToken setContext(ServiceToken const& iNewToken);
00144       void unsetContext(ServiceToken const& iOldToken);
00145 
00146       ServiceRegistry();
00147       ServiceRegistry(ServiceRegistry const&); // stop default
00148 
00149       ServiceRegistry const& operator=(ServiceRegistry const&); // stop default
00150 
00151       // ---------- member data --------------------------------
00152       boost::shared_ptr<serviceregistry::ServicesManager> manager_;
00153    };
00154 }
00155 
00156 #endif