Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:13:10

0001 #ifndef Subsystem_Package_TestHandle_h
0002 #define Subsystem_Package_TestHandle_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     Subsystem/Package
0006 // Class  :     TestHandle
0007 //
0008 /**\class TestHandle TestHandle.h "TestHandle.h"
0009 
0010  Description: [one line class summary]
0011 
0012  Usage:
0013     <usage>
0014 
0015 */
0016 //
0017 // Original Author:  root
0018 //         Created:  Thu, 03 May 2018 15:50:05 GMT
0019 //
0020 
0021 // system include files
0022 #include <memory>
0023 
0024 // user include files
0025 #include "DataFormats/Common/interface/BasicHandle.h"
0026 #include "FWCore/Utilities/interface/Exception.h"
0027 
0028 // forward declarations
0029 
0030 namespace edm {
0031   namespace test {
0032     template <typename T>
0033     class TestHandle {
0034     public:
0035       explicit TestHandle(T const* product) : product_{product} {}
0036 
0037       explicit TestHandle(std::shared_ptr<HandleExceptionFactory const> iFailed)
0038           : product_{nullptr}, whyFailedFactory_{std::move(iFailed)} {}
0039 
0040       operator bool() { return product_ != nullptr; }
0041 
0042       bool isValid() const { return product_ != nullptr; }
0043 
0044       T const* product() const { return productStorage(); }
0045 
0046       T const* operator->() const { return product(); }
0047 
0048       T const& operator*() const { return *product(); }
0049 
0050     private:
0051       T const* productStorage() const {
0052         if (product_) {
0053           return product_;
0054         }
0055         throw *(whyFailedFactory_->make());
0056       }
0057 
0058       T const* product_;
0059       std::shared_ptr<HandleExceptionFactory const> whyFailedFactory_;
0060     };
0061 
0062   }  // namespace test
0063 }  // namespace edm
0064 
0065 #endif