![]() |
![]() |
00001 #ifndef ServiceRegistry_ServicesManager_h 00002 #define ServiceRegistry_ServicesManager_h 00003 // -*- C++ -*- 00004 // 00005 // Package: ServiceRegistry 00006 // Class : ServicesManager 00007 // 00016 // 00017 // Original Author: Chris Jones 00018 // Created: Mon Sep 5 13:33:01 EDT 2005 00019 // $Id: ServicesManager.h,v 1.10 2010/01/19 22:37:05 wdd Exp $ 00020 // 00021 00022 // system include files 00023 #include <vector> 00024 #include <cassert> 00025 #include "boost/shared_ptr.hpp" 00026 00027 // user include files 00028 #include "FWCore/Utilities/interface/TypeIDBase.h" 00029 #include "FWCore/ServiceRegistry/interface/ServiceWrapper.h" 00030 #include "FWCore/ServiceRegistry/interface/ServiceMakerBase.h" 00031 #include "FWCore/ServiceRegistry/interface/ServiceLegacy.h" 00032 #include "FWCore/ServiceRegistry/interface/ActivityRegistry.h" 00033 00034 #include "FWCore/Utilities/interface/EDMException.h" 00035 00036 // forward declarations 00037 namespace edm { 00038 class ParameterSet; 00039 class ServiceToken; 00040 00041 namespace serviceregistry { 00042 00043 class ServicesManager 00044 { 00045 00046 public: 00047 struct MakerHolder { 00048 MakerHolder(boost::shared_ptr<ServiceMakerBase> iMaker, 00049 edm::ParameterSet& iPSet, 00050 edm::ActivityRegistry&) ; 00051 bool add(ServicesManager&) const; 00052 00053 boost::shared_ptr<ServiceMakerBase> maker_; 00054 edm::ParameterSet* pset_; 00055 edm::ActivityRegistry* registry_; 00056 mutable bool wasAdded_; 00057 }; 00058 typedef std::map< TypeIDBase, boost::shared_ptr<ServiceWrapperBase> > Type2Service; 00059 typedef std::map< TypeIDBase, MakerHolder > Type2Maker; 00060 00061 ServicesManager(std::vector<edm::ParameterSet>& iConfiguration); 00062 00067 ServicesManager(ServiceToken iToken, 00068 ServiceLegacy iLegacy, 00069 std::vector<edm::ParameterSet>& iConfiguration); 00070 00071 ~ServicesManager(); 00072 00073 // ---------- const member functions --------------------- 00074 template<class T> 00075 T& get() const { 00076 Type2Service::const_iterator itFound = type2Service_.find(TypeIDBase(typeid(T))); 00077 Type2Maker::const_iterator itFoundMaker ; 00078 if(itFound == type2Service_.end()) { 00079 //do on demand building of the service 00080 if(0 == type2Maker_.get() || 00081 type2Maker_->end() == (itFoundMaker = type2Maker_->find(TypeIDBase(typeid(T))))) { 00082 Exception::throwThis(errors::NotFound, 00083 "Service Request unable to find requested service with compiler type name '", 00084 typeid(T).name(), 00085 "'.\n"); 00086 } else { 00087 itFoundMaker->second.add(const_cast<ServicesManager&>(*this)); 00088 itFound = type2Service_.find(TypeIDBase(typeid(T))); 00089 //the 'add()' should have put the service into the list 00090 assert(itFound != type2Service_.end()); 00091 } 00092 } 00093 //convert it to its actual type 00094 boost::shared_ptr<ServiceWrapper<T> > ptr(boost::dynamic_pointer_cast<ServiceWrapper<T> >(itFound->second)); 00095 assert(0 != ptr.get()); 00096 return ptr->get(); 00097 } 00098 00100 template<class T> 00101 bool isAvailable() const { 00102 Type2Service::const_iterator itFound = type2Service_.find(TypeIDBase(typeid(T))); 00103 Type2Maker::const_iterator itFoundMaker ; 00104 if(itFound == type2Service_.end()) { 00105 //do on demand building of the service 00106 if(0 == type2Maker_.get() || 00107 type2Maker_->end() == (itFoundMaker = type2Maker_->find(TypeIDBase(typeid(T))))) { 00108 return false; 00109 } else { 00110 //Actually create the service in order to 'flush out' any 00111 // configuration errors for the service 00112 itFoundMaker->second.add(const_cast<ServicesManager&>(*this)); 00113 itFound = type2Service_.find(TypeIDBase(typeid(T))); 00114 //the 'add()' should have put the service into the list 00115 assert(itFound != type2Service_.end()); 00116 } 00117 } 00118 return true; 00119 } 00120 00121 // ---------- static member functions -------------------- 00122 00123 // ---------- member functions --------------------------- 00125 template<class T> 00126 bool put(boost::shared_ptr<ServiceWrapper<T> > iPtr) { 00127 Type2Service::const_iterator itFound = type2Service_.find(TypeIDBase(typeid(T))); 00128 if(itFound != type2Service_.end()) { 00129 return false; 00130 } 00131 type2Service_[ TypeIDBase(typeid(T)) ] = iPtr; 00132 actualCreationOrder_.push_back(TypeIDBase(typeid(T))); 00133 return true; 00134 } 00135 00137 void connect(ActivityRegistry& iOther); 00138 00140 void connectTo(ActivityRegistry& iOther); 00141 00143 void copySlotsTo(ActivityRegistry&); 00145 void copySlotsFrom(ActivityRegistry&); 00146 00147 private: 00148 ServicesManager(const ServicesManager&); // stop default 00149 00150 const ServicesManager& operator=(const ServicesManager&); // stop default 00151 00152 void fillListOfMakers(std::vector<edm::ParameterSet>&); 00153 void createServices(); 00154 00155 // ---------- member data -------------------------------- 00156 //hold onto the Manager passed in from the ServiceToken so that 00157 // the ActivityRegistry of that Manager does not go out of scope 00158 // This must be first to get the Service destructors called in 00159 // the correct order. 00160 boost::shared_ptr<ServicesManager> associatedManager_; 00161 00162 edm::ActivityRegistry registry_; 00163 Type2Service type2Service_; 00164 std::auto_ptr<Type2Maker> type2Maker_; 00165 std::vector<TypeIDBase> requestedCreationOrder_; 00166 std::vector<TypeIDBase> actualCreationOrder_; 00167 00168 }; 00169 } 00170 } 00171 00172 00173 #endif