-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Specialise Handle and OrphanHandle for WrapperBase
#49449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| #ifndef FWCore_Framework_interface_WrapperBaseHandle_h | ||
| #define FWCore_Framework_interface_WrapperBaseHandle_h | ||
| /* | ||
| Description: Allows interaction with data in the Event without actually using the C++ class | ||
|
|
||
| Usage: | ||
| The Handle<WrapperBase> allows one to get data back from the edm::Event as an edm::Wrapper<T> | ||
| via a polymorphic pointer of type edm::WrapperBase, instead of as the actual C++ class type. | ||
|
|
||
| // make a handle to hold an instance of MyClass | ||
| edm::Handle<edm::WrapperBase> handle(typeid(MyClass)); | ||
| event.getByToken(token, handle); | ||
|
|
||
| // handle.product() returns a polymorphic pointer of type edm::WrapperBase to the underlying | ||
| // edm::Wrapper<MyClass> | ||
| assert(handle.product()->dynamicTypeInfo() == typeid(MyClass)); | ||
| edm::Wrapper<MyClass> const* wrapper = dynamic_cast<edm::Wrapper<MyClass> const*>(handle.product()); | ||
| */ | ||
|
|
||
| // c++ include files | ||
| #include <memory> | ||
| #include <string> | ||
|
|
||
| // CMSSW include files | ||
| #include "DataFormats/Common/interface/Handle.h" | ||
| #include "DataFormats/Common/interface/WrapperBase.h" | ||
| #include "DataFormats/Provenance/interface/ProductID.h" | ||
| #include "FWCore/Framework/interface/Event.h" | ||
| #include "FWCore/Utilities/interface/EDMException.h" | ||
| #include "FWCore/Utilities/interface/TypeID.h" | ||
|
|
||
| // forward declarations | ||
| namespace edm { | ||
|
|
||
| class Provenance; | ||
|
|
||
| template <> | ||
| class Handle<WrapperBase> { | ||
| public: | ||
| explicit Handle(std::type_info const& type) : type_(type), product_(nullptr), prov_(nullptr) {} | ||
|
|
||
| explicit Handle(TypeID type) : type_(type), product_(nullptr), prov_(nullptr) { | ||
| if (not type_) { | ||
| throw Exception(errors::NotFound, "Handle<WrapperBase> given an invalid type."); | ||
| } | ||
| } | ||
|
|
||
| Handle(WrapperBase const* product, Provenance const* prov) | ||
| : type_(product->dynamicTypeInfo()), product_(product), prov_(prov) { | ||
| assert(product_); | ||
| assert(prov_); | ||
| } | ||
|
|
||
| // Reimplement the interface of HandleBase | ||
|
|
||
| void clear() { | ||
| product_ = nullptr; | ||
| prov_ = nullptr; | ||
| whyFailedFactory_ = nullptr; | ||
| } | ||
|
|
||
| bool isValid() const { return nullptr != product_ and nullptr != prov_; } | ||
|
|
||
| bool failedToGet() const { return bool(whyFailedFactory_); } | ||
|
|
||
| Provenance const* provenance() const { return prov_; } | ||
|
|
||
| ProductID id() const { return prov_->productID(); } | ||
|
|
||
| std::shared_ptr<cms::Exception> whyFailed() const { | ||
| if (whyFailedFactory_.get()) { | ||
| return whyFailedFactory_->make(); | ||
| } | ||
| return std::shared_ptr<cms::Exception>(); | ||
| } | ||
|
|
||
| std::shared_ptr<HandleExceptionFactory const> const& whyFailedFactory() const { return whyFailedFactory_; } | ||
|
|
||
| explicit operator bool() const { return isValid(); } | ||
|
|
||
| bool operator!() const { return not isValid(); } | ||
|
|
||
| // Reimplement the interface of Handle<T> | ||
|
|
||
| WrapperBase const* product() const { | ||
| if (this->failedToGet()) { | ||
| whyFailedFactory_->make()->raise(); | ||
| } | ||
| return product_; | ||
| } | ||
|
|
||
| WrapperBase const* operator->() const { return this->product(); } | ||
| WrapperBase const& operator*() const { return *(this->product()); } | ||
|
|
||
| // Additional methods | ||
|
|
||
| TypeID const& type() const { return type_; } | ||
|
|
||
| void setWhyFailedFactory(std::shared_ptr<HandleExceptionFactory const> const& iWhyFailed) { | ||
| whyFailedFactory_ = iWhyFailed; | ||
| } | ||
|
|
||
| private: | ||
| TypeID type_; | ||
| WrapperBase const* product_; | ||
| Provenance const* prov_; | ||
| std::shared_ptr<HandleExceptionFactory const> whyFailedFactory_; | ||
| }; | ||
|
|
||
| // Specialize convert_handle for Handle<WrapperBase> | ||
| void convert_handle(BasicHandle&& orig, Handle<WrapperBase>& result); | ||
|
|
||
| // Specialize the Event's getByToken method to work with a Handle<WrapperBase> | ||
| template <> | ||
| bool Event::getByToken<WrapperBase>(EDGetToken token, Handle<WrapperBase>& result) const; | ||
|
|
||
| } // namespace edm | ||
|
|
||
| #endif // FWCore_Framework_interface_WrapperBaseHandle_h |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| #ifndef FWCore_Framework_interface_WrapperBaseOrphanHandle_h | ||
| #define FWCore_Framework_interface_WrapperBaseOrphanHandle_h | ||
|
|
||
| // c++ include files | ||
| #include <memory> | ||
| #include <typeinfo> | ||
|
|
||
| // CMSSW include files | ||
| #include "DataFormats/Common/interface/OrphanHandle.h" | ||
| #include "DataFormats/Common/interface/WrapperBase.h" | ||
| #include "DataFormats/Provenance/interface/ProductID.h" | ||
| #include "FWCore/Framework/interface/Event.h" | ||
| #include "FWCore/Utilities/interface/TypeID.h" | ||
|
|
||
| // forward declarations | ||
| namespace edm { | ||
|
|
||
| template <> | ||
| class OrphanHandle<WrapperBase> { | ||
| public: | ||
| OrphanHandle() : product_(nullptr), id_() {} | ||
|
|
||
| OrphanHandle(WrapperBase const* prod, ProductID const& id) : product_(prod), id_(id) { assert(product_); } | ||
|
|
||
| // Reimplement the interface of OrphanHandleBase | ||
|
|
||
| void clear() { product_ = nullptr; } | ||
|
|
||
| bool isValid() const { return nullptr != product_; } | ||
|
|
||
| ProductID id() const { return id_; } | ||
|
|
||
| // Reimplement the interface of OrphanHandle | ||
|
|
||
| WrapperBase const* product() const { return product_; } | ||
|
|
||
| WrapperBase const* operator->() const { return this->product(); } | ||
| WrapperBase const& operator*() const { return *(this->product()); } | ||
|
|
||
| private: | ||
| WrapperBase const* product_; | ||
| ProductID id_; | ||
| }; | ||
|
|
||
| // specialise Event::putImpl for WrapperBase | ||
| template <> | ||
| inline OrphanHandle<WrapperBase> Event::putImpl(EDPutToken::value_type index, std::unique_ptr<WrapperBase> product) { | ||
| // Event::putImpl<PROD> calls detail::do_post_insert_if_available(prod), | ||
| // but that requires access to the concrete type of PROD, which is not | ||
| // available here. Eventually the call to post_insert() should be moved to | ||
| // be part of the Wrapper constructor. | ||
| // For now the call to post_insert() is the responsibility of the caller. | ||
|
|
||
| assert(index < putProducts().size()); | ||
|
|
||
| // move the wrapped product into the event | ||
| putProducts()[index] = std::move(product); | ||
|
|
||
| // construct and return a handle to the product | ||
| WrapperBase const* prod = putProducts()[index].get(); | ||
| ProductID const& prodID = provRecorder_.getProductID(index); | ||
| return OrphanHandle<WrapperBase>(prod, prodID); | ||
| } | ||
|
|
||
| // specialise Event::put for WrapperBase | ||
| template <> | ||
| inline OrphanHandle<WrapperBase> Event::put(EDPutToken token, std::unique_ptr<WrapperBase> product) { | ||
| if (UNLIKELY(product.get() == nullptr)) { // null pointer is illegal | ||
| TypeID typeID(typeid(WrapperBase)); | ||
| principal_get_adapter_detail::throwOnPutOfNullProduct("Event", typeID, provRecorder_.productInstanceLabel(token)); | ||
| } | ||
| std::type_info const& type = product->dynamicTypeInfo(); | ||
| if (UNLIKELY(token.isUninitialized())) { | ||
| principal_get_adapter_detail::throwOnPutOfUninitializedToken("Event", type); | ||
| } | ||
| TypeID const& expected = provRecorder_.getTypeIDForPutTokenIndex(token.index()); | ||
| if (UNLIKELY(expected != TypeID{type})) { | ||
| principal_get_adapter_detail::throwOnPutOfWrongType(type, expected); | ||
| } | ||
|
|
||
| return putImpl(token.index(), std::move(product)); | ||
| } | ||
|
|
||
| } // namespace edm | ||
|
|
||
| #endif // FWCore_Framework_interface_WrapperBaseOrphanHandle_h | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // CMSSW include files | ||
| #include "DataFormats/Common/interface/Handle.h" | ||
| #include "DataFormats/Common/interface/WrapperBase.h" | ||
| #include "DataFormats/Provenance/interface/Provenance.h" | ||
| #include "FWCore/Framework/interface/Event.h" | ||
| #include "FWCore/Framework/interface/WrapperBaseHandle.h" | ||
| #include "FWCore/Utilities/interface/EDMException.h" | ||
| #include "FWCore/Utilities/interface/TypeDemangler.h" | ||
| #include "FWCore/Utilities/interface/TypeID.h" | ||
|
|
||
| namespace edm { | ||
|
|
||
| // Specialize the convert_handle free function for Handle<WrapperBase> | ||
| void convert_handle(BasicHandle&& handle, Handle<WrapperBase>& result) { | ||
| if (handle.failedToGet()) { | ||
| result.setWhyFailedFactory(handle.whyFailedFactory()); | ||
| return; | ||
| } | ||
|
|
||
| WrapperBase const* wrapper = handle.wrapper(); | ||
| if (wrapper == nullptr) { | ||
| throw Exception(errors::InvalidReference, "NullPointer") << "edm::BasicHandle has null pointer to Wrapper"; | ||
| } | ||
|
|
||
| if (TypeID(wrapper->dynamicTypeInfo()) != result.type()) { | ||
| throw Exception(errors::LogicError) << "WrapperBase asked for " << typeDemangle(result.type().name()) | ||
| << " but was given a " << typeDemangle(wrapper->dynamicTypeInfo().name()); | ||
| } | ||
|
|
||
| // Move the handle into result | ||
| result = Handle<WrapperBase>(wrapper, handle.provenance()); | ||
| } | ||
|
|
||
| // Specialize the Event::getByToken method for Handle<WrapperBase> | ||
| template <> | ||
| bool Event::getByToken<WrapperBase>(EDGetToken token, Handle<WrapperBase>& result) const { | ||
| result.clear(); | ||
| BasicHandle bh = provRecorder_.getByToken_(result.type(), PRODUCT_TYPE, token, moduleCallingContext_); | ||
| convert_handle(std::move(bh), result); // throws on conversion error | ||
| if (UNLIKELY(result.failedToGet())) { | ||
| return false; | ||
| } | ||
| addToGotBranchIDs(*result.provenance()); | ||
| return true; | ||
| } | ||
|
|
||
| } // namespace edm |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.