Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:02:16

0001 // -*- C++ -*-
0002 //
0003 // Package:     Framework
0004 // Class  :     GenericHandle
0005 //
0006 // Implementation:
0007 //     <Notes on implementation>
0008 //
0009 // Original Author:  Chris Jones
0010 //         Created:  Thu Mar 30 15:48:37 EST 2006
0011 //
0012 
0013 // system include files
0014 
0015 // user include files
0016 #include "FWCore/Framework/interface/GenericHandle.h"
0017 
0018 namespace edm {
0019   void convert_handle(BasicHandle&& orig, Handle<GenericObject>& result) {
0020     if (orig.failedToGet()) {
0021       result.setWhyFailedFactory(orig.whyFailedFactory());
0022       return;
0023     }
0024     WrapperBase const* originalWrap = orig.wrapper();
0025     if (originalWrap == nullptr) {
0026       throw Exception(errors::InvalidReference, "NullPointer") << "edm::BasicHandle has null pointer to Wrapper";
0027     }
0028 
0029     ObjectWithDict wrap(originalWrap->wrappedTypeInfo(), const_cast<WrapperBase*>(originalWrap));
0030     assert(bool(wrap));
0031 
0032     ObjectWithDict product(wrap.get("obj"));
0033     if (!product) {
0034       throw Exception(errors::LogicError) << "GenericObject could not find 'obj' member";
0035     }
0036     if (product.typeOf() != result.type()) {
0037       throw Exception(errors::LogicError)
0038           << "GenericObject asked for " << result.type().name() << " but was given a " << product.typeOf().name();
0039     }
0040 
0041     Handle<GenericObject> h(product, orig.provenance(), orig.id());
0042     h.swap(result);
0043   }
0044 
0045   ///Specialize the getByLabel method to work with a Handle<GenericObject>
0046   template <>
0047   bool Event::getByLabel<GenericObject>(std::string const& label,
0048                                         std::string const& productInstanceName,
0049                                         Handle<GenericObject>& result) const {
0050     BasicHandle bh = provRecorder_.getByLabel_(
0051         TypeID(result.type().typeInfo()), label, productInstanceName, std::string(), moduleCallingContext_);
0052     convert_handle(std::move(bh), result);  // throws on conversion error
0053     if (!result.failedToGet()) {
0054       addToGotBranchIDs(*bh.provenance());
0055       return true;
0056     }
0057     return false;
0058   }
0059 
0060   template <>
0061   bool Event::getByLabel<GenericObject>(InputTag const& tag, Handle<GenericObject>& result) const {
0062     if (tag.process().empty()) {
0063       return this->getByLabel(tag.label(), tag.instance(), result);
0064     } else {
0065       BasicHandle bh = provRecorder_.getByLabel_(
0066           TypeID(result.type().typeInfo()), tag.label(), tag.instance(), tag.process(), moduleCallingContext_);
0067       convert_handle(std::move(bh), result);  // throws on conversion error
0068       if (!result.failedToGet()) {
0069         addToGotBranchIDs(*bh.provenance());
0070         return true;
0071       }
0072     }
0073     return false;
0074   }
0075 
0076   ///Specialize the Event's getByToken method to work with a Handle<GenericObject>
0077   template <>
0078   bool Event::getByToken<GenericObject>(EDGetToken token, Handle<GenericObject>& result) const {
0079     result.clear();
0080     BasicHandle bh =
0081         provRecorder_.getByToken_(TypeID(result.type().typeInfo()), PRODUCT_TYPE, token, moduleCallingContext_);
0082     convert_handle(std::move(bh), result);  // throws on conversion error
0083     if (UNLIKELY(result.failedToGet())) {
0084       return false;
0085     }
0086     addToGotBranchIDs(*result.provenance());
0087     return true;
0088   }
0089 
0090 }  // namespace edm