CMS 3D CMS Logo

Handle.h

Go to the documentation of this file.
00001 #ifndef DataFormats_FWLite_Handle_h
00002 #define DataFormats_FWLite_Handle_h
00003 // -*- C++ -*-
00004 //
00005 // Package:     FWLite
00006 // Class  :     Handle
00007 // 
00016 //
00017 // Original Author:  Chris Jones
00018 //         Created:  Tue May  8 15:01:26 EDT 2007
00019 // $Id: Handle.h,v 1.11 2008/09/23 16:59:16 chrjones Exp $
00020 //
00021 
00022 // system include files
00023 
00024 // user include files
00025 #if !defined(__CINT__) && !defined(__MAKECINT__)
00026 //CINT can't handle parsing these files
00027 #include "DataFormats/Common/interface/Wrapper.h"
00028 #include "DataFormats/FWLite/interface/Event.h"
00029 #include "DataFormats/FWLite/interface/ChainEvent.h"
00030 #include "DataFormats/FWLite/interface/ErrorThrower.h"
00031 #endif
00032 
00033 // forward declarations
00034 namespace fwlite {
00035    class ErrorThrower;
00036    
00037 template <class T>
00038 class Handle
00039 {
00040 
00041    public:
00042       // this typedef is to avoid a Cint bug where calling
00043       // edm::Wrapper<T>::typeInfo() fails for some types with
00044       // very long expansions.  First noticed for the expansion
00045       // of reco::JetTracksAssociation::Container
00046       typedef edm::Wrapper<T> TempWrapT;
00047  
00048       Handle() : data_(0), errorThrower_(ErrorThrower::unsetErrorThrower()) {}
00049       ~Handle() { delete errorThrower_;}
00050 
00051       Handle(const Handle<T>& iOther) : data_(iOther.data_),
00052       errorThrower_( iOther.errorThrower_? iOther.errorThrower_->clone(): 0) {}
00053    
00054       const Handle<T>& operator=(const Handle<T>& iOther) {
00055          Handle<T> temp(iOther);
00056          swap(iOther);
00057       }
00058    
00059       // ---------- const member functions ---------------------
00060       bool isValid() const { return data_ != 0; }
00061    
00063       bool failedToGet() const {return errorThrower_ != 0; }
00064    
00065       T const* product() const { check(); return data_;}
00066 
00067       const T* ptr() const { check(); return data_;}
00068       const T& ref() const { check(); return *data_;}
00069   
00070       const T* operator->() const {
00071         check();
00072         return data_;
00073       }
00074   
00075       const T& operator*() const {
00076         check();
00077         return *data_;
00078       }
00079       // ---------- static member functions --------------------
00080 
00081       // ---------- member functions ---------------------------
00082   /*
00083       void getByBranchName(const fwlite::Event& iEvent, const char* iBranchName) {
00084         iEvent.getByBranchName(edm::Wrapper<T>::typeInfo(), iBranchName, data_);
00085       }
00086    */
00087   
00088       void getByLabel(const fwlite::Event& iEvent, 
00089                       const char* iModuleLabel,
00090                       const char* iProductInstanceLabel = 0,
00091                       const char* iProcessLabel = 0) {
00092         TempWrapT* temp;
00093         void* pTemp = &temp;
00094         iEvent.getByLabel(TempWrapT::typeInfo(),
00095                           iModuleLabel,
00096                           iProductInstanceLabel,
00097                           iProcessLabel,
00098                           pTemp);
00099         delete errorThrower_;
00100         errorThrower_ = 0;
00101         if(0==temp) {
00102            errorThrower_=ErrorThrower::errorThrowerBranchNotFoundException(TempWrapT::typeInfo(),
00103                                                                            iModuleLabel,
00104                                                                            iProductInstanceLabel,
00105                                                                            iProcessLabel);
00106            return;
00107         }
00108         data_ = temp->product();
00109         if(data_==0) {
00110            errorThrower_=ErrorThrower::errorThrowerProductNotFoundException(TempWrapT::typeInfo(),
00111                                                                             iModuleLabel,
00112                                                                             iProductInstanceLabel,
00113                                                                             iProcessLabel);
00114         }
00115       }
00116 
00117       void getByLabel(const fwlite::ChainEvent& iEvent, 
00118                       const char* iModuleLabel,
00119                       const char* iProductInstanceLabel = 0,
00120                       const char* iProcessLabel = 0) {
00121       TempWrapT* temp;
00122       void* pTemp = &temp;
00123       iEvent.getByLabel(TempWrapT::typeInfo(),
00124                         iModuleLabel,
00125                         iProductInstanceLabel,
00126                         iProcessLabel,
00127                         pTemp);
00128          delete errorThrower_;
00129          errorThrower_ = 0;
00130          if(0==temp) {
00131             errorThrower_=ErrorThrower::errorThrowerBranchNotFoundException(TempWrapT::typeInfo(),
00132                                                                             iModuleLabel,
00133                                                                             iProductInstanceLabel,
00134                                                                             iProcessLabel);
00135             return;
00136          }
00137          data_ = temp->product();
00138          if(data_==0) {
00139             errorThrower_=ErrorThrower::errorThrowerProductNotFoundException(TempWrapT::typeInfo(),
00140                                                                              iModuleLabel,
00141                                                                              iProductInstanceLabel,
00142                                                                              iProcessLabel);
00143          }
00144       }
00145 
00146     const std::string getBranchNameFor(const fwlite::Event& iEvent, 
00147                                        const char* iModuleLabel,
00148                                        const char* iProductInstanceLabel = 0,
00149                                        const char* iProcessLabel = 0) {
00150       return iEvent.getBranchNameFor(TempWrapT::typeInfo(),
00151                                      iModuleLabel,
00152                                      iProductInstanceLabel,
00153                                      iProcessLabel);
00154     }
00155 
00156     const std::string getBranchNameFor(const fwlite::ChainEvent& iEvent, 
00157                                        const char* iModuleLabel,
00158                                        const char* iProductInstanceLabel = 0,
00159                                        const char* iProcessLabel = 0) {
00160       return iEvent.getBranchNameFor(TempWrapT::typeInfo(),
00161                                      iModuleLabel,
00162                                      iProductInstanceLabel,
00163                                      iProcessLabel);
00164     }
00165 
00166    void swap( Handle<T>& iOther) {
00167       const T* temp = data_;
00168       data_ = iOther.data_;
00169       iOther.data_ = temp;
00170       ErrorThrower* tempE = errorThrower_;
00171       errorThrower_ = iOther.errorThrower_;
00172       iOther.errorThrower = tempE;
00173    }
00174    private:
00175       void check() const { if(errorThrower_) { errorThrower_->throwIt();} }
00176 
00177 
00178       // ---------- member data --------------------------------
00179       const T* data_;
00180       ErrorThrower*  errorThrower_;
00181 };
00182 
00183 }
00184 #endif

Generated on Tue Jun 9 17:29:04 2009 for CMSSW by  doxygen 1.5.4