Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:27:46

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