CMS 3D CMS Logo

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 // $Id: GenericHandle.h,v 1.13 2008/05/12 18:14:07 wmtan Exp $
00028 //
00029 
00030 // system include files
00031 #include <string>
00032 
00033 // user include files
00034 #include "Reflex/Object.h"
00035 #include "FWCore/Framework/interface/Event.h"
00036 #include "DataFormats/Common/interface/Handle.h"
00037 
00038 // forward declarations
00039 namespace edm {
00041 struct GenericObject
00042 {
00043 };
00044 
00045 template<>
00046 class Handle<GenericObject> {
00047 public:
00049       Handle(const std::string& iName) : 
00050         type_(ROOT::Reflex::Type::ByName(iName)), prod_(), prov_(0), id_(0) {
00051            if(type_ == ROOT::Reflex::Type()) {
00052               throw edm::Exception(edm::errors::NotFound)<<"Handle<GenericObject> told to use uknown type '"<<iName<<"'.\n Please check spelling or that a module uses this type in the job.";
00053            }
00054            if(type_.IsTypedef()){
00055               //For a 'Reflex::Typedef' the 'toType' method returns the actual type
00056               // this is needed since you are now allowed to 'invoke' methods of a 'Typedef'
00057               // only for a 'real' class
00058               type_ = type_.ToType();
00059            }
00060         }
00061    
00063    Handle(const ROOT::Reflex::Type& iType):
00064       type_(iType), prod_(), prov_(0), id_(0) {
00065          if(iType == ROOT::Reflex::Type()) {
00066             throw edm::Exception(edm::errors::NotFound)<<"Handle<GenericObject> given an invalid ROOT::Reflex::Type";
00067          }
00068          if(type_.IsTypedef()){
00069             //For a 'Reflex::Typedef' the 'toType' method returns the actual type
00070             // this is needed since you are now allowed to 'invoke' methods of a 'Typedef'
00071             // only for a 'real' class
00072             type_ = type_.ToType();
00073          }
00074       }
00075    
00076    Handle(const Handle<GenericObject>& h):
00077    type_(h.type_),
00078    prod_(h.prod_),
00079    prov_(h.prov_),
00080    id_(h.id_),
00081    whyFailed_(h.whyFailed_)
00082    { }
00083    
00084    Handle(ROOT::Reflex::Object const& prod, Provenance const* prov):
00085    type_(prod.TypeOf()),
00086    prod_(prod),
00087    prov_(prov),
00088    id_(prov->productID()) { 
00089       assert(prod_);
00090       assert(prov_);
00091       assert(id_ != ProductID());
00092    }
00093    
00094       //~Handle();
00095       
00096    void swap(Handle<GenericObject>& other)
00097    {
00098       // use unqualified swap for user defined classes
00099       using std::swap;
00100       swap(type_, other.type_);
00101       std::swap(prod_, other.prod_);
00102       swap(prov_, other.prov_);
00103       swap(id_, other.id_);
00104       swap(whyFailed_, other.whyFailed_);
00105    }
00106    
00107    
00108    Handle<GenericObject>& operator=(const Handle<GenericObject>& rhs)
00109    {
00110       Handle<GenericObject> temp(rhs);
00111       this->swap(temp);
00112       return *this;
00113    }
00114    
00115    bool isValid() const {
00116       return prod_ && 0!= prov_;
00117    }
00118 
00119    bool failedToGet() const {
00120      return 0 != whyFailed_.get();
00121    }
00122    ROOT::Reflex::Object const* product() const { 
00123      if(this->failedToGet()) { 
00124        throw *whyFailed_;
00125      } 
00126      return &prod_;
00127    }
00128    ROOT::Reflex::Object const* operator->() const {return this->product();}
00129    ROOT::Reflex::Object const& operator*() const {return *(this->product());}
00130    
00131    ROOT::Reflex::Type const& type() const {return type_;}
00132    Provenance const* provenance() const {return prov_;}
00133    
00134    ProductID id() const {return id_;}
00135 
00136    void clear() { prov_ = 0; id_ = ProductID(); 
00137    whyFailed_.reset();}
00138       
00139    void setWhyFailed(const boost::shared_ptr<cms::Exception>& iWhyFailed) {
00140     whyFailed_=iWhyFailed;
00141   }
00142 private:
00143    ROOT::Reflex::Type type_;
00144    ROOT::Reflex::Object prod_;
00145    Provenance const* prov_;    
00146    ProductID id_;
00147    boost::shared_ptr<cms::Exception> whyFailed_;
00148 
00149 };
00150 
00151 typedef Handle<GenericObject> GenericHandle;
00152 
00154 void convert_handle(BasicHandle const& orig,
00155                     Handle<GenericObject>& result);
00156 
00157 
00159 template<>
00160 bool
00161 edm::Event::getByLabel<GenericObject>(std::string const& label,
00162                                       const std::string& productInstanceName,
00163                                       Handle<GenericObject>& result) const;
00164 
00165 template <>      
00166 bool     
00167 edm::Event::getByLabel(edm::InputTag const& tag, Handle<GenericObject>& result) const;   
00168 
00169 }
00170 #endif

Generated on Tue Jun 9 17:35:41 2009 for CMSSW by  doxygen 1.5.4