CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
28 #if !defined(__CINT__) && !defined(__MAKECINT__)
29 //CINT can't handle parsing these files
33 #endif
34 
35 // forward declarations
36 namespace fwlite {
37  class ErrorThrower;
38  class EventBase;
39 
40 template <class T>
41 class Handle
42 {
43 
44  public:
45  // this typedef is to avoid a Cint bug where calling
46  // edm::Wrapper<T>::typeInfo() fails for some types with
47  // very long expansions. First noticed for the expansion
48  // of reco::JetTracksAssociation::Container
50 
51  Handle() : data_(0), errorThrower_(ErrorThrower::unsetErrorThrower()) {}
52  ~Handle() { delete errorThrower_;}
53 
54  Handle(const Handle<T>& iOther) : data_(iOther.data_),
55  errorThrower_( iOther.errorThrower_? iOther.errorThrower_->clone(): 0) {}
56 
57  const Handle<T>& operator=(const Handle<T>& iOther) {
58  Handle<T> temp(iOther);
59  swap(temp);
60  return *this;
61  }
62 
63  // ---------- const member functions ---------------------
64  bool isValid() const { return data_ != 0; }
65 
67  bool failedToGet() const {return errorThrower_ != 0; }
68 
69  T const* product() const { check(); return data_;}
70 
71  const T* ptr() const { check(); return data_;}
72  const T& ref() const { check(); return *data_;}
73 
74  const T* operator->() const {
75  check();
76  return data_;
77  }
78 
79  const T& operator*() const {
80  check();
81  return *data_;
82  }
83  // ---------- static member functions --------------------
84 
85  // ---------- member functions ---------------------------
86  /*
87  void getByBranchName(const fwlite::Event& iEvent, const char* iBranchName) {
88  iEvent.getByBranchName(edm::Wrapper<T>::typeInfo(), iBranchName, data_);
89  }
90  */
91 
92  // P can be Run, Event, or LuminosityBlock
93 
94  template <class P> void getByLabel(const P& iP,
95  const char* iModuleLabel,
96  const char* iProductInstanceLabel = 0,
97  const char* iProcessLabel = 0) {
98  TempWrapT* temp;
99  void* pTemp = &temp;
100  iP.getByLabel(TempWrapT::typeInfo(),
101  iModuleLabel,
102  iProductInstanceLabel,
103  iProcessLabel,
104  pTemp);
105  delete errorThrower_;
106  errorThrower_ = 0;
107  if(0==temp) {
109  iModuleLabel,
110  iProductInstanceLabel,
111  iProcessLabel);
112  return;
113  }
114  data_ = temp->product();
115  if(data_==0) {
117  iModuleLabel,
118  iProductInstanceLabel,
119  iProcessLabel);
120  }
121  }
122 
123 
124  // void getByLabel(const fwlite::Event& iEvent,
125  // const char* iModuleLabel,
126  // const char* iProductInstanceLabel = 0,
127  // const char* iProcessLabel = 0) {
128  // TempWrapT* temp;
129  // void* pTemp = &temp;
130  // iEvent.getByLabel(TempWrapT::typeInfo(),
131  // iModuleLabel,
132  // iProductInstanceLabel,
133  // iProcessLabel,
134  // pTemp);
135  // delete errorThrower_;
136  // errorThrower_ = 0;
137  // if(0==temp) {
138  // errorThrower_=ErrorThrower::errorThrowerBranchNotFoundException(TempWrapT::typeInfo(),
139  // iModuleLabel,
140  // iProductInstanceLabel,
141  // iProcessLabel);
142  // return;
143  // }
144  // data_ = temp->product();
145  // if(data_==0) {
146  // errorThrower_=ErrorThrower::errorThrowerProductNotFoundException(TempWrapT::typeInfo(),
147  // iModuleLabel,
148  // iProductInstanceLabel,
149  // iProcessLabel);
150  // }
151  // }
152  //
153  // void getByLabel(const fwlite::ChainEvent& iEvent,
154  // const char* iModuleLabel,
155  // const char* iProductInstanceLabel = 0,
156  // const char* iProcessLabel = 0) {
157  // TempWrapT* temp;
158  // void* pTemp = &temp;
159  // iEvent.getByLabel(TempWrapT::typeInfo(),
160  // iModuleLabel,
161  // iProductInstanceLabel,
162  // iProcessLabel,
163  // pTemp);
164  // delete errorThrower_;
165  // errorThrower_ = 0;
166  // if(0==temp) {
167  // errorThrower_=ErrorThrower::errorThrowerBranchNotFoundException(TempWrapT::typeInfo(),
168  // iModuleLabel,
169  // iProductInstanceLabel,
170  // iProcessLabel);
171  // return;
172  // }
173  // data_ = temp->product();
174  // if(data_==0) {
175  // errorThrower_=ErrorThrower::errorThrowerProductNotFoundException(TempWrapT::typeInfo(),
176  // iModuleLabel,
177  // iProductInstanceLabel,
178  // iProcessLabel);
179  // }
180  // }
181  //
182  //
183  // void getByLabel(const fwlite::MultiChainEvent& iEvent,
184  // const char* iModuleLabel,
185  // const char* iProductInstanceLabel = 0,
186  // const char* iProcessLabel = 0) {
187  // TempWrapT* temp;
188  // void* pTemp = &temp;
189  // iEvent.getByLabel(TempWrapT::typeInfo(),
190  // iModuleLabel,
191  // iProductInstanceLabel,
192  // iProcessLabel,
193  // pTemp);
194  // if ( 0 != errorThrower_ ) delete errorThrower_;
195  // errorThrower_ = 0;
196  // if(0==temp) {
197  // errorThrower_=ErrorThrower::errorThrowerBranchNotFoundException(TempWrapT::typeInfo(),
198  // iModuleLabel,
199  // iProductInstanceLabel,
200  // iProcessLabel);
201  // return;
202  // }
203  // data_ = temp->product();
204  // if(data_==0) {
205  // errorThrower_=ErrorThrower::errorThrowerProductNotFoundException(TempWrapT::typeInfo(),
206  // iModuleLabel,
207  // iProductInstanceLabel,
208  // iProcessLabel);
209  // }
210  // }
211 
212  const std::string getBranchNameFor(const fwlite::EventBase& iEvent,
213  const char* iModuleLabel,
214  const char* iProductInstanceLabel = 0,
215  const char* iProcessLabel = 0)
216  {
217  return iEvent.getBranchNameFor(TempWrapT::typeInfo(),
218  iModuleLabel,
219  iProductInstanceLabel,
220  iProcessLabel);
221  }
222 
223  // const std::string getBranchNameFor(const fwlite::Event& iEvent,
224  // const char* iModuleLabel,
225  // const char* iProductInstanceLabel = 0,
226  // const char* iProcessLabel = 0) {
227  // return iEvent.getBranchNameFor(TempWrapT::typeInfo(),
228  // iModuleLabel,
229  // iProductInstanceLabel,
230  // iProcessLabel);
231  // }
232  //
233  // const std::string getBranchNameFor(const fwlite::ChainEvent& iEvent,
234  // const char* iModuleLabel,
235  // const char* iProductInstanceLabel = 0,
236  // const char* iProcessLabel = 0) {
237  // return iEvent.getBranchNameFor(TempWrapT::typeInfo(),
238  // iModuleLabel,
239  // iProductInstanceLabel,
240  // iProcessLabel);
241  // }
242  //
243  //
244  //
245  // const std::string getBranchNameFor(const fwlite::MultiChainEvent& iEvent,
246  // const char* iModuleLabel,
247  // const char* iProductInstanceLabel = 0,
248  // const char* iProcessLabel = 0) {
249  // return iEvent.getBranchNameFor(TempWrapT::typeInfo(),
250  // iModuleLabel,
251  // iProductInstanceLabel,
252  // iProcessLabel);
253  // }
254 
255  void swap( Handle<T>& iOther) {
256  const T* temp = data_;
257  data_ = iOther.data_;
258  iOther.data_ = temp;
259  ErrorThrower* tempE = errorThrower_;
260  errorThrower_ = iOther.errorThrower_;
261  iOther.errorThrower_ = tempE;
262  }
263  private:
264  void check() const { if(errorThrower_) { errorThrower_->throwIt();} }
265 
266 
267  // ---------- member data --------------------------------
268  const T* data_;
270 };
271 
272 }
273 #endif
bool isValid() const
Definition: Handle.h:64
static ErrorThrower * errorThrowerBranchNotFoundException(const std::type_info &, const char *, const char *, const char *)
#define P
virtual std::string const getBranchNameFor(std::type_info const &, char const *, char const *, char const *) const =0
const T * ptr() const
Definition: Handle.h:71
void check() const
Definition: Handle.h:264
void getByLabel(const P &iP, const char *iModuleLabel, const char *iProductInstanceLabel=0, const char *iProcessLabel=0)
Definition: Handle.h:94
int iEvent
Definition: GenABIO.cc:243
const std::string getBranchNameFor(const fwlite::EventBase &iEvent, const char *iModuleLabel, const char *iProductInstanceLabel=0, const char *iProcessLabel=0)
Definition: Handle.h:212
const T & ref() const
Definition: Handle.h:72
const T & operator*() const
Definition: Handle.h:79
void swap(Handle< T > &iOther)
Definition: Handle.h:255
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:67
const T * operator->() const
Definition: Handle.h:74
edm::Wrapper< T > TempWrapT
Definition: Handle.h:49
T const * product() const
Definition: Wrapper.h:36
ErrorThrower * errorThrower_
Definition: Handle.h:269
static ErrorThrower * errorThrowerProductNotFoundException(const std::type_info &, const char *, const char *, const char *)
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
Handle(const Handle< T > &iOther)
Definition: Handle.h:54
T const * product() const
Definition: Handle.h:69
const T * data_
Definition: Handle.h:268
long double T
const Handle< T > & operator=(const Handle< T > &iOther)
Definition: Handle.h:57
static std::type_info const & typeInfo()
Definition: Wrapper.h:41
virtual void throwIt() const =0