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
29 
30 // system include files
31 #include <memory>
32 
33 #include <cassert>
34 #include <vector>
35 
36 // forward declarations
37 namespace edm {
38  class ParameterSet;
39  class ServiceToken;
40 
41  namespace serviceregistry {
42 
44  public:
45  struct MakerHolder {
46  MakerHolder(std::shared_ptr<ServiceMakerBase> iMaker, ParameterSet& iPSet, ActivityRegistry&);
47  bool add(ServicesManager&) const;
48 
51  ActivityRegistry* registry_; // We do not use propagate_const because the registry itself is mutable
52  mutable bool wasAdded_;
53  };
54  typedef std::map<TypeIDBase, std::shared_ptr<ServiceWrapperBase>> Type2Service;
55  typedef std::map<TypeIDBase, MakerHolder> Type2Maker;
56 
57  ServicesManager(std::vector<ParameterSet>& iConfiguration);
58 
64  ServiceLegacy iLegacy,
65  std::vector<ParameterSet>& iConfiguration,
66  bool associate = true);
67 
69 
70  // ---------- const member functions ---------------------
71  template <typename T>
72  T& get() const {
73  Type2Service::const_iterator itFound = type2Service_.find(TypeIDBase(typeid(T)));
74  Type2Maker::const_iterator itFoundMaker;
75  if (itFound == type2Service_.end()) {
76  //do on demand building of the service
77  if (nullptr == type2Maker_.get() ||
78  type2Maker_->end() == (itFoundMaker = type2Maker_->find(TypeIDBase(typeid(T))))) {
80  "Service Request unable to find requested service with compiler type name '",
81  typeid(T).name(),
82  "'.\n");
83  } else {
84  const_cast<ServicesManager&>(*this).createServiceFor(itFoundMaker->second);
85  itFound = type2Service_.find(TypeIDBase(typeid(T)));
86  //the 'add()' should have put the service into the list
87  assert(itFound != type2Service_.end());
88  }
89  }
90  //convert it to its actual type
91  std::shared_ptr<ServiceWrapper<T>> ptr(std::dynamic_pointer_cast<ServiceWrapper<T>>(itFound->second));
92  assert(nullptr != ptr.get());
93  return ptr->get();
94  }
95 
97  template <typename T>
98  bool isAvailable() const {
99  Type2Service::const_iterator itFound = type2Service_.find(TypeIDBase(typeid(T)));
100  Type2Maker::const_iterator itFoundMaker;
101  if (itFound == type2Service_.end()) {
102  //do on demand building of the service
103  if (nullptr == type2Maker_.get() ||
104  type2Maker_->end() == (itFoundMaker = type2Maker_->find(TypeIDBase(typeid(T))))) {
105  return false;
106  } else {
107  //Actually create the service in order to 'flush out' any
108  // configuration errors for the service
109  const_cast<ServicesManager&>(*this).createServiceFor(itFoundMaker->second);
110  itFound = type2Service_.find(TypeIDBase(typeid(T)));
111  //the 'add()' should have put the service into the list
112  assert(itFound != type2Service_.end());
113  }
114  }
115  return true;
116  }
117 
118  // ---------- static member functions --------------------
119 
120  // ---------- member functions ---------------------------
122  template <typename T>
123  bool put(std::shared_ptr<ServiceWrapper<T>> iPtr) {
124  Type2Service::const_iterator itFound = type2Service_.find(TypeIDBase(typeid(T)));
125  if (itFound != type2Service_.end()) {
126  return false;
127  }
128  type2Service_[TypeIDBase(typeid(T))] = iPtr;
129  actualCreationOrder_.push_back(TypeIDBase(typeid(T)));
130  return true;
131  }
132 
134  void connect(ActivityRegistry& iOther);
135 
137  void connectTo(ActivityRegistry& iOther);
138 
143 
144  private:
145  ServicesManager(ServicesManager const&) = delete; // stop default
146 
147  ServicesManager const& operator=(ServicesManager const&) = delete; // stop default
148 
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 
161  Type2Service type2Service_;
163  std::vector<TypeIDBase> requestedCreationOrder_;
164  std::vector<TypeIDBase> actualCreationOrder_;
165  };
166  } // namespace serviceregistry
167 } // namespace edm
168 
169 #endif
MakerHolder(std::shared_ptr< ServiceMakerBase > iMaker, ParameterSet &iPSet, ActivityRegistry &)
void fillListOfMakers(std::vector< ParameterSet > &)
ServicesManager const & operator=(ServicesManager const &)=delete
void createServiceFor(MakerHolder const &)
void copySlotsTo(ActivityRegistry &)
copy our Service&#39;s slots to the argument&#39;s signals
bool put(std::shared_ptr< ServiceWrapper< T >> iPtr)
returns false if put fails because a service of this type already exists
std::map< TypeIDBase, MakerHolder > Type2Maker
edm::propagate_const< std::shared_ptr< ServiceMakerBase > > maker_
static void throwThis(Code category, char const *message0="", char const *message1="", char const *message2="", char const *message3="", char const *message4="")
Definition: EDMException.cc:82
std::vector< TypeIDBase > actualCreationOrder_
void connectTo(ActivityRegistry &iOther)
causes iOther&#39;s signals to be forward to us
element_type const * get() const
void copySlotsFrom(ActivityRegistry &)
the copy the argument&#39;s slots to the our signals
std::vector< TypeIDBase > requestedCreationOrder_
edm::propagate_const< std::unique_ptr< Type2Maker > > type2Maker_
ServicesManager(std::vector< ParameterSet > &iConfiguration)
HLT enums.
bool isAvailable() const
returns true of the particular service is accessible
std::map< TypeIDBase, std::shared_ptr< ServiceWrapperBase > > Type2Service
edm::propagate_const< std::shared_ptr< ServicesManager > > associatedManager_
long double T
void connect(ActivityRegistry &iOther)
causes our ActivityRegistry&#39;s signals to be forwarded to iOther