CMS 3D CMS Logo

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 // $Id: ServiceRegistry.h,v 1.11 2007/10/27 21:46:43 chrjones Exp $
00020 //
00021 
00022 // system include files
00023 
00024 // user include files
00025 #include "FWCore/ServiceRegistry/interface/ServiceToken.h"
00026 #include "FWCore/ServiceRegistry/interface/ServiceLegacy.h"
00027 #include "FWCore/ServiceRegistry/interface/ServicesManager.h"
00028 #include "FWCore/ParameterSet/interface/ParameterSet.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                throw edm::Exception(edm::errors::NotFound,"Service")
00069                <<" no ServiceRegistry has been set for this thread";
00070             }
00071             return manager_-> template get<T>();
00072          }
00073       
00074       template<class T>
00075          bool isAvailable() const {
00076             if(0 == manager_.get()) {
00077                throw edm::Exception(edm::errors::NotFound,"Service")
00078                <<" no ServiceRegistry has been set for this thread";
00079             }
00080             return manager_-> template isAvailable<T>();
00081          }
00085       ServiceToken presentToken() const;
00086       // ---------- static member functions --------------------
00087       static ServiceRegistry& instance();
00088       
00089       // ---------- member functions ---------------------------
00090       
00091    
00092       static ServiceToken createServicesFromConfig(const std::string& config);
00093 
00094    public: // Made public (temporarily) at the request of Emilio Meschi.
00095       static ServiceToken createSet(const std::vector<ParameterSet>&);
00096       static ServiceToken createSet(const std::vector<ParameterSet>&,
00097                                     ServiceToken,
00098                                     serviceregistry::ServiceLegacy);
00100       template<class T>
00101          static ServiceToken createContaining(std::auto_ptr<T> iService){
00102             std::vector<edm::ParameterSet> config;
00103             boost::shared_ptr<serviceregistry::ServicesManager> manager( new serviceregistry::ServicesManager(config) );
00104             boost::shared_ptr<serviceregistry::ServiceWrapper<T> >
00105                wrapper(new serviceregistry::ServiceWrapper<T>(iService));
00106             manager->put(wrapper);
00107             return manager;
00108          }
00109       template<class T>
00110          static ServiceToken createContaining(std::auto_ptr<T> iService,
00111                                               ServiceToken iToken,
00112                                               serviceregistry::ServiceLegacy iLegacy){
00113             std::vector<edm::ParameterSet> config;
00114             boost::shared_ptr<serviceregistry::ServicesManager> manager( new serviceregistry::ServicesManager(iToken,
00115                                                                                                               iLegacy,
00116                                                                                                               config) );
00117             boost::shared_ptr<serviceregistry::ServiceWrapper<T> >
00118             wrapper(new serviceregistry::ServiceWrapper<T>(iService));
00119             manager->put(wrapper);
00120             return manager;
00121          }
00123       template<class T>
00124          static ServiceToken createContaining(boost::shared_ptr<serviceregistry::ServiceWrapper<T> > iWrapper) {
00125             std::vector<edm::ParameterSet> config;
00126             boost::shared_ptr<serviceregistry::ServicesManager> manager( new serviceregistry::ServicesManager(config) );
00127             manager->put(iWrapper);
00128             return manager;
00129          }
00130       template<class T>
00131          static ServiceToken createContaining(boost::shared_ptr<serviceregistry::ServiceWrapper<T> > iWrapper,
00132                                               ServiceToken iToken,
00133                                               serviceregistry::ServiceLegacy iLegacy){
00134             std::vector<edm::ParameterSet> config;
00135             boost::shared_ptr<serviceregistry::ServicesManager> manager( new serviceregistry::ServicesManager(iToken,
00136                                                                                                               iLegacy,
00137                                                                                                               config) );
00138             manager->put(iWrapper);
00139             return manager;
00140          }
00141       
00142 private:
00143       
00144       //returns old token
00145       ServiceToken setContext(const ServiceToken& iNewToken);
00146       void unsetContext(const ServiceToken& iOldToken);
00147       
00148       ServiceRegistry();
00149       ServiceRegistry(const ServiceRegistry&); // stop default
00150 
00151       const ServiceRegistry& operator=(const ServiceRegistry&); // stop default
00152 
00153       // ---------- member data --------------------------------
00154       boost::shared_ptr<serviceregistry::ServicesManager> manager_;
00155    };
00156 }
00157 
00158 #endif

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