Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions FWCore/Framework/interface/WrapperBaseHandle.h
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
86 changes: 86 additions & 0 deletions FWCore/Framework/interface/WrapperBaseOrphanHandle.h
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
47 changes: 47 additions & 0 deletions FWCore/Framework/src/WrapperBaseHandle.cc
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
9 changes: 9 additions & 0 deletions FWCore/Integration/plugins/BuildFile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@
<use name="DataFormats/TestObjects"/>
</library>

<library file="WrapperBaseProducer.cc" name="FWCoreIntegrationWrapperBase">
<flags EDM_PLUGIN="1"/>
<use name="DataFormats/TestObjects"/>
<use name="FWCore/Framework"/>
<use name="FWCore/ParameterSet"/>
<use name="FWCore/Reflection"/>
<use name="FWCore/Utilities"/>
</library>

<library file="HistProducer.cc" name="TestHistProducer">
<flags EDM_PLUGIN="1"/>
<use name="FWCore/Framework"/>
Expand Down
Loading