CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/DataFormats/Common/interface/ConvertHandle.h

Go to the documentation of this file.
00001 #ifndef DataFormats_Common_ConvertHandle_h
00002 #define DataFormats_Common_ConvertHandle_h
00003 
00004 #include "DataFormats/Common/interface/BasicHandle.h"
00005 #include "DataFormats/Common/interface/Handle.h"
00006 #include "DataFormats/Common/interface/Wrapper.h"
00007 
00008 #include <typeinfo>
00009 
00010 namespace edm {
00011 
00012   namespace handleimpl {
00013     void throwInvalidReference();
00014     void throwConvertTypeError(std::type_info const& expected, std::type_info const& actual);
00015   }
00016 
00017   // Convert from handle-to-void to handle-to-T
00018   template<typename T>
00019   void convert_handle(BasicHandle const& bh,
00020                       Handle<T>& result) {
00021     if(bh.failedToGet()) {
00022       Handle<T> h(bh.whyFailed());
00023       h.swap(result);
00024       return;
00025     }
00026     void const* basicWrapper = bh.wrapper();
00027     if(basicWrapper == 0) {
00028       handleimpl::throwInvalidReference();
00029     }
00030     if(!(bh.interface()->dynamicTypeInfo() == typeid(T))) {
00031       handleimpl::throwConvertTypeError(typeid(T), bh.interface()->dynamicTypeInfo());
00032     }
00033     Wrapper<T> const* wrapper = static_cast<Wrapper<T> const*>(basicWrapper);
00034 
00035     Handle<T> h(wrapper->product(), bh.provenance());
00036     h.swap(result);
00037   }
00038 }
00039 
00040 #endif