File indexing completed on 2023-03-17 11:02:02
0001 #ifndef FWCore_Framework_GenericHandle_h
0002 #define FWCore_Framework_GenericHandle_h
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030 #include "FWCore/Framework/interface/Event.h"
0031 #include "FWCore/Reflection/interface/ObjectWithDict.h"
0032 #include "FWCore/Reflection/interface/TypeWithDict.h"
0033
0034
0035 #include <string>
0036
0037
0038 namespace edm {
0039
0040 struct GenericObject {};
0041
0042 template <>
0043 class Handle<GenericObject> {
0044 public:
0045
0046 Handle(std::string const& iName) : type_(TypeWithDict::byName(iName)), prod_(), prov_(nullptr) {
0047 if (!bool(type_)) {
0048 Exception::throwThis(errors::NotFound,
0049 "Handle<GenericObject> told to use uknown type '",
0050 iName.c_str(),
0051 "'.\n Please check spelling or that a module uses this type in the job.");
0052 }
0053 }
0054
0055
0056 Handle(TypeWithDict const& iType) : type_(iType), prod_(), prov_(nullptr) {
0057 if (!bool(iType)) {
0058 Exception::throwThis(errors::NotFound, "Handle<GenericObject> given an invalid type");
0059 }
0060 }
0061
0062 Handle(Handle<GenericObject> const& h)
0063 : type_(h.type_), prod_(h.prod_), prov_(h.prov_), whyFailedFactory_(h.whyFailedFactory_) {}
0064
0065 Handle(ObjectWithDict const& prod, Provenance const* prov, ProductID const&)
0066 : type_(prod.typeOf()), prod_(prod), prov_(prov) {
0067 assert(prod_);
0068 assert(prov_);
0069 }
0070
0071
0072
0073 void swap(Handle<GenericObject>& other) {
0074
0075 using std::swap;
0076 swap(type_, other.type_);
0077 std::swap(prod_, other.prod_);
0078 swap(prov_, other.prov_);
0079 swap(whyFailedFactory_, other.whyFailedFactory_);
0080 }
0081
0082 Handle<GenericObject>& operator=(Handle<GenericObject> const& rhs) {
0083 Handle<GenericObject> temp(rhs);
0084 this->swap(temp);
0085 return *this;
0086 }
0087
0088 bool isValid() const { return prod_ && nullptr != prov_; }
0089
0090 bool failedToGet() const { return bool(whyFailedFactory_); }
0091 ObjectWithDict const* product() const {
0092 if (this->failedToGet()) {
0093 whyFailedFactory_->make()->raise();
0094 }
0095 return &prod_;
0096 }
0097 ObjectWithDict const* operator->() const { return this->product(); }
0098 ObjectWithDict const& operator*() const { return *(this->product()); }
0099
0100 TypeWithDict const& type() const { return type_; }
0101 Provenance const* provenance() const { return prov_; }
0102
0103 ProductID id() const { return prov_->productID(); }
0104
0105 void clear() {
0106 prov_ = nullptr;
0107 whyFailedFactory_ = nullptr;
0108 }
0109
0110 void setWhyFailedFactory(std::shared_ptr<HandleExceptionFactory const> const& iWhyFailed) {
0111 whyFailedFactory_ = iWhyFailed;
0112 }
0113
0114 private:
0115 TypeWithDict type_;
0116 ObjectWithDict prod_;
0117 Provenance const* prov_;
0118 std::shared_ptr<HandleExceptionFactory const> whyFailedFactory_;
0119 };
0120
0121 typedef Handle<GenericObject> GenericHandle;
0122
0123
0124 void convert_handle(BasicHandle&& orig, Handle<GenericObject>& result);
0125
0126
0127 template <>
0128 bool Event::getByLabel<GenericObject>(std::string const& label,
0129 std::string const& productInstanceName,
0130 Handle<GenericObject>& result) const;
0131
0132 template <>
0133 bool Event::getByLabel<GenericObject>(InputTag const& tag, Handle<GenericObject>& result) const;
0134
0135
0136 template <>
0137 bool Event::getByToken<GenericObject>(EDGetToken token, Handle<GenericObject>& result) const;
0138
0139 }
0140 #endif