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
28 
29 // system include files
30 #include <memory>
31 
32 #include <cassert>
33 #include <vector>
34 
35 // forward declarations
36 namespace edm {
37  class ParameterSet;
38  class ServiceToken;
39 
40  namespace serviceregistry {
41 
43 public:
44  struct MakerHolder {
45  MakerHolder(std::shared_ptr<ServiceMakerBase> iMaker,
46  ParameterSet& iPSet,
48  bool add(ServicesManager&) const;
49 
50  std::shared_ptr<ServiceMakerBase> maker_;
53  mutable bool wasAdded_;
54  };
55  typedef std::map<TypeIDBase, std::shared_ptr<ServiceWrapperBase> > Type2Service;
56  typedef std::map<TypeIDBase, MakerHolder> Type2Maker;
57 
58  ServicesManager(std::vector<ParameterSet>& iConfiguration);
59 
65  ServiceLegacy iLegacy,
66  std::vector<ParameterSet>& iConfiguration,
67  bool associate = true);
68 
70 
71  // ---------- const member functions ---------------------
72  template<typename T>
73  T& get() const {
74  Type2Service::const_iterator itFound = type2Service_.find(TypeIDBase(typeid(T)));
75  Type2Maker::const_iterator itFoundMaker ;
76  if(itFound == type2Service_.end()) {
77  //do on demand building of the service
78  if(0 == type2Maker_.get() ||
79  type2Maker_->end() == (itFoundMaker = type2Maker_->find(TypeIDBase(typeid(T))))) {
81  "Service Request unable to find requested service with compiler type name '",
82  typeid(T).name(),
83  "'.\n");
84  } else {
85  itFoundMaker->second.add(const_cast<ServicesManager&>(*this));
86  itFound = type2Service_.find(TypeIDBase(typeid(T)));
87  //the 'add()' should have put the service into the list
88  assert(itFound != type2Service_.end());
89  }
90  }
91  //convert it to its actual type
92  std::shared_ptr<ServiceWrapper<T> > ptr(std::dynamic_pointer_cast<ServiceWrapper<T> >(itFound->second));
93  assert(0 != ptr.get());
94  return ptr->get();
95  }
96 
98  template<typename T>
99  bool isAvailable() const {
100  Type2Service::const_iterator itFound = type2Service_.find(TypeIDBase(typeid(T)));
101  Type2Maker::const_iterator itFoundMaker ;
102  if(itFound == type2Service_.end()) {
103  //do on demand building of the service
104  if(0 == type2Maker_.get() ||
105  type2Maker_->end() == (itFoundMaker = type2Maker_->find(TypeIDBase(typeid(T))))) {
106  return false;
107  } else {
108  //Actually create the service in order to 'flush out' any
109  // configuration errors for the service
110  itFoundMaker->second.add(const_cast<ServicesManager&>(*this));
111  itFound = type2Service_.find(TypeIDBase(typeid(T)));
112  //the 'add()' should have put the service into the list
113  assert(itFound != type2Service_.end());
114  }
115  }
116  return true;
117  }
118 
119  // ---------- static member functions --------------------
120 
121  // ---------- member functions ---------------------------
123  template<typename T>
124  bool put(std::shared_ptr<ServiceWrapper<T> > iPtr) {
125  Type2Service::const_iterator itFound = type2Service_.find(TypeIDBase(typeid(T)));
126  if(itFound != type2Service_.end()) {
127  return false;
128  }
129  type2Service_[ TypeIDBase(typeid(T)) ] = iPtr;
130  actualCreationOrder_.push_back(TypeIDBase(typeid(T)));
131  return true;
132  }
133 
135  void connect(ActivityRegistry& iOther);
136 
138  void connectTo(ActivityRegistry& iOther);
139 
144 
145 private:
146  ServicesManager(ServicesManager const&); // stop default
147 
148  ServicesManager const& operator=(ServicesManager const&); // stop default
149 
150  void fillListOfMakers(std::vector<ParameterSet>&);
151  void createServices();
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.
158  std::shared_ptr<ServicesManager> associatedManager_;
159 
162  std::auto_ptr<Type2Maker> type2Maker_;
163  std::vector<TypeIDBase> requestedCreationOrder_;
164  std::vector<TypeIDBase> actualCreationOrder_;
165  };
166  }
167 }
168 
169 #endif
MakerHolder(std::shared_ptr< ServiceMakerBase > iMaker, ParameterSet &iPSet, ActivityRegistry &)
void fillListOfMakers(std::vector< ParameterSet > &)
void copySlotsTo(ActivityRegistry &)
copy our Service&#39;s slots to the argument&#39;s signals
std::shared_ptr< ServiceMakerBase > maker_
assert(m_qm.get())
std::map< TypeIDBase, MakerHolder > Type2Maker
std::auto_ptr< Type2Maker > type2Maker_
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
void copySlotsFrom(ActivityRegistry &)
the copy the argument&#39;s slots to the our signals
std::vector< TypeIDBase > requestedCreationOrder_
ServicesManager(std::vector< ParameterSet > &iConfiguration)
bool isAvailable() const
returns true of the particular service is accessible
std::map< TypeIDBase, std::shared_ptr< ServiceWrapperBase > > Type2Service
long double T
void connect(ActivityRegistry &iOther)
causes our ActivityRegistry&#39;s signals to be forwarded to iOther
std::shared_ptr< ServicesManager > associatedManager_
ServicesManager const & operator=(ServicesManager const &)