CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/FWCore/ServiceRegistry/interface/ServicesManager.h

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