CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_1/src/FWCore/Framework/interface/GenericHandle.h

Go to the documentation of this file.
00001 #ifndef FWCore_Framework_GenericHandle_h
00002 #define FWCore_Framework_GenericHandle_h
00003 // -*- C++ -*-
00004 //
00005 // Package:     Framework
00006 // Class  :     GenericHandle
00007 // 
00024 //
00025 // Original Author:  Chris Jones
00026 //         Created:  Sat Jan  7 15:40:43 EST 2006
00027 //
00028 
00029 // user include files
00030 #include "FWCore/Framework/interface/Event.h"
00031 #include "FWCore/Utilities/interface/ObjectWithDict.h"
00032 #include "FWCore/Utilities/interface/TypeWithDict.h"
00033 
00034 // system include files
00035 #include <string>
00036 
00037 // forward declarations
00038 namespace edm {
00040 struct GenericObject {
00041 };
00042 
00043 template<>
00044 class Handle<GenericObject> {
00045 public:
00047     Handle(std::string const& iName) : 
00048        type_(TypeWithDict::byName(iName)), prod_(), prov_(0) {
00049           if(!bool(type_)) {
00050              Exception::throwThis(errors::NotFound,
00051              "Handle<GenericObject> told to use uknown type '",
00052              iName.c_str(),
00053              "'.\n Please check spelling or that a module uses this type in the job.");
00054            }
00055            if(type_.isTypedef()){
00056               //For a 'Typedef' the 'toType' method returns the actual type
00057               // this is needed since you are not allowed to 'invoke' methods of a 'Typedef'
00058               // only for a 'real' class
00059               type_ = type_.toType();
00060            }
00061         }
00062    
00064    Handle(TypeWithDict const& iType) :
00065       type_(iType), prod_(), prov_(0) {
00066          if(!bool(iType)) {
00067             Exception::throwThis(errors::NotFound, "Handle<GenericObject> given an invalid type");
00068          }
00069          if(type_.isTypedef()){
00070             //For a 'Typedef' the 'toType' method returns the actual type
00071             // this is needed since you are now allowed to 'invoke' methods of a 'Typedef'
00072             // only for a 'real' class
00073             type_ = type_.toType();
00074          }
00075       }
00076    
00077    Handle(Handle<GenericObject> const& h):
00078    type_(h.type_),
00079    prod_(h.prod_),
00080    prov_(h.prov_),
00081    whyFailed_(h.whyFailed_) {
00082    }
00083    
00084    Handle(ObjectWithDict const& prod, Provenance const* prov, ProductID const&):
00085    type_(prod.typeOf()),
00086    prod_(prod),
00087    prov_(prov) { 
00088       assert(prod_);
00089       assert(prov_);
00090    }
00091    
00092       //~Handle();
00093       
00094    void swap(Handle<GenericObject>& other) {
00095       // use unqualified swap for user defined classes
00096       using std::swap;
00097       swap(type_, other.type_);
00098       std::swap(prod_, other.prod_);
00099       swap(prov_, other.prov_);
00100       swap(whyFailed_, other.whyFailed_);
00101    }
00102    
00103    
00104    Handle<GenericObject>& operator=(Handle<GenericObject> const& rhs) {
00105       Handle<GenericObject> temp(rhs);
00106       this->swap(temp);
00107       return *this;
00108    }
00109    
00110    bool isValid() const {
00111       return prod_ && 0!= prov_;
00112    }
00113 
00114    bool failedToGet() const {
00115      return 0 != whyFailed_.get();
00116    }
00117    ObjectWithDict const* product() const { 
00118      if(this->failedToGet()) { 
00119        whyFailed_->raise();
00120      } 
00121      return &prod_;
00122    }
00123    ObjectWithDict const* operator->() const {return this->product();}
00124    ObjectWithDict const& operator*() const {return *(this->product());}
00125    
00126    TypeWithDict const& type() const {return type_;}
00127    Provenance const* provenance() const {return prov_;}
00128    
00129    ProductID id() const {return prov_->productID();}
00130 
00131    void clear() { prov_ = 0; whyFailed_.reset();}
00132       
00133    void setWhyFailed(boost::shared_ptr<cms::Exception> const& iWhyFailed) {
00134     whyFailed_=iWhyFailed;
00135   }
00136 private:
00137    TypeWithDict type_;
00138    ObjectWithDict prod_;
00139    Provenance const* prov_;    
00140    boost::shared_ptr<cms::Exception> whyFailed_;
00141 
00142 };
00143 
00144 typedef Handle<GenericObject> GenericHandle;
00145 
00147 void convert_handle(BasicHandle const& orig,
00148                     Handle<GenericObject>& result);
00149 
00150 
00152 template<>
00153 bool
00154 edm::Event::getByLabel<GenericObject>(std::string const& label,
00155                                       std::string const& productInstanceName,
00156                                       Handle<GenericObject>& result) const;
00157 
00158 template <>
00159 bool
00160 edm::Event::getByLabel(edm::InputTag const& tag, Handle<GenericObject>& result) const;
00161 
00162 }
00163 #endif