CMS 3D CMS Logo

ServicesManager.h
Go to the documentation of this file.
1 #ifndef ServiceRegistry_ServicesManager_h
2 #define ServiceRegistry_ServicesManager_h
3 // -*- C++ -*-
4 //
5 // Package: ServiceRegistry
6 // Class : ServicesManager
7 //
16 //
17 // Original Author: Chris Jones
18 // Created: Mon Sep 5 13:33:01 EDT 2005
19 //
20 
21 // user include files
30 
31 // system include files
32 #include <memory>
33 
34 #include <cassert>
35 #include <vector>
36 
37 // forward declarations
38 namespace edm {
39  class ParameterSet;
40  class ServiceToken;
41 
42  namespace serviceregistry {
43 
45  public:
46  struct MakerHolder {
47  MakerHolder(std::shared_ptr<ServiceMakerBase> iMaker, ParameterSet& iPSet, ActivityRegistry&);
48  bool add(ServicesManager&) const;
49 
52  ActivityRegistry* registry_; // We do not use propagate_const because the registry itself is mutable
53  //Services construction is not allowed to occur across threads
54  CMS_SA_ALLOW mutable bool wasAdded_;
55  };
56  typedef std::map<TypeIDBase, std::shared_ptr<ServiceWrapperBase>> Type2Service;
57  typedef std::map<TypeIDBase, MakerHolder> Type2Maker;
58 
59  ServicesManager(std::vector<ParameterSet>& iConfiguration);
60 
66  ServiceLegacy iLegacy,
67  std::vector<ParameterSet>& iConfiguration,
68  bool associate = true);
69 
70  ServicesManager(ServicesManager const&) = delete; // stop default
71  ServicesManager const& operator=(ServicesManager const&) = delete; // stop default
73 
74  // ---------- const member functions ---------------------
75  template <typename T>
76  T& get() const {
77  Type2Service::const_iterator itFound = type2Service_.find(TypeIDBase(typeid(T)));
78  Type2Maker::const_iterator itFoundMaker;
79  if (itFound == type2Service_.end()) {
80  //do on demand building of the service
81  if (nullptr == type2Maker_.get() ||
82  type2Maker_->end() == (itFoundMaker = type2Maker_->find(TypeIDBase(typeid(T))))) {
84  "Service Request unable to find requested service with compiler type name '",
85  typeid(T).name(),
86  "'.\n");
87  } else {
88  const_cast<ServicesManager&>(*this).createServiceFor(itFoundMaker->second);
89  itFound = type2Service_.find(TypeIDBase(typeid(T)));
90  //the 'add()' should have put the service into the list
91  assert(itFound != type2Service_.end());
92  }
93  }
94  //convert it to its actual type
95  std::shared_ptr<ServiceWrapper<T>> ptr(std::dynamic_pointer_cast<ServiceWrapper<T>>(itFound->second));
96  assert(nullptr != ptr.get());
97  return ptr->get();
98  }
99 
101  template <typename T>
102  bool isAvailable() const {
103  Type2Service::const_iterator itFound = type2Service_.find(TypeIDBase(typeid(T)));
104  Type2Maker::const_iterator itFoundMaker;
105  if (itFound == type2Service_.end()) {
106  //do on demand building of the service
107  if (nullptr == type2Maker_.get() ||
108  type2Maker_->end() == (itFoundMaker = type2Maker_->find(TypeIDBase(typeid(T))))) {
109  return false;
110  } else {
111  //Actually create the service in order to 'flush out' any
112  // configuration errors for the service
113  const_cast<ServicesManager&>(*this).createServiceFor(itFoundMaker->second);
114  itFound = type2Service_.find(TypeIDBase(typeid(T)));
115  //the 'add()' should have put the service into the list
116  assert(itFound != type2Service_.end());
117  }
118  }
119  return true;
120  }
121 
122  // ---------- static member functions --------------------
123 
124  // ---------- member functions ---------------------------
126  template <typename T>
127  bool put(std::shared_ptr<ServiceWrapper<T>> iPtr) {
128  Type2Service::const_iterator itFound = type2Service_.find(TypeIDBase(typeid(T)));
129  if (itFound != type2Service_.end()) {
130  return false;
131  }
132  type2Service_[TypeIDBase(typeid(T))] = iPtr;
133  actualCreationOrder_.push_back(TypeIDBase(typeid(T)));
134  return true;
135  }
136 
138  void connect(ActivityRegistry& iOther);
139 
141  void connectTo(ActivityRegistry& iOther);
142 
147 
148  private:
149  void fillListOfMakers(std::vector<ParameterSet>&);
150  void createServices();
151  void createServiceFor(MakerHolder const&);
152 
153  // ---------- member data --------------------------------
154  //hold onto the Manager passed in from the ServiceToken so that
155  // the ActivityRegistry of that Manager does not go out of scope
156  // This must be first to get the Service destructors called in
157  // the correct order.
159 
163  std::vector<TypeIDBase> requestedCreationOrder_;
164  std::vector<TypeIDBase> actualCreationOrder_;
165  };
166  } // namespace serviceregistry
167 } // namespace edm
168 
169 #endif
edm::serviceregistry::ServicesManager::put
bool put(std::shared_ptr< ServiceWrapper< T >> iPtr)
returns false if put fails because a service of this type already exists
Definition: ServicesManager.h:127
edm::serviceregistry::ServicesManager::MakerHolder::pset_
ParameterSet * pset_
Definition: ServicesManager.h:51
edm::serviceregistry::ServicesManager::createServices
void createServices()
Definition: ServicesManager.cc:279
edm::serviceregistry::ServicesManager::connect
void connect(ActivityRegistry &iOther)
causes our ActivityRegistry's signals to be forwarded to iOther
Definition: ServicesManager.cc:212
propagate_const.h
edm::serviceregistry::ServicesManager::MakerHolder::registry_
ActivityRegistry * registry_
Definition: ServicesManager.h:52
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::serviceregistry::ServicesManager::type2Service_
Type2Service type2Service_
Definition: ServicesManager.h:161
edm::serviceregistry::ServicesManager::fillListOfMakers
void fillListOfMakers(std::vector< ParameterSet > &)
Definition: ServicesManager.cc:220
edm::serviceregistry::ServicesManager
Definition: ServicesManager.h:44
edm::serviceregistry::ServicesManager::operator=
ServicesManager const & operator=(ServicesManager const &)=delete
edm::serviceregistry::ServicesManager::copySlotsTo
void copySlotsTo(ActivityRegistry &)
copy our Service's slots to the argument's signals
Definition: ServicesManager.cc:218
cms::cuda::assert
assert(be >=bs)
edm::propagate_const::get
constexpr element_type const * get() const
Definition: propagate_const.h:64
edm::serviceregistry::ServicesManager::Type2Maker
std::map< TypeIDBase, MakerHolder > Type2Maker
Definition: ServicesManager.h:57
edm::errors::NotFound
Definition: EDMException.h:57
CMS_SA_ALLOW
#define CMS_SA_ALLOW
Definition: thread_safety_macros.h:5
edm::serviceregistry::ServicesManager::createServiceFor
void createServiceFor(MakerHolder const &)
Definition: ServicesManager.cc:251
ActivityRegistry.h
EDMException.h
edm::ServiceToken
Definition: ServiceToken.h:40
edm::propagate_const
Definition: propagate_const.h:32
edm::serviceregistry::ServicesManager::MakerHolder::maker_
edm::propagate_const< std::shared_ptr< ServiceMakerBase > > maker_
Definition: ServicesManager.h:50
TypeIDBase.h
ServiceLegacy
edm::ActivityRegistry
Definition: ActivityRegistry.h:133
edm::serviceregistry::ServicesManager::actualCreationOrder_
std::vector< TypeIDBase > actualCreationOrder_
Definition: ServicesManager.h:164
edm::serviceregistry::ServicesManager::MakerHolder
Definition: ServicesManager.h:46
edm::serviceregistry::ServicesManager::requestedCreationOrder_
std::vector< TypeIDBase > requestedCreationOrder_
Definition: ServicesManager.h:163
ServiceWrapper.h
edm::ParameterSet
Definition: ParameterSet.h:47
ParameterSet
Definition: Functions.h:16
thread_safety_macros.h
edm::serviceregistry::ServicesManager::ServicesManager
ServicesManager(std::vector< ParameterSet > &iConfiguration)
Definition: ServicesManager.cc:65
edm::serviceregistry::ServicesManager::connectTo
void connectTo(ActivityRegistry &iOther)
causes iOther's signals to be forward to us
Definition: ServicesManager.cc:214
edm::serviceregistry::ServicesManager::MakerHolder::wasAdded_
bool wasAdded_
Definition: ServicesManager.h:54
ServiceMakerBase.h
edm::serviceregistry::ServicesManager::get
T & get() const
Definition: ServicesManager.h:76
ServiceToken
T
long double T
Definition: Basic3DVectorLD.h:48
edm::serviceregistry::ServicesManager::registry_
ActivityRegistry registry_
Definition: ServicesManager.h:160
edm::serviceregistry::ServicesManager::type2Maker_
edm::propagate_const< std::unique_ptr< Type2Maker > > type2Maker_
Definition: ServicesManager.h:162
ServiceLegacy.h
edm::Exception::throwThis
static void throwThis(Code category, char const *message0="", char const *message1="", char const *message2="", char const *message3="", char const *message4="")
Definition: EDMException.cc:83
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
edm::TypeIDBase
Definition: TypeIDBase.h:29
edm::serviceregistry::ServicesManager::copySlotsFrom
void copySlotsFrom(ActivityRegistry &)
the copy the argument's slots to the our signals
Definition: ServicesManager.cc:216
edm::serviceregistry::ServicesManager::~ServicesManager
~ServicesManager()
Definition: ServicesManager.cc:175
edm::serviceregistry::ServicesManager::Type2Service
std::map< TypeIDBase, std::shared_ptr< ServiceWrapperBase > > Type2Service
Definition: ServicesManager.h:56
edm::serviceregistry::ServicesManager::associatedManager_
edm::propagate_const< std::shared_ptr< ServicesManager > > associatedManager_
Definition: ServicesManager.h:158
edm::serviceregistry::ServicesManager::MakerHolder::add
bool add(ServicesManager &) const
Definition: ServicesManager.cc:48
edm::serviceregistry::ServicesManager::MakerHolder::MakerHolder
MakerHolder(std::shared_ptr< ServiceMakerBase > iMaker, ParameterSet &iPSet, ActivityRegistry &)
Definition: ServicesManager.cc:43
edm::serviceregistry::ServicesManager::isAvailable
bool isAvailable() const
returns true of the particular service is accessible
Definition: ServicesManager.h:102
edm::serviceregistry::ServiceWrapper
Definition: ServiceRegistry.h:35