CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros 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
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,
47  ParameterSet& iPSet,
49  bool add(ServicesManager&) const;
50 
53  ActivityRegistry* registry_; // We do not use propagate_const because the registry itself is mutable
54  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(0 == 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(0 != 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(0 == 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&); // stop default
148 
149  ServicesManager const& operator=(ServicesManager const&); // 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  }
169 }
170 
171 #endif
MakerHolder(std::shared_ptr< ServiceMakerBase > iMaker, ParameterSet &iPSet, ActivityRegistry &)
void fillListOfMakers(std::vector< ParameterSet > &)
void createServiceFor(MakerHolder const &)
void copySlotsTo(ActivityRegistry &)
copy our Service&#39;s slots to the argument&#39;s signals
assert(m_qm.get())
std::map< TypeIDBase, MakerHolder > Type2Maker
edm::propagate_const< std::shared_ptr< ServiceMakerBase > > maker_
bool put(std::shared_ptr< ServiceWrapper< T > > iPtr)
returns false if put fails because a service of this type already exists
static void throwThis(Code category, char const *message0="", char const *message1="", char const *message2="", char const *message3="", char const *message4="")
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)
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
ServicesManager const & operator=(ServicesManager const &)