CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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
MakerHolder(std::shared_ptr< ServiceMakerBase > iMaker, ParameterSet &iPSet, ActivityRegistry &)
#define CMS_SA_ALLOW
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_
assert(be >=bs)
static void throwThis(Code category, char const *message0="", char const *message1="", char const *message2="", char const *message3="", char const *message4="")
Definition: EDMException.cc:83
std::vector< TypeIDBase > actualCreationOrder_
void connectTo(ActivityRegistry &iOther)
causes iOther&#39;s signals to be forward to us
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_
constexpr element_type const * get() const
ServicesManager(std::vector< ParameterSet > &iConfiguration)
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