Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:04:09

0001 #ifndef DataFormats_FWLite_Handle_h
0002 #define DataFormats_FWLite_Handle_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     FWLite
0006 // Class  :     Handle
0007 //
0008 /**\class Handle Handle.h DataFormats/FWLite/interface/Handle.h
0009 
0010  Description: <one line class summary>
0011 
0012  Usage:
0013     <usage>
0014 
0015 */
0016 //
0017 // Original Author:  Chris Jones
0018 //         Created:  Tue May  8 15:01:26 EDT 2007
0019 //
0020 
0021 // system include files
0022 
0023 // user include files
0024 namespace edm {
0025   template <typename T>
0026   class Wrapper;
0027 }
0028 
0029 #include "DataFormats/FWLite/interface/EventBase.h"
0030 #include "DataFormats/FWLite/interface/ErrorThrower.h"
0031 #include "DataFormats/Common/interface/Wrapper.h"
0032 
0033 // forward declarations
0034 namespace fwlite {
0035   class ErrorThrower;
0036   class EventBase;
0037 
0038   template <class T>
0039   class Handle {
0040   public:
0041     // this typedef is to avoid a Cint bug where calling
0042     // edm::Wrapper<T>::typeInfo() fails for some types with
0043     // very long expansions.  First noticed for the expansion
0044     // of reco::JetTracksAssociation::Container
0045     typedef edm::Wrapper<T> TempWrapT;
0046 
0047     Handle() : data_(nullptr), errorThrower_(ErrorThrower::unsetErrorThrower()) {}
0048     ~Handle() { delete errorThrower_; }
0049 
0050     Handle(const Handle<T>& iOther)
0051         : data_(iOther.data_), errorThrower_(iOther.errorThrower_ ? iOther.errorThrower_->clone() : nullptr) {}
0052 
0053     const Handle<T>& operator=(const Handle<T>& iOther) {
0054       Handle<T> temp(iOther);
0055       swap(temp);
0056       return *this;
0057     }
0058 
0059     // ---------- const member functions ---------------------
0060     bool isValid() const { return data_ != nullptr; }
0061 
0062     ///Returns true only if Handle was used in a 'get' call and the data could not be found
0063     bool failedToGet() const { return errorThrower_ != nullptr; }
0064 
0065     T const* product() const {
0066       check();
0067       return data_;
0068     }
0069 
0070     const T* ptr() const {
0071       check();
0072       return data_;
0073     }
0074     const T& ref() const {
0075       check();
0076       return *data_;
0077     }
0078 
0079     const T* operator->() const {
0080       check();
0081       return data_;
0082     }
0083 
0084     const T& operator*() const {
0085       check();
0086       return *data_;
0087     }
0088     // ---------- static member functions --------------------
0089 
0090     // ---------- member functions ---------------------------
0091     /*
0092       void getByBranchName(const fwlite::Event& iEvent, const char* iBranchName) {
0093         iEvent.getByBranchName(edm::Wrapper<T>::typeInfo(), iBranchName, data_);
0094       }
0095    */
0096 
0097     // P can be Run, Event, or LuminosityBlock
0098 
0099     template <class P>
0100     void getByLabel(const P& iP,
0101                     const char* iModuleLabel,
0102                     const char* iProductInstanceLabel = nullptr,
0103                     const char* iProcessLabel = nullptr) {
0104       TempWrapT* temp;
0105       void* pTemp = &temp;
0106       iP.getByLabel(TempWrapT::typeInfo(), iModuleLabel, iProductInstanceLabel, iProcessLabel, pTemp);
0107       delete errorThrower_;
0108       errorThrower_ = nullptr;
0109       if (nullptr == temp) {
0110         errorThrower_ = ErrorThrower::errorThrowerBranchNotFoundException(
0111             TempWrapT::typeInfo(), iModuleLabel, iProductInstanceLabel, iProcessLabel);
0112         return;
0113       }
0114       data_ = temp->product();
0115       if (data_ == nullptr) {
0116         errorThrower_ = ErrorThrower::errorThrowerProductNotFoundException(
0117             TempWrapT::typeInfo(), iModuleLabel, iProductInstanceLabel, iProcessLabel);
0118       }
0119     }
0120 
0121     // void getByLabel(const fwlite::Event& iEvent,
0122     //                 const char* iModuleLabel,
0123     //                 const char* iProductInstanceLabel = nullptr,
0124     //                 const char* iProcessLabel = nullptr) {
0125     //   TempWrapT* temp;
0126     //   void* pTemp = &temp;
0127     //   iEvent.getByLabel(TempWrapT::typeInfo(),
0128     //                     iModuleLabel,
0129     //                     iProductInstanceLabel,
0130     //                     iProcessLabel,
0131     //                     pTemp);
0132     //   delete errorThrower_;
0133     //   errorThrower_ = nullptr;
0134     //   if(nullptr == temp) {
0135     //      errorThrower_=ErrorThrower::errorThrowerBranchNotFoundException(TempWrapT::typeInfo(),
0136     //                                                                      iModuleLabel,
0137     //                                                                      iProductInstanceLabel,
0138     //                                                                      iProcessLabel);
0139     //  return;
0140     //   }
0141     //   data_ = temp->product();
0142     //   if(data_ == nullptr) {
0143     //      errorThrower_=ErrorThrower::errorThrowerProductNotFoundException(TempWrapT::typeInfo(),
0144     //                                                                       iModuleLabel,
0145     //                                                                       iProductInstanceLabel,
0146     //                                                                       iProcessLabel);
0147     //   }
0148     // }
0149     //
0150     // void getByLabel(const fwlite::ChainEvent& iEvent,
0151     //                 const char* iModuleLabel,
0152     //                 const char* iProductInstanceLabel = nullptr,
0153     //                 const char* iProcessLabel = nullptr) {
0154     // TempWrapT* temp;
0155     // void* pTemp = &temp;
0156     // iEvent.getByLabel(TempWrapT::typeInfo(),
0157     //                   iModuleLabel,
0158     //                   iProductInstanceLabel,
0159     //                   iProcessLabel,
0160     //                   pTemp);
0161     //    delete errorThrower_;
0162     //    errorThrower_ = nullptr;
0163     //    if(nullptr == temp) {
0164     //       errorThrower_=ErrorThrower::errorThrowerBranchNotFoundException(TempWrapT::typeInfo(),
0165     //                                                                       iModuleLabel,
0166     //                                                                       iProductInstanceLabel,
0167     //                                                                       iProcessLabel);
0168     //   return;
0169     //    }
0170     //    data_ = temp->product();
0171     //    if(data_ == nullptr) {
0172     //       errorThrower_=ErrorThrower::errorThrowerProductNotFoundException(TempWrapT::typeInfo(),
0173     //                                                                        iModuleLabel,
0174     //                                                                        iProductInstanceLabel,
0175     //                                                                        iProcessLabel);
0176     //    }
0177     // }
0178     //
0179     //
0180     // void getByLabel(const fwlite::MultiChainEvent& iEvent,
0181     //                 const char* iModuleLabel,
0182     //                 const char* iProductInstanceLabel = nullptr,
0183     //                 const char* iProcessLabel = nullptr) {
0184     // TempWrapT* temp;
0185     // void* pTemp = &temp;
0186     // iEvent.getByLabel(TempWrapT::typeInfo(),
0187     //                   iModuleLabel,
0188     //                   iProductInstanceLabel,
0189     //                   iProcessLabel,
0190     //                   pTemp);
0191     //    if ( nullptr != errorThrower_ ) delete errorThrower_;
0192     //    errorThrower_ = nullptr;
0193     //    if(nullptr == temp) {
0194     //       errorThrower_=ErrorThrower::errorThrowerBranchNotFoundException(TempWrapT::typeInfo(),
0195     //                                                                       iModuleLabel,
0196     //                                                                       iProductInstanceLabel,
0197     //                                                                       iProcessLabel);
0198     //   return;
0199     //    }
0200     //    data_ = temp->product();
0201     //    if(data_ == nullptr) {
0202     //       errorThrower_=ErrorThrower::errorThrowerProductNotFoundException(TempWrapT::typeInfo(),
0203     //                                                                        iModuleLabel,
0204     //                                                                        iProductInstanceLabel,
0205     //                                                                        iProcessLabel);
0206     //    }
0207     // }
0208 
0209     const std::string getBranchNameFor(const fwlite::EventBase& iEvent,
0210                                        const char* iModuleLabel,
0211                                        const char* iProductInstanceLabel = nullptr,
0212                                        const char* iProcessLabel = nullptr) {
0213       return iEvent.getBranchNameFor(TempWrapT::typeInfo(), iModuleLabel, iProductInstanceLabel, iProcessLabel);
0214     }
0215 
0216     // const std::string getBranchNameFor(const fwlite::Event& iEvent,
0217     //                                    const char* iModuleLabel,
0218     //                                    const char* iProductInstanceLabel = nullptr,
0219     //                                    const char* iProcessLabel = nullptr) {
0220     //    return iEvent.getBranchNameFor(TempWrapT::typeInfo(),
0221     //                                   iModuleLabel,
0222     //                                   iProductInstanceLabel,
0223     //                                   iProcessLabel);
0224     // }
0225     //
0226     // const std::string getBranchNameFor(const fwlite::ChainEvent& iEvent,
0227     //                                    const char* iModuleLabel,
0228     //                                    const char* iProductInstanceLabel = nullptr,
0229     //                                    const char* iProcessLabel = nullptr) {
0230     //    return iEvent.getBranchNameFor(TempWrapT::typeInfo(),
0231     //                                   iModuleLabel,
0232     //                                   iProductInstanceLabel,
0233     //                                   iProcessLabel);
0234     // }
0235     //
0236     //
0237     //
0238     // const std::string getBranchNameFor(const fwlite::MultiChainEvent& iEvent,
0239     //                                    const char* iModuleLabel,
0240     //                                    const char* iProductInstanceLabel = nullptr,
0241     //                                    const char* iProcessLabel = nullptr) {
0242     //    return iEvent.getBranchNameFor(TempWrapT::typeInfo(),
0243     //                                   iModuleLabel,
0244     //                                   iProductInstanceLabel,
0245     //                                   iProcessLabel);
0246     // }
0247 
0248     void swap(Handle<T>& iOther) {
0249       const T* temp = data_;
0250       data_ = iOther.data_;
0251       iOther.data_ = temp;
0252       ErrorThrower const* tempE = errorThrower_;
0253       errorThrower_ = iOther.errorThrower_;
0254       iOther.errorThrower_ = tempE;
0255     }
0256 
0257   private:
0258     void check() const {
0259       if (errorThrower_) {
0260         errorThrower_->throwIt();
0261       }
0262     }
0263 
0264     // ---------- member data --------------------------------
0265     const T* data_;
0266     ErrorThrower const* errorThrower_;
0267   };
0268 
0269 }  // namespace fwlite
0270 
0271 #endif