File indexing completed on 2024-04-06 12:13:02
0001 #ifndef ServiceRegistry_ServiceRegistry_h
0002 #define ServiceRegistry_ServiceRegistry_h
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
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 #include "FWCore/Utilities/interface/TypeDemangler.h"
0027
0028
0029 #include <vector>
0030
0031
0032 namespace edm {
0033 class FwkImpl;
0034 namespace serviceregistry {
0035 template <typename T>
0036 class ServiceWrapper;
0037 }
0038
0039 class ServiceRegistry {
0040 public:
0041 class Operate {
0042 public:
0043 Operate(ServiceToken const& iToken) : oldToken_(ServiceRegistry::instance().setContext(iToken)) {}
0044 Operate(Operate const&) = delete;
0045 Operate const& operator=(Operate const&) = delete;
0046 ~Operate() { ServiceRegistry::instance().unsetContext(oldToken_); }
0047
0048
0049 private:
0050 ServiceToken oldToken_;
0051 };
0052
0053 friend class edm::FwkImpl;
0054 friend int main(int argc, char* argv[]);
0055 friend class Operate;
0056
0057 ServiceRegistry(ServiceRegistry const&) = delete;
0058 ServiceRegistry const& operator=(ServiceRegistry const&) = delete;
0059 virtual ~ServiceRegistry();
0060
0061
0062 template <typename T>
0063 T& get() const {
0064 if (nullptr == manager_.get()) {
0065 auto demangled = typeDemangle(typeid(T).name());
0066 Exception::throwThis(errors::NotFound,
0067 "No ServiceRegistry has been set for this thread. Tried to get a Service ",
0068 demangled.c_str());
0069 }
0070 return manager_->template get<T>();
0071 }
0072
0073 template <typename T>
0074 bool isAvailable() const {
0075 if (nullptr == manager_.get()) {
0076 auto demangled = typeDemangle(typeid(T).name());
0077 Exception::throwThis(errors::NotFound,
0078 "No ServiceRegistry has been set for this thread. Tried to ask availability of a Service ",
0079 demangled.c_str());
0080 }
0081 return manager_->template isAvailable<T>();
0082 }
0083
0084
0085
0086 ServiceToken presentToken() const;
0087
0088 static ServiceRegistry& instance();
0089
0090
0091
0092 static ServiceToken createServicesFromConfig(std::unique_ptr<ParameterSet> params);
0093
0094 public:
0095 static ServiceToken createSet(std::vector<ParameterSet>&);
0096 static ServiceToken createSet(std::vector<ParameterSet>&,
0097 ServiceToken,
0098 serviceregistry::ServiceLegacy,
0099 bool associate = true);
0100
0101 template <typename T>
0102 static ServiceToken createContaining(std::unique_ptr<T> iService) {
0103 std::vector<edm::ParameterSet> config;
0104 auto manager = std::make_shared<serviceregistry::ServicesManager>(config);
0105 auto wrapper = std::make_shared<serviceregistry::ServiceWrapper<T> >(std::move(iService));
0106 manager->put(wrapper);
0107 return manager;
0108 }
0109 template <typename T>
0110 static ServiceToken createContaining(std::unique_ptr<T> iService,
0111 ServiceToken iToken,
0112 bool iOverrideIfServiceInToken) {
0113 std::vector<edm::ParameterSet> config;
0114 auto manager =
0115 std::make_shared<serviceregistry::ServicesManager>(iToken, serviceregistry::kOverlapIsError, config);
0116 auto wrapper = std::make_shared<serviceregistry::ServiceWrapper<T> >(std::move(iService));
0117 manager->put(wrapper, iOverrideIfServiceInToken);
0118 return manager;
0119 }
0120
0121 template <typename T>
0122 static ServiceToken createContaining(std::shared_ptr<serviceregistry::ServiceWrapper<T> > iWrapper) {
0123 std::vector<edm::ParameterSet> config;
0124 auto manager = std::make_shared<serviceregistry::ServicesManager>(config);
0125 manager->put(iWrapper);
0126 return manager;
0127 }
0128 template <typename T>
0129 static ServiceToken createContaining(std::shared_ptr<serviceregistry::ServiceWrapper<T> > iWrapper,
0130 ServiceToken iToken,
0131 serviceregistry::ServiceLegacy iLegacy) {
0132 std::vector<edm::ParameterSet> config;
0133 auto manager = std::make_shared<serviceregistry::ServicesManager>(iToken, iLegacy, config);
0134 manager->put(iWrapper);
0135 return manager;
0136 }
0137
0138 private:
0139
0140 ServiceToken setContext(ServiceToken const& iNewToken);
0141 void unsetContext(ServiceToken const& iOldToken);
0142
0143 ServiceRegistry();
0144
0145
0146 std::shared_ptr<serviceregistry::ServicesManager> manager_;
0147 };
0148 }
0149
0150 #endif