Handle

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
#ifndef DataFormats_FWLite_Handle_h
#define DataFormats_FWLite_Handle_h
// -*- C++ -*-
//
// Package:     FWLite
// Class  :     Handle
//
/**\class Handle Handle.h DataFormats/FWLite/interface/Handle.h

 Description: <one line class summary>

 Usage:
    <usage>

*/
//
// Original Author:  Chris Jones
//         Created:  Tue May  8 15:01:26 EDT 2007
//

// system include files

// user include files
namespace edm {
  template <typename T>
  class Wrapper;
}

#include "DataFormats/FWLite/interface/EventBase.h"
#include "DataFormats/FWLite/interface/ErrorThrower.h"
#include "DataFormats/Common/interface/Wrapper.h"

// forward declarations
namespace fwlite {
  class ErrorThrower;
  class EventBase;

  template <class T>
  class Handle {
  public:
    // this typedef is to avoid a Cint bug where calling
    // edm::Wrapper<T>::typeInfo() fails for some types with
    // very long expansions.  First noticed for the expansion
    // of reco::JetTracksAssociation::Container
    typedef edm::Wrapper<T> TempWrapT;

    Handle() : data_(nullptr), errorThrower_(ErrorThrower::unsetErrorThrower()) {}
    ~Handle() { delete errorThrower_; }

    Handle(const Handle<T>& iOther)
        : data_(iOther.data_), errorThrower_(iOther.errorThrower_ ? iOther.errorThrower_->clone() : nullptr) {}

    const Handle<T>& operator=(const Handle<T>& iOther) {
      Handle<T> temp(iOther);
      swap(temp);
      return *this;
    }

    // ---------- const member functions ---------------------
    bool isValid() const { return data_ != nullptr; }

    ///Returns true only if Handle was used in a 'get' call and the data could not be found
    bool failedToGet() const { return errorThrower_ != nullptr; }

    T const* product() const {
      check();
      return data_;
    }

    const T* ptr() const {
      check();
      return data_;
    }
    const T& ref() const {
      check();
      return *data_;
    }

    const T* operator->() const {
      check();
      return data_;
    }

    const T& operator*() const {
      check();
      return *data_;
    }
    // ---------- static member functions --------------------

    // ---------- member functions ---------------------------
    /*
      void getByBranchName(const fwlite::Event& iEvent, const char* iBranchName) {
        iEvent.getByBranchName(edm::Wrapper<T>::typeInfo(), iBranchName, data_);
      }
   */

    // P can be Run, Event, or LuminosityBlock

    template <class P>
    void getByLabel(const P& iP,
                    const char* iModuleLabel,
                    const char* iProductInstanceLabel = nullptr,
                    const char* iProcessLabel = nullptr) {
      TempWrapT* temp;
      void* pTemp = &temp;
      iP.getByLabel(TempWrapT::typeInfo(), iModuleLabel, iProductInstanceLabel, iProcessLabel, pTemp);
      delete errorThrower_;
      errorThrower_ = nullptr;
      if (nullptr == temp) {
        errorThrower_ = ErrorThrower::errorThrowerBranchNotFoundException(
            TempWrapT::typeInfo(), iModuleLabel, iProductInstanceLabel, iProcessLabel);
        return;
      }
      data_ = temp->product();
      if (data_ == nullptr) {
        errorThrower_ = ErrorThrower::errorThrowerProductNotFoundException(
            TempWrapT::typeInfo(), iModuleLabel, iProductInstanceLabel, iProcessLabel);
      }
    }

    // void getByLabel(const fwlite::Event& iEvent,
    //                 const char* iModuleLabel,
    //                 const char* iProductInstanceLabel = nullptr,
    //                 const char* iProcessLabel = nullptr) {
    //   TempWrapT* temp;
    //   void* pTemp = &temp;
    //   iEvent.getByLabel(TempWrapT::typeInfo(),
    //                     iModuleLabel,
    //                     iProductInstanceLabel,
    //                     iProcessLabel,
    //                     pTemp);
    //   delete errorThrower_;
    //   errorThrower_ = nullptr;
    //   if(nullptr == temp) {
    //      errorThrower_=ErrorThrower::errorThrowerBranchNotFoundException(TempWrapT::typeInfo(),
    //                                                                      iModuleLabel,
    //                                                                      iProductInstanceLabel,
    //                                                                      iProcessLabel);
    //  return;
    //   }
    //   data_ = temp->product();
    //   if(data_ == nullptr) {
    //      errorThrower_=ErrorThrower::errorThrowerProductNotFoundException(TempWrapT::typeInfo(),
    //                                                                       iModuleLabel,
    //                                                                       iProductInstanceLabel,
    //                                                                       iProcessLabel);
    //   }
    // }
    //
    // void getByLabel(const fwlite::ChainEvent& iEvent,
    //                 const char* iModuleLabel,
    //                 const char* iProductInstanceLabel = nullptr,
    //                 const char* iProcessLabel = nullptr) {
    // TempWrapT* temp;
    // void* pTemp = &temp;
    // iEvent.getByLabel(TempWrapT::typeInfo(),
    //                   iModuleLabel,
    //                   iProductInstanceLabel,
    //                   iProcessLabel,
    //                   pTemp);
    //    delete errorThrower_;
    //    errorThrower_ = nullptr;
    //    if(nullptr == temp) {
    //       errorThrower_=ErrorThrower::errorThrowerBranchNotFoundException(TempWrapT::typeInfo(),
    //                                                                       iModuleLabel,
    //                                                                       iProductInstanceLabel,
    //                                                                       iProcessLabel);
    //   return;
    //    }
    //    data_ = temp->product();
    //    if(data_ == nullptr) {
    //       errorThrower_=ErrorThrower::errorThrowerProductNotFoundException(TempWrapT::typeInfo(),
    //                                                                        iModuleLabel,
    //                                                                        iProductInstanceLabel,
    //                                                                        iProcessLabel);
    //    }
    // }
    //
    //
    // void getByLabel(const fwlite::MultiChainEvent& iEvent,
    //                 const char* iModuleLabel,
    //                 const char* iProductInstanceLabel = nullptr,
    //                 const char* iProcessLabel = nullptr) {
    // TempWrapT* temp;
    // void* pTemp = &temp;
    // iEvent.getByLabel(TempWrapT::typeInfo(),
    //                   iModuleLabel,
    //                   iProductInstanceLabel,
    //                   iProcessLabel,
    //                   pTemp);
    //    if ( nullptr != errorThrower_ ) delete errorThrower_;
    //    errorThrower_ = nullptr;
    //    if(nullptr == temp) {
    //       errorThrower_=ErrorThrower::errorThrowerBranchNotFoundException(TempWrapT::typeInfo(),
    //                                                                       iModuleLabel,
    //                                                                       iProductInstanceLabel,
    //                                                                       iProcessLabel);
    //   return;
    //    }
    //    data_ = temp->product();
    //    if(data_ == nullptr) {
    //       errorThrower_=ErrorThrower::errorThrowerProductNotFoundException(TempWrapT::typeInfo(),
    //                                                                        iModuleLabel,
    //                                                                        iProductInstanceLabel,
    //                                                                        iProcessLabel);
    //    }
    // }

    const std::string getBranchNameFor(const fwlite::EventBase& iEvent,
                                       const char* iModuleLabel,
                                       const char* iProductInstanceLabel = nullptr,
                                       const char* iProcessLabel = nullptr) {
      return iEvent.getBranchNameFor(TempWrapT::typeInfo(), iModuleLabel, iProductInstanceLabel, iProcessLabel);
    }

    // const std::string getBranchNameFor(const fwlite::Event& iEvent,
    //                                    const char* iModuleLabel,
    //                                    const char* iProductInstanceLabel = nullptr,
    //                                    const char* iProcessLabel = nullptr) {
    //    return iEvent.getBranchNameFor(TempWrapT::typeInfo(),
    //                                   iModuleLabel,
    //                                   iProductInstanceLabel,
    //                                   iProcessLabel);
    // }
    //
    // const std::string getBranchNameFor(const fwlite::ChainEvent& iEvent,
    //                                    const char* iModuleLabel,
    //                                    const char* iProductInstanceLabel = nullptr,
    //                                    const char* iProcessLabel = nullptr) {
    //    return iEvent.getBranchNameFor(TempWrapT::typeInfo(),
    //                                   iModuleLabel,
    //                                   iProductInstanceLabel,
    //                                   iProcessLabel);
    // }
    //
    //
    //
    // const std::string getBranchNameFor(const fwlite::MultiChainEvent& iEvent,
    //                                    const char* iModuleLabel,
    //                                    const char* iProductInstanceLabel = nullptr,
    //                                    const char* iProcessLabel = nullptr) {
    //    return iEvent.getBranchNameFor(TempWrapT::typeInfo(),
    //                                   iModuleLabel,
    //                                   iProductInstanceLabel,
    //                                   iProcessLabel);
    // }

    void swap(Handle<T>& iOther) {
      const T* temp = data_;
      data_ = iOther.data_;
      iOther.data_ = temp;
      ErrorThrower const* tempE = errorThrower_;
      errorThrower_ = iOther.errorThrower_;
      iOther.errorThrower_ = tempE;
    }

  private:
    void check() const {
      if (errorThrower_) {
        errorThrower_->throwIt();
      }
    }

    // ---------- member data --------------------------------
    const T* data_;
    ErrorThrower const* errorThrower_;
  };

}  // namespace fwlite

#endif