CMS 3D CMS Logo

Handle.h
Go to the documentation of this file.
1 #ifndef DataFormats_FWLite_Handle_h
2 #define DataFormats_FWLite_Handle_h
3 // -*- C++ -*-
4 //
5 // Package: FWLite
6 // Class : Handle
7 //
16 //
17 // Original Author: Chris Jones
18 // Created: Tue May 8 15:01:26 EDT 2007
19 //
20 
21 // system include files
22 
23 // user include files
24 namespace edm {
25  template<typename T> class Wrapper;
26 }
27 
31 
32 // forward declarations
33 namespace fwlite {
34  class ErrorThrower;
35  class EventBase;
36 
37 template <class T>
38 class Handle
39 {
40 
41  public:
42  // this typedef is to avoid a Cint bug where calling
43  // edm::Wrapper<T>::typeInfo() fails for some types with
44  // very long expansions. First noticed for the expansion
45  // of reco::JetTracksAssociation::Container
47 
48  Handle() : data_(nullptr), errorThrower_(ErrorThrower::unsetErrorThrower()) {}
49  ~Handle() { delete errorThrower_;}
50 
51  Handle(const Handle<T>& iOther) : data_(iOther.data_),
52  errorThrower_( iOther.errorThrower_? iOther.errorThrower_->clone(): nullptr) {}
53 
54  const Handle<T>& operator=(const Handle<T>& iOther) {
55  Handle<T> temp(iOther);
56  swap(temp);
57  return *this;
58  }
59 
60  // ---------- const member functions ---------------------
61  bool isValid() const { return data_ != nullptr; }
62 
64  bool failedToGet() const {return errorThrower_ != nullptr; }
65 
66  T const* product() const { check(); return data_;}
67 
68  const T* ptr() const { check(); return data_;}
69  const T& ref() const { check(); return *data_;}
70 
71  const T* operator->() const {
72  check();
73  return data_;
74  }
75 
76  const T& operator*() const {
77  check();
78  return *data_;
79  }
80  // ---------- static member functions --------------------
81 
82  // ---------- member functions ---------------------------
83  /*
84  void getByBranchName(const fwlite::Event& iEvent, const char* iBranchName) {
85  iEvent.getByBranchName(edm::Wrapper<T>::typeInfo(), iBranchName, data_);
86  }
87  */
88 
89  // P can be Run, Event, or LuminosityBlock
90 
91  template <class P> void getByLabel(const P& iP,
92  const char* iModuleLabel,
93  const char* iProductInstanceLabel = nullptr,
94  const char* iProcessLabel = nullptr) {
95  TempWrapT* temp;
96  void* pTemp = &temp;
97  iP.getByLabel(TempWrapT::typeInfo(),
98  iModuleLabel,
99  iProductInstanceLabel,
100  iProcessLabel,
101  pTemp);
102  delete errorThrower_;
103  errorThrower_ = nullptr;
104  if(nullptr == temp) {
105  errorThrower_=ErrorThrower::errorThrowerBranchNotFoundException(TempWrapT::typeInfo(),
106  iModuleLabel,
107  iProductInstanceLabel,
108  iProcessLabel);
109  return;
110  }
111  data_ = temp->product();
112  if(data_== nullptr) {
113  errorThrower_=ErrorThrower::errorThrowerProductNotFoundException(TempWrapT::typeInfo(),
114  iModuleLabel,
115  iProductInstanceLabel,
116  iProcessLabel);
117  }
118  }
119 
120 
121  // void getByLabel(const fwlite::Event& iEvent,
122  // const char* iModuleLabel,
123  // const char* iProductInstanceLabel = nullptr,
124  // const char* iProcessLabel = nullptr) {
125  // TempWrapT* temp;
126  // void* pTemp = &temp;
127  // iEvent.getByLabel(TempWrapT::typeInfo(),
128  // iModuleLabel,
129  // iProductInstanceLabel,
130  // iProcessLabel,
131  // pTemp);
132  // delete errorThrower_;
133  // errorThrower_ = nullptr;
134  // if(nullptr == temp) {
135  // errorThrower_=ErrorThrower::errorThrowerBranchNotFoundException(TempWrapT::typeInfo(),
136  // iModuleLabel,
137  // iProductInstanceLabel,
138  // iProcessLabel);
139  // return;
140  // }
141  // data_ = temp->product();
142  // if(data_ == nullptr) {
143  // errorThrower_=ErrorThrower::errorThrowerProductNotFoundException(TempWrapT::typeInfo(),
144  // iModuleLabel,
145  // iProductInstanceLabel,
146  // iProcessLabel);
147  // }
148  // }
149  //
150  // void getByLabel(const fwlite::ChainEvent& iEvent,
151  // const char* iModuleLabel,
152  // const char* iProductInstanceLabel = nullptr,
153  // const char* iProcessLabel = nullptr) {
154  // TempWrapT* temp;
155  // void* pTemp = &temp;
156  // iEvent.getByLabel(TempWrapT::typeInfo(),
157  // iModuleLabel,
158  // iProductInstanceLabel,
159  // iProcessLabel,
160  // pTemp);
161  // delete errorThrower_;
162  // errorThrower_ = nullptr;
163  // if(nullptr == temp) {
164  // errorThrower_=ErrorThrower::errorThrowerBranchNotFoundException(TempWrapT::typeInfo(),
165  // iModuleLabel,
166  // iProductInstanceLabel,
167  // iProcessLabel);
168  // return;
169  // }
170  // data_ = temp->product();
171  // if(data_ == nullptr) {
172  // errorThrower_=ErrorThrower::errorThrowerProductNotFoundException(TempWrapT::typeInfo(),
173  // iModuleLabel,
174  // iProductInstanceLabel,
175  // iProcessLabel);
176  // }
177  // }
178  //
179  //
180  // void getByLabel(const fwlite::MultiChainEvent& iEvent,
181  // const char* iModuleLabel,
182  // const char* iProductInstanceLabel = nullptr,
183  // const char* iProcessLabel = nullptr) {
184  // TempWrapT* temp;
185  // void* pTemp = &temp;
186  // iEvent.getByLabel(TempWrapT::typeInfo(),
187  // iModuleLabel,
188  // iProductInstanceLabel,
189  // iProcessLabel,
190  // pTemp);
191  // if ( nullptr != errorThrower_ ) delete errorThrower_;
192  // errorThrower_ = nullptr;
193  // if(nullptr == temp) {
194  // errorThrower_=ErrorThrower::errorThrowerBranchNotFoundException(TempWrapT::typeInfo(),
195  // iModuleLabel,
196  // iProductInstanceLabel,
197  // iProcessLabel);
198  // return;
199  // }
200  // data_ = temp->product();
201  // if(data_ == nullptr) {
202  // errorThrower_=ErrorThrower::errorThrowerProductNotFoundException(TempWrapT::typeInfo(),
203  // iModuleLabel,
204  // iProductInstanceLabel,
205  // iProcessLabel);
206  // }
207  // }
208 
210  const char* iModuleLabel,
211  const char* iProductInstanceLabel = nullptr,
212  const char* iProcessLabel = nullptr)
213  {
214  return iEvent.getBranchNameFor(TempWrapT::typeInfo(),
215  iModuleLabel,
216  iProductInstanceLabel,
217  iProcessLabel);
218  }
219 
220  // const std::string getBranchNameFor(const fwlite::Event& iEvent,
221  // const char* iModuleLabel,
222  // const char* iProductInstanceLabel = nullptr,
223  // const char* iProcessLabel = nullptr) {
224  // return iEvent.getBranchNameFor(TempWrapT::typeInfo(),
225  // iModuleLabel,
226  // iProductInstanceLabel,
227  // iProcessLabel);
228  // }
229  //
230  // const std::string getBranchNameFor(const fwlite::ChainEvent& iEvent,
231  // const char* iModuleLabel,
232  // const char* iProductInstanceLabel = nullptr,
233  // const char* iProcessLabel = nullptr) {
234  // return iEvent.getBranchNameFor(TempWrapT::typeInfo(),
235  // iModuleLabel,
236  // iProductInstanceLabel,
237  // iProcessLabel);
238  // }
239  //
240  //
241  //
242  // const std::string getBranchNameFor(const fwlite::MultiChainEvent& iEvent,
243  // const char* iModuleLabel,
244  // const char* iProductInstanceLabel = nullptr,
245  // const char* iProcessLabel = nullptr) {
246  // return iEvent.getBranchNameFor(TempWrapT::typeInfo(),
247  // iModuleLabel,
248  // iProductInstanceLabel,
249  // iProcessLabel);
250  // }
251 
252  void swap( Handle<T>& iOther) {
253  const T* temp = data_;
254  data_ = iOther.data_;
255  iOther.data_ = temp;
256  ErrorThrower const* tempE = errorThrower_;
257  errorThrower_ = iOther.errorThrower_;
258  iOther.errorThrower_ = tempE;
259  }
260  private:
261  void check() const { if(errorThrower_) { errorThrower_->throwIt();} }
262 
263 
264  // ---------- member data --------------------------------
265  const T* data_;
267 };
268 
269 }
270 
271 #endif
bool isValid() const
Definition: Handle.h:61
const T * ptr() const
Definition: Handle.h:68
#define nullptr
virtual std::string const getBranchNameFor(std::type_info const &, char const *, char const *, char const *) const =0
void check() const
Definition: Handle.h:261
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:116
void getByLabel(const P &iP, const char *iModuleLabel, const char *iProductInstanceLabel=0, const char *iProcessLabel=0)
Definition: Handle.h:91
int iEvent
Definition: GenABIO.cc:230
const std::string getBranchNameFor(const fwlite::EventBase &iEvent, const char *iModuleLabel, const char *iProductInstanceLabel=0, const char *iProcessLabel=0)
Definition: Handle.h:209
const T & ref() const
Definition: Handle.h:69
const T & operator*() const
Definition: Handle.h:76
void swap(Handle< T > &iOther)
Definition: Handle.h:252
bool failedToGet() const
Returns true only if Handle was used in a &#39;get&#39; call and the data could not be found.
Definition: Handle.h:64
const T * operator->() const
Definition: Handle.h:71
edm::Wrapper< T > TempWrapT
Definition: Handle.h:46
ErrorThrower const * errorThrower_
Definition: Handle.h:266
T const * product() const
Definition: Wrapper.h:32
std::pair< OmniClusterRef, TrackingParticleRef > P
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
HLT enums.
Handle(const Handle< T > &iOther)
Definition: Handle.h:51
T const * product() const
Definition: Handle.h:66
const T * data_
Definition: Handle.h:265
long double T
static void check(T const &p, std::string const &id, SelectedProducts const &iProducts)
const Handle< T > & operator=(const Handle< T > &iOther)
Definition: Handle.h:54