Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-06-11 04:36:58

0001 #ifndef DataFormats_Common_ValidHandle_h
0002 #define DataFormats_Common_ValidHandle_h
0003 
0004 /*----------------------------------------------------------------------
0005   
0006 Handle: Non-owning "smart pointer" for reference to products and
0007 their provenances.
0008 
0009 The data product is always guaranteed to be valid for this handle type.
0010 ----------------------------------------------------------------------*/
0011 
0012 #include <utility>
0013 #include "DataFormats/Provenance/interface/ProductID.h"
0014 
0015 namespace edm {
0016   namespace vhhelper {
0017     void throwIfNotValid(const void*) noexcept(false);
0018   }
0019   template <typename T>
0020   class ValidHandle {
0021   public:
0022     using element_type = T;
0023 
0024     ValidHandle() = delete;
0025     ValidHandle(T const* prod, ProductID id) noexcept(false) : product_(prod), id_(id) {
0026       vhhelper::throwIfNotValid(prod);
0027     }
0028 
0029     //NOTE: C++ disallows references to null
0030     ValidHandle(T const& prod, ProductID id) noexcept(true) : product_(&prod), id_(id) {}
0031     ValidHandle(const ValidHandle<T>&) = default;
0032     ValidHandle<T>& operator=(ValidHandle<T> const& rhs) = default;
0033     ~ValidHandle() = default;
0034 
0035     ProductID const& id() const noexcept(true) { return id_; }
0036 
0037     T const* product() const noexcept(true) { return product_; }
0038 
0039     T const* operator->() const noexcept(true) { return product(); }
0040     T const& operator*() const noexcept(true) { return *product(); }
0041 
0042   private:
0043     T const* product_;
0044     ProductID id_;
0045   };
0046 
0047   /** Take a handle (e.g. edm::Handle<T> or edm::OrphanHandle<T> and
0048    create a edm::ValidHandle<T>. If the argument is an invalid handle,
0049    an exception will be thrown.
0050    */
0051   template <typename U>
0052   auto makeValid(const U& iOtherHandleType) noexcept(false) {
0053     vhhelper::throwIfNotValid(iOtherHandleType.product());
0054     //because of the check, we know this is valid and do not have to check again
0055     return ValidHandle<typename U::element_type>(*iOtherHandleType.product(), iOtherHandleType.id());
0056   }
0057 }  // namespace edm
0058 
0059 #endif