Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:11:38

0001 #ifndef Fireworks_FWGenericHandle_h
0002 #define Fireworks_FWGenericHandle_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     Framework
0006 // Class  :     FWGenericHandle
0007 //
0008 /**\class FWGenericHandle FWGenericHandle.h Fireworks/Core/interface/FWGenericHandle.h
0009 
0010  Description: Allows interaction with data in the Event without actually using 
0011               the C++ class. Ported to work with 
0012 
0013  Usage:
0014     This is a rip-off of edm::FWGenericHandle. I extended it to work with 
0015     edm::EventBase as well.
0016 
0017     The FWGenericHandle allows one to get data back from the edm::EventBase as 
0018     a edm::ObjectWithDict instead of as the actual C++ class type.
0019 
0020     //make a handle to hold an instance of 'MyClass'
0021     edm::FWGenericHandle myHandle("MyClass");
0022     
0023     event.getByLabel("mine",myHandle);
0024     
0025     //call the print method of 'MyClass' instance
0026     myHandle->invoke("print);  
0027 */
0028 //
0029 // Original Author:  Chris Jones
0030 //         Created:  Sat Jan  7 15:40:43 EST 2006
0031 //
0032 
0033 // system include files
0034 #include <string>
0035 
0036 // user include files
0037 #include "FWCore/Reflection/interface/ObjectWithDict.h"
0038 #include "FWCore/Common/interface/EventBase.h"
0039 #include "DataFormats/Common/interface/Handle.h"
0040 #include "DataFormats/Common/interface/ConvertHandle.h"
0041 
0042 // forward declarations
0043 namespace edm {
0044   ///This class is just a 'tag' used to allow a specialization of edm::Handle
0045   struct FWGenericObject {};
0046 
0047   template <>
0048   class Handle<FWGenericObject> {
0049   public:
0050     ///Throws exception if iName is not a known C++ class type
0051     Handle(std::string const& iName) : type_(edm::TypeWithDict::byName(iName)), prod_(), prov_(nullptr) {
0052       if (type_ == edm::TypeWithDict()) {
0053         Exception::throwThis(errors::NotFound,
0054                              "Handle<FWGenericObject> told to use uknown type '",
0055                              iName.c_str(),
0056                              "'.\n Please check spelling or that a module uses this type in the job.");
0057       }
0058     }
0059 
0060     ///Throws exception if iType is invalid
0061     Handle(edm::TypeWithDict const& iType) : type_(iType), prod_(), prov_(nullptr) {
0062       if (iType == edm::TypeWithDict()) {
0063         Exception::throwThis(errors::NotFound, "Handle<FWGenericObject> given an invalid edm::TypeWithDict");
0064       }
0065     }
0066 
0067     Handle(Handle<FWGenericObject> const& h)
0068         : type_(h.type_), prod_(h.prod_), prov_(h.prov_), whyFailed_(h.whyFailed_) {}
0069 
0070     Handle(edm::ObjectWithDict const& prod, Provenance const* prov, ProductID const& pid)
0071         : type_(prod.typeOf()), prod_(prod), prov_(prov) {
0072       assert(prod_);
0073       assert(prov_);
0074       // assert(prov_->productID() != ProductID());
0075     }
0076 
0077     //~Handle();
0078 
0079     void swap(Handle<FWGenericObject>& other) {
0080       // use unqualified swap for user defined classes
0081       using std::swap;
0082       swap(type_, other.type_);
0083       std::swap(prod_, other.prod_);
0084       swap(prov_, other.prov_);
0085       swap(whyFailed_, other.whyFailed_);
0086     }
0087 
0088     Handle<FWGenericObject>& operator=(Handle<FWGenericObject> const& rhs) {
0089       Handle<FWGenericObject> temp(rhs);
0090       this->swap(temp);
0091       return *this;
0092     }
0093 
0094     bool isValid() const { return prod_ && nullptr != prov_; }
0095 
0096     bool failedToGet() const { return nullptr != whyFailed_.get(); }
0097     edm::ObjectWithDict const* product() const {
0098       if (this->failedToGet()) {
0099         whyFailed_->raise();
0100       }
0101       return &prod_;
0102     }
0103     edm::ObjectWithDict const* operator->() const { return this->product(); }
0104     edm::ObjectWithDict const& operator*() const { return *(this->product()); }
0105 
0106     edm::TypeWithDict const& type() const { return type_; }
0107     Provenance const* provenance() const { return prov_; }
0108 
0109     ProductID id() const { return prov_->productID(); }
0110 
0111     void clear() {
0112       prov_ = nullptr;
0113       whyFailed_.reset();
0114     }
0115 
0116     void setWhyFailed(std::shared_ptr<cms::Exception> const& iWhyFailed) { whyFailed_ = iWhyFailed; }
0117 
0118   private:
0119     edm::TypeWithDict type_;
0120     edm::ObjectWithDict prod_;
0121     Provenance const* prov_;
0122     std::shared_ptr<cms::Exception> whyFailed_;
0123   };
0124 
0125   typedef Handle<FWGenericObject> FWGenericHandle;
0126 
0127   ///specialize this function forFWGenericHandle
0128   void convert_handle(BasicHandle const& orig, Handle<FWGenericObject>& result);
0129 
0130   ///Specialize the Event's getByLabel method to work with a Handle<FWGenericObject>
0131   template <>
0132   bool edm::EventBase::getByLabel(edm::InputTag const& tag, Handle<FWGenericObject>& result) const;
0133 
0134 }  // namespace edm
0135 #endif