CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/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 
00032 #include "Reflex/Object.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_(Reflex::Type::ByName(iName)), prod_(), prov_(0) {
00049           if(type_ == Reflex::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 'Reflex::Typedef' the 'toType' method returns the actual type
00057               // this is needed since you are now allowed to 'invoke' methods of a 'Typedef'
00058               // only for a 'real' class
00059               type_ = type_.ToType();
00060            }
00061         }
00062    
00064    Handle(Reflex::Type const& iType) :
00065       type_(iType), prod_(), prov_(0) {
00066          if(iType == Reflex::Type()) {
00067             Exception::throwThis(errors::NotFound, "Handle<GenericObject> given an invalid Reflex::Type");
00068          }
00069          if(type_.IsTypedef()){
00070             //For a 'Reflex::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(Reflex::Object const& prod, Provenance const* prov, ProductID const&):
00085    type_(prod.TypeOf()),
00086    prod_(prod),
00087    prov_(prov) { 
00088       assert(prod_);
00089       assert(prov_);
00090       assert(prov_->productID() != ProductID());
00091    }
00092    
00093       //~Handle();
00094       
00095    void swap(Handle<GenericObject>& other) {
00096       // use unqualified swap for user defined classes
00097       using std::swap;
00098       swap(type_, other.type_);
00099       std::swap(prod_, other.prod_);
00100       swap(prov_, other.prov_);
00101       swap(whyFailed_, other.whyFailed_);
00102    }
00103    
00104    
00105    Handle<GenericObject>& operator=(Handle<GenericObject> const& rhs) {
00106       Handle<GenericObject> temp(rhs);
00107       this->swap(temp);
00108       return *this;
00109    }
00110    
00111    bool isValid() const {
00112       return prod_ && 0!= prov_;
00113    }
00114 
00115    bool failedToGet() const {
00116      return 0 != whyFailed_.get();
00117    }
00118    Reflex::Object const* product() const { 
00119      if(this->failedToGet()) { 
00120        whyFailed_->raise();
00121      } 
00122      return &prod_;
00123    }
00124    Reflex::Object const* operator->() const {return this->product();}
00125    Reflex::Object const& operator*() const {return *(this->product());}
00126    
00127    Reflex::Type const& type() const {return type_;}
00128    Provenance const* provenance() const {return prov_;}
00129    
00130    ProductID id() const {return prov_->productID();}
00131 
00132    void clear() { prov_ = 0; whyFailed_.reset();}
00133       
00134    void setWhyFailed(boost::shared_ptr<cms::Exception> const& iWhyFailed) {
00135     whyFailed_=iWhyFailed;
00136   }
00137 private:
00138    Reflex::Type type_;
00139    Reflex::Object prod_;
00140    Provenance const* prov_;    
00141    boost::shared_ptr<cms::Exception> whyFailed_;
00142 
00143 };
00144 
00145 typedef Handle<GenericObject> GenericHandle;
00146 
00148 void convert_handle(BasicHandle const& orig,
00149                     Handle<GenericObject>& result);
00150 
00151 
00153 template<>
00154 bool
00155 edm::Event::getByLabel<GenericObject>(std::string const& label,
00156                                       std::string const& productInstanceName,
00157                                       Handle<GenericObject>& result) const;
00158 
00159 template <>
00160 bool
00161 edm::Event::getByLabel(edm::InputTag const& tag, Handle<GenericObject>& result) const;
00162 
00163 }
00164 #endif