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 
71 
72  // ---------- const member functions ---------------------
73  template <typename T>
74  T& get() const {
75  Type2Service::const_iterator itFound = type2Service_.find(TypeIDBase(typeid(T)));
76  Type2Maker::const_iterator itFoundMaker;
77  if (itFound == type2Service_.end()) {
78  //do on demand building of the service
79  if (nullptr == type2Maker_.get() ||
80  type2Maker_->end() == (itFoundMaker = type2Maker_->find(TypeIDBase(typeid(T))))) {
82  "Service Request unable to find requested service with compiler type name '",
83  typeid(T).name(),
84  "'.\n");
85  } else {
86  const_cast<ServicesManager&>(*this).createServiceFor(itFoundMaker->second);
87  itFound = type2Service_.find(TypeIDBase(typeid(T)));
88  //the 'add()' should have put the service into the list
89  assert(itFound != type2Service_.end());
90  }
91  }
92  //convert it to its actual type
93  std::shared_ptr<ServiceWrapper<T>> ptr(std::dynamic_pointer_cast<ServiceWrapper<T>>(itFound->second));
94  assert(nullptr != ptr.get());
95  return ptr->get();
96  }
97 
99  template <typename T>
100  bool isAvailable() const {
101  Type2Service::const_iterator itFound = type2Service_.find(TypeIDBase(typeid(T)));
102  Type2Maker::const_iterator itFoundMaker;
103  if (itFound == type2Service_.end()) {
104  //do on demand building of the service
105  if (nullptr == type2Maker_.get() ||
106  type2Maker_->end() == (itFoundMaker = type2Maker_->find(TypeIDBase(typeid(T))))) {
107  return false;
108  } else {
109  //Actually create the service in order to 'flush out' any
110  // configuration errors for the service
111  const_cast<ServicesManager&>(*this).createServiceFor(itFoundMaker->second);
112  itFound = type2Service_.find(TypeIDBase(typeid(T)));
113  //the 'add()' should have put the service into the list
114  assert(itFound != type2Service_.end());
115  }
116  }
117  return true;
118  }
119 
120  // ---------- static member functions --------------------
121 
122  // ---------- member functions ---------------------------
124  template <typename T>
125  bool put(std::shared_ptr<ServiceWrapper<T>> iPtr) {
126  Type2Service::const_iterator itFound = type2Service_.find(TypeIDBase(typeid(T)));
127  if (itFound != type2Service_.end()) {
128  return false;
129  }
130  type2Service_[TypeIDBase(typeid(T))] = iPtr;
131  actualCreationOrder_.push_back(TypeIDBase(typeid(T)));
132  return true;
133  }
134 
136  void connect(ActivityRegistry& iOther);
137 
139  void connectTo(ActivityRegistry& iOther);
140 
145 
146  private:
147  ServicesManager(ServicesManager const&) = delete; // stop default
148 
149  ServicesManager const& operator=(ServicesManager const&) = delete; // stop default
150 
151  void fillListOfMakers(std::vector<ParameterSet>&);
152  void createServices();
153  void createServiceFor(MakerHolder const&);
154 
155  // ---------- member data --------------------------------
156  //hold onto the Manager passed in from the ServiceToken so that
157  // the ActivityRegistry of that Manager does not go out of scope
158  // This must be first to get the Service destructors called in
159  // the correct order.
161 
165  std::vector<TypeIDBase> requestedCreationOrder_;
166  std::vector<TypeIDBase> actualCreationOrder_;
167  };
168  } // namespace serviceregistry
169 } // namespace edm
170 
171 #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:125
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:163
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::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::get
element_type const * get() const
Definition: propagate_const.h:64
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:132
edm::serviceregistry::ServicesManager::actualCreationOrder_
std::vector< TypeIDBase > actualCreationOrder_
Definition: ServicesManager.h:166
edm::serviceregistry::ServicesManager::MakerHolder
Definition: ServicesManager.h:46
edm::serviceregistry::ServicesManager::requestedCreationOrder_
std::vector< TypeIDBase > requestedCreationOrder_
Definition: ServicesManager.h:165
ServiceWrapper.h
edm::ParameterSet
Definition: ParameterSet.h:36
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:74
ServiceToken
T
long double T
Definition: Basic3DVectorLD.h:48
edm::serviceregistry::ServicesManager::registry_
ActivityRegistry registry_
Definition: ServicesManager.h:162
edm::serviceregistry::ServicesManager::type2Maker_
edm::propagate_const< std::unique_ptr< Type2Maker > > type2Maker_
Definition: ServicesManager.h:164
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:160
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:100
edm::serviceregistry::ServiceWrapper
Definition: ServiceRegistry.h:35