CMS 3D CMS Logo

HandleBase.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_HandleBase_h
2 #define DataFormats_Common_HandleBase_h
3 
4 /*----------------------------------------------------------------------
5 
6 Handle: Non-owning "smart pointer" for reference to products and
7 their provenances.
8 
9 This is a very preliminary version, and lacks safety features and
10 elegance.
11 
12 If the pointed-to product or provenance is destroyed, use of the
13 Handle becomes undefined. There is no way to query the Handle to
14 discover if this has happened.
15 
16 Handles can have:
17  -- Product and Provenance pointers both null;
18  -- Both pointers valid
19 
20 To check validity, one can use the isValid() function.
21 
22 If failedToGet() returns true then the requested data is not available
23 If failedToGet() returns false but isValid() is also false then no attempt
24  to get data has occurred
25 
26 ----------------------------------------------------------------------*/
27 
28 #include <cassert>
32 #include <algorithm>
33 
34 #include <memory>
35 
36 namespace cms {
37  class Exception;
38 }
39 namespace edm {
40  class HandleBase {
41  public:
42  HandleBase() : product_(nullptr), prov_(nullptr) {}
43 
44  HandleBase(void const* prod, Provenance const* prov) : product_(prod), prov_(prov) {
45  assert(prod);
46  assert(prov);
47  }
48 
50 
51  void clear() {
52  product_ = nullptr;
53  prov_ = nullptr;
54  whyFailedFactory_.reset();
55  }
56 
58  using std::swap;
59  swap(product_, other.product_);
60  std::swap(prov_, other.prov_);
61  swap(whyFailedFactory_, other.whyFailedFactory_);
62  }
63 
65  HandleBase temp(rhs);
66  this->swap(temp);
67  return *this;
68  }
69 
70  bool isValid() const { return product_ && prov_; }
71 
72  bool failedToGet() const { return bool(whyFailedFactory_); }
73 
74  Provenance const* provenance() const { return prov_; }
75 
76  ProductID id() const;
77 
78  HandleBase(HandleBase const&) = default;
79 
81  HandleBase(std::shared_ptr<HandleExceptionFactory const>&& iWhyFailed)
82  : product_(), prov_(nullptr), whyFailedFactory_(iWhyFailed) {}
83 
85  product_ = rhs.product_;
86  prov_ = rhs.prov_;
87  whyFailedFactory_ = std::move(rhs.whyFailedFactory_);
88  return *this;
89  }
90 
91  std::shared_ptr<cms::Exception> whyFailed() const {
92  if (whyFailedFactory_.get()) {
93  return whyFailedFactory_->make();
94  }
95  return std::shared_ptr<cms::Exception>();
96  }
97 
98  std::shared_ptr<HandleExceptionFactory const> const& whyFailedFactory() const { return whyFailedFactory_; }
99 
100  explicit operator bool() const { return isValid(); }
101 
102  bool operator!() const { return not isValid(); }
103 
104  protected:
105  void const* productStorage() const;
106 
107  private:
108  void const* product_;
110  std::shared_ptr<HandleExceptionFactory const> whyFailedFactory_;
111  };
112 
113  // Free swap function
114  inline void swap(HandleBase& a, HandleBase& b) { a.swap(b); }
115 } // namespace edm
116 
117 #endif
HandleExceptionFactory.h
ProvenanceFwd.h
edm::HandleBase::productStorage
void const * productStorage() const
Definition: HandleBase.cc:18
edm::HandleBase::HandleBase
HandleBase(std::shared_ptr< HandleExceptionFactory const > &&iWhyFailed)
Used when the attempt to get the data failed.
Definition: HandleBase.h:81
electrons_cff.bool
bool
Definition: electrons_cff.py:366
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::swap
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:117
edm::HandleBase
Definition: HandleBase.h:40
cms::cuda::assert
assert(be >=bs)
groupFilesInBlocks.temp
list temp
Definition: groupFilesInBlocks.py:142
std::swap
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
Definition: DataFrameContainer.h:209
dumpMFGeometry_cfg.prod
prod
Definition: dumpMFGeometry_cfg.py:24
ProductID.h
edm::HandleBase::provenance
Provenance const * provenance() const
Definition: HandleBase.h:74
trackingPlots.other
other
Definition: trackingPlots.py:1460
edm::HandleBase::whyFailed
std::shared_ptr< cms::Exception > whyFailed() const
Definition: HandleBase.h:91
edm::HandleBase::swap
void swap(HandleBase &other)
Definition: HandleBase.h:57
b
double b
Definition: hdecay.h:118
edm::HandleBase::whyFailedFactory
std::shared_ptr< HandleExceptionFactory const > const & whyFailedFactory() const
Definition: HandleBase.h:98
edm::HandleBase::clear
void clear()
Definition: HandleBase.h:51
edm::HandleBase::failedToGet
bool failedToGet() const
Definition: HandleBase.h:72
a
double a
Definition: hdecay.h:119
Exception
edm::HandleBase::operator=
HandleBase & operator=(HandleBase &&rhs)
Definition: HandleBase.h:84
edm::HandleBase::whyFailedFactory_
std::shared_ptr< HandleExceptionFactory const > whyFailedFactory_
Definition: HandleBase.h:110
edm::HandleBase::operator!
bool operator!() const
Definition: HandleBase.h:102
edm::HandleBase::prov_
Provenance const * prov_
Definition: HandleBase.h:109
edm::HandleBase::~HandleBase
~HandleBase()
Definition: HandleBase.h:49
eostools.move
def move(src, dest)
Definition: eostools.py:511
edm::HandleBase::HandleBase
HandleBase(void const *prod, Provenance const *prov)
Definition: HandleBase.h:44
edm::HandleBase::product_
void const * product_
Definition: HandleBase.h:108
edm::HandleBase::HandleBase
HandleBase()
Definition: HandleBase.h:42
edm::Provenance
Definition: Provenance.h:34
edm::HandleBase::isValid
bool isValid() const
Definition: HandleBase.h:70
edm::HandleBase::operator=
HandleBase & operator=(HandleBase const &rhs)
Definition: HandleBase.h:64
edm::HandleBase::id
ProductID id() const
Definition: HandleBase.cc:29
edm::ProductID
Definition: ProductID.h:27
cms
Namespace of DDCMS conversion namespace.
Definition: ProducerAnalyzer.cc:21