CMS 3D CMS Logo

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

Go to the documentation of this file.
00001 #ifndef Framework_GenericHandle_h
00002 #define 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 // system include files
00030 #include <string>
00031 
00032 // user include files
00033 #include "Reflex/Object.h"
00034 #include "FWCore/Framework/interface/Event.h"
00035 
00036 // forward declarations
00037 namespace edm {
00039 struct GenericObject
00040 {
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& pid):
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    {
00097       // use unqualified swap for user defined classes
00098       using std::swap;
00099       swap(type_, other.type_);
00100       std::swap(prod_, other.prod_);
00101       swap(prov_, other.prov_);
00102       swap(whyFailed_, other.whyFailed_);
00103    }
00104    
00105    
00106    Handle<GenericObject>& operator=(Handle<GenericObject> const& rhs)
00107    {
00108       Handle<GenericObject> temp(rhs);
00109       this->swap(temp);
00110       return *this;
00111    }
00112    
00113    bool isValid() const {
00114       return prod_ && 0!= prov_;
00115    }
00116 
00117    bool failedToGet() const {
00118      return 0 != whyFailed_.get();
00119    }
00120    Reflex::Object const* product() const { 
00121      if(this->failedToGet()) { 
00122        whyFailed_->raise();
00123      } 
00124      return &prod_;
00125    }
00126    Reflex::Object const* operator->() const {return this->product();}
00127    Reflex::Object const& operator*() const {return *(this->product());}
00128    
00129    Reflex::Type const& type() const {return type_;}
00130    Provenance const* provenance() const {return prov_;}
00131    
00132    ProductID id() const {return prov_->productID();}
00133 
00134    void clear() { prov_ = 0; whyFailed_.reset();}
00135       
00136    void setWhyFailed(boost::shared_ptr<cms::Exception> const& iWhyFailed) {
00137     whyFailed_=iWhyFailed;
00138   }
00139 private:
00140    Reflex::Type type_;
00141    Reflex::Object prod_;
00142    Provenance const* prov_;    
00143    boost::shared_ptr<cms::Exception> whyFailed_;
00144 
00145 };
00146 
00147 typedef Handle<GenericObject> GenericHandle;
00148 
00150 void convert_handle(BasicHandle const& orig,
00151                     Handle<GenericObject>& result);
00152 
00153 
00155 template<>
00156 bool
00157 edm::Event::getByLabel<GenericObject>(std::string const& label,
00158                                       std::string const& productInstanceName,
00159                                       Handle<GenericObject>& result) const;
00160 
00161 template <>      
00162 bool     
00163 edm::Event::getByLabel(edm::InputTag const& tag, Handle<GenericObject>& result) const;   
00164 
00165 }
00166 #endif