00001 #ifndef ServiceRegistry_ServiceRegistry_h
00002 #define ServiceRegistry_ServiceRegistry_h
00003
00004
00005
00006
00007
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
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
00052 private:
00053 Operate(const Operate&);
00054 const Operate& operator=(const Operate&);
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
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
00087 static ServiceRegistry& instance();
00088
00089
00090
00091
00092 static ServiceToken createServicesFromConfig(const std::string& config);
00093
00094 public:
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
00145 ServiceToken setContext(const ServiceToken& iNewToken);
00146 void unsetContext(const ServiceToken& iOldToken);
00147
00148 ServiceRegistry();
00149 ServiceRegistry(const ServiceRegistry&);
00150
00151 const ServiceRegistry& operator=(const ServiceRegistry&);
00152
00153
00154 boost::shared_ptr<serviceregistry::ServicesManager> manager_;
00155 };
00156 }
00157
00158 #endif