Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:29:08

0001 #ifndef ServiceRegistry_ServiceRegistry_h
0002 #define ServiceRegistry_ServiceRegistry_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     ServiceRegistry
0006 // Class  :     ServiceRegistry
0007 //
0008 /**\class ServiceRegistry ServiceRegistry.h FWCore/ServiceRegistry/interface/ServiceRegistry.h
0009 
0010  Description: Manages the 'thread specific' instance of Services
0011 
0012  Usage:
0013     <usage>
0014 
0015 */
0016 //
0017 // Original Author:  Chris Jones
0018 //         Created:  Mon Sep  5 13:33:00 EDT 2005
0019 //
0020 
0021 // user include files
0022 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0023 #include "FWCore/ServiceRegistry/interface/ServiceLegacy.h"
0024 #include "FWCore/ServiceRegistry/interface/ServiceToken.h"
0025 #include "FWCore/ServiceRegistry/interface/ServicesManager.h"
0026 
0027 // system include files
0028 #include <vector>
0029 
0030 // forward declarations
0031 namespace edm {
0032   class FwkImpl;
0033   namespace serviceregistry {
0034     template <typename T>
0035     class ServiceWrapper;
0036   }
0037 
0038   class ServiceRegistry {
0039   public:
0040     class Operate {
0041     public:
0042       Operate(ServiceToken const& iToken) : oldToken_(ServiceRegistry::instance().setContext(iToken)) {}
0043       Operate(Operate const&) = delete;                   //stop default
0044       Operate const& operator=(Operate const&) = delete;  //stop default
0045       ~Operate() { ServiceRegistry::instance().unsetContext(oldToken_); }
0046 
0047       //override operator new to stop use on heap?
0048     private:
0049       ServiceToken oldToken_;
0050     };
0051 
0052     friend class edm::FwkImpl;
0053     friend int main(int argc, char* argv[]);
0054     friend class Operate;
0055 
0056     ServiceRegistry(ServiceRegistry const&) = delete;                   // stop default
0057     ServiceRegistry const& operator=(ServiceRegistry const&) = delete;  // stop default
0058     virtual ~ServiceRegistry();
0059 
0060     // ---------- const member functions ---------------------
0061     template <typename T>
0062     T& get() const {
0063       if (nullptr == manager_.get()) {
0064         Exception::throwThis(errors::NotFound,
0065                              "Service"
0066                              " no ServiceRegistry has been set for this thread");
0067       }
0068       return manager_->template get<T>();
0069     }
0070 
0071     template <typename T>
0072     bool isAvailable() const {
0073       if (nullptr == manager_.get()) {
0074         Exception::throwThis(errors::NotFound,
0075                              "Service"
0076                              " no ServiceRegistry has been set for this thread");
0077       }
0078       return manager_->template isAvailable<T>();
0079     }
0080     /** The token can be passed to another thread in order to have the
0081          same services available in the other thread.
0082          */
0083     ServiceToken presentToken() const;
0084     // ---------- static member functions --------------------
0085     static ServiceRegistry& instance();
0086 
0087     // ---------- member functions ---------------------------
0088 
0089     static ServiceToken createServicesFromConfig(std::unique_ptr<ParameterSet> params);
0090 
0091   public:  // Made public (temporarily) at the request of Emilio Meschi.
0092     static ServiceToken createSet(std::vector<ParameterSet>&);
0093     static ServiceToken createSet(std::vector<ParameterSet>&,
0094                                   ServiceToken,
0095                                   serviceregistry::ServiceLegacy,
0096                                   bool associate = true);
0097     /// create a service token that holds the service defined by iService
0098     template <typename T>
0099     static ServiceToken createContaining(std::unique_ptr<T> iService) {
0100       std::vector<edm::ParameterSet> config;
0101       auto manager = std::make_shared<serviceregistry::ServicesManager>(config);
0102       auto wrapper = std::make_shared<serviceregistry::ServiceWrapper<T> >(std::move(iService));
0103       manager->put(wrapper);
0104       return manager;
0105     }
0106     template <typename T>
0107     static ServiceToken createContaining(std::unique_ptr<T> iService,
0108                                          ServiceToken iToken,
0109                                          serviceregistry::ServiceLegacy iLegacy) {
0110       std::vector<edm::ParameterSet> config;
0111       auto manager = std::make_shared<serviceregistry::ServicesManager>(iToken, iLegacy, config);
0112       auto wrapper = std::make_shared<serviceregistry::ServiceWrapper<T> >(std::move(iService));
0113       manager->put(wrapper);
0114       return manager;
0115     }
0116     /// create a service token that holds the service held by iWrapper
0117     template <typename T>
0118     static ServiceToken createContaining(std::shared_ptr<serviceregistry::ServiceWrapper<T> > iWrapper) {
0119       std::vector<edm::ParameterSet> config;
0120       auto manager = std::make_shared<serviceregistry::ServicesManager>(config);
0121       manager->put(iWrapper);
0122       return manager;
0123     }
0124     template <typename T>
0125     static ServiceToken createContaining(std::shared_ptr<serviceregistry::ServiceWrapper<T> > iWrapper,
0126                                          ServiceToken iToken,
0127                                          serviceregistry::ServiceLegacy iLegacy) {
0128       std::vector<edm::ParameterSet> config;
0129       auto manager = std::make_shared<serviceregistry::ServicesManager>(iToken, iLegacy, config);
0130       manager->put(iWrapper);
0131       return manager;
0132     }
0133 
0134   private:
0135     //returns old token
0136     ServiceToken setContext(ServiceToken const& iNewToken);
0137     void unsetContext(ServiceToken const& iOldToken);
0138 
0139     ServiceRegistry();
0140 
0141     // ---------- member data --------------------------------
0142     std::shared_ptr<serviceregistry::ServicesManager> manager_;
0143   };
0144 }  // namespace edm
0145 
0146 #endif