Skip to content

Commit

Permalink
Simplify setting EDPutTokenT via call to produces
Browse files Browse the repository at this point in the history
Call to produces does not require specifying the data product type as it will be deduced via EDPutTokenT.
  • Loading branch information
Dr15Jones committed Aug 17, 2021
1 parent c7ba375 commit 07f14de
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 19 deletions.
10 changes: 10 additions & 0 deletions FWCore/Framework/interface/ProducerBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ EDProducts into an Event.
----------------------------------------------------------------------*/

#include "FWCore/Framework/interface/ProductRegistryHelper.h"
#include "FWCore/Framework/interface/ProducesCollector.h"
#include "FWCore/Utilities/interface/ProductResolverIndex.h"

#include <functional>
Expand Down Expand Up @@ -103,6 +104,15 @@ namespace edm {
ProducesCollector producesCollector();
using ProductRegistryHelper::produces;

template <Transition Tr = Transition::Event>
[[nodiscard]] auto produces(std::string instanceName) noexcept {
return producesCollector().produces<Tr>(std::move(instanceName));
}
template <Transition Tr = Transition::Event>
[[nodiscard]] auto produces() noexcept {
return producesCollector().produces<Tr>();
}

private:
friend class EDProducer;
friend class EDFilter;
Expand Down
33 changes: 33 additions & 0 deletions FWCore/Framework/interface/ProducesCollector.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ a functor passed to the Framework with a call to callWhenNewProductsRegistered.
namespace edm {

class TypeID;
template <Transition B>
class ProducesCollectorAdaptor;

class ProducesCollector {
public:
Expand Down Expand Up @@ -68,6 +70,15 @@ namespace edm {
return helper_->produces<ProductType, B>(std::move(instanceName));
}

template <Transition Tr = Transition::Event>
[[nodiscard]] auto produces(std::string instanceName) noexcept {
return ProducesCollectorAdaptor<Tr>(*this, std::move(instanceName));
}
template <Transition Tr = Transition::Event>
[[nodiscard]] auto produces() noexcept {
return ProducesCollectorAdaptor<Tr>(*this);
}

ProductRegistryHelper::BranchAliasSetter produces(const TypeID& id,
std::string instanceName = std::string(),
bool recordProvenance = true);
Expand All @@ -86,5 +97,27 @@ namespace edm {
propagate_const<ProductRegistryHelper*> helper_;
};

template <Transition B>
class ProducesCollectorAdaptor {
public:
using Adapter = ProducesCollectorAdaptor<B>;

template <typename TYPE>
EDPutTokenT<TYPE> produces() {
return m_producer.template produces<TYPE, B>(m_label);
}

private:
//only ConsumesCollector is allowed to make an instance of this class
friend class ProducesCollector;

ProducesCollectorAdaptor(ProducesCollector iBase, std::string iLabel)
: m_producer(iBase), m_label(std::move(iLabel)) {}
ProducesCollectorAdaptor(ProducesCollector iBase) : m_producer(iBase), m_label() {}

ProducesCollector m_producer;
std::string const m_label;
};

} // namespace edm
#endif
6 changes: 6 additions & 0 deletions FWCore/Framework/interface/ProductRegistryHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ namespace edm {
TypeLabelItem& value_;
EDPutTokenT<T> token_;

template <typename U>
EDPutTokenT<T> produces() {
static_assert(std::is_same_v<T, U>);
return token_;
}

operator EDPutTokenT<T>() { return token_; }
operator EDPutToken() { return EDPutToken(token_.index()); }
};
Expand Down
2 changes: 1 addition & 1 deletion FWCore/Framework/src/EndPathStatusInserter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <memory>

namespace edm {
EndPathStatusInserter::EndPathStatusInserter(unsigned int) : token_{produces<EndPathStatus>()} {}
EndPathStatusInserter::EndPathStatusInserter(unsigned int) : token_{produces()} {}

void EndPathStatusInserter::produce(StreamID, edm::Event& event, edm::EventSetup const&) const {
//Puts a default constructed EndPathStatus
Expand Down
2 changes: 1 addition & 1 deletion FWCore/Framework/src/PathStatusInserter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace edm {
PathStatusInserter::PathStatusInserter(unsigned int numberOfStreams)
: hltPathStatus_(numberOfStreams), token_{produces<HLTPathStatus>()} {}
: hltPathStatus_(numberOfStreams), token_{produces()} {}

void PathStatusInserter::setPathStatus(StreamID const& streamID, HLTPathStatus const& hltPathStatus) {
hltPathStatus_[streamID.value()] = hltPathStatus;
Expand Down
2 changes: 1 addition & 1 deletion FWCore/Framework/src/TriggerResultInserter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace edm {
TriggerResultInserter::TriggerResultInserter(const ParameterSet& pset, unsigned int iNStreams)
: resultsPerStream_(iNStreams), pset_id_(pset.id()), token_{produces<TriggerResults>()} {}
: resultsPerStream_(iNStreams), pset_id_(pset.id()), token_{produces()} {}

void TriggerResultInserter::setTrigResultForStream(unsigned int iStreamIndex, const TrigResPtr& trptr) {
resultsPerStream_[iStreamIndex] = trptr;
Expand Down
2 changes: 1 addition & 1 deletion FWCore/Framework/test/Event_t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ namespace {
template <typename T>
class TestProducer : public edm::ProducerBase {
public:
TestProducer(std::string const& productInstanceName) { token_ = produces<T>(productInstanceName); }
TestProducer(std::string const& productInstanceName) { token_ = produces(productInstanceName); }
EDPutTokenT<T> token_;
};
} // namespace
Expand Down
3 changes: 1 addition & 2 deletions FWCore/Framework/test/stubs/TestGlobalFilters.cc
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,7 @@ namespace edmtest {
class TestBeginProcessBlockFilter : public edm::global::EDFilter<edm::BeginProcessBlockProducer> {
public:
explicit TestBeginProcessBlockFilter(edm::ParameterSet const& p)
: trans_(p.getParameter<int>("transitions")),
token_(produces<unsigned int, edm::Transition::BeginProcessBlock>("begin")) {
: trans_(p.getParameter<int>("transitions")), token_(produces<edm::Transition::BeginProcessBlock>("begin")) {
produces<unsigned int>();

auto tag = p.getParameter<edm::InputTag>("consumesBeginProcessBlock");
Expand Down
2 changes: 1 addition & 1 deletion FWCore/Framework/test/stubs/TestGlobalProducers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ namespace edmtest {
public:
explicit TestAccumulator(edm::ParameterSet const& p)
: m_expectedCount(p.getParameter<unsigned int>("expectedCount")),
m_putToken(produces<unsigned int, edm::Transition::EndLuminosityBlock>()) {}
m_putToken(produces<edm::Transition::EndLuminosityBlock>()) {}

void accumulate(edm::StreamID iID, edm::Event const&, edm::EventSetup const&) const override { ++m_count; }

Expand Down
2 changes: 1 addition & 1 deletion FWCore/Modules/src/LogErrorHarvester.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace edm {
EDPutTokenT<std::vector<ErrorSummaryEntry>> token_;
};

LogErrorHarvester::LogErrorHarvester(ParameterSet const& iPSet) : token_{produces<std::vector<ErrorSummaryEntry>>()} {
LogErrorHarvester::LogErrorHarvester(ParameterSet const& iPSet) : token_{produces()} {
const edm::TypeID endPathStatusType{typeid(edm::EndPathStatus)};
const edm::TypeID pathStatusType{typeid(edm::PathStatus)};
const edm::TypeID triggerResultsType{typeid(edm::TriggerResults)};
Expand Down
41 changes: 30 additions & 11 deletions FWCore/Utilities/interface/EDPutToken.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,22 @@ namespace edm {
public:
using value_type = unsigned int;

EDPutToken() : m_value{s_uninitializedValue} {}
constexpr EDPutToken() noexcept : m_value{s_uninitializedValue} {}

template <typename T>
EDPutToken(EDPutTokenT<T> iOther) : m_value{iOther.m_value} {}
constexpr EDPutToken(EDPutTokenT<T> iOther) noexcept : m_value{iOther.m_value} {}

// ---------- const member functions ---------------------
value_type index() const { return m_value; }
bool isUninitialized() const { return m_value == s_uninitializedValue; }
constexpr value_type index() const noexcept { return m_value; }
constexpr bool isUninitialized() const noexcept { return m_value == s_uninitializedValue; }

private:
//for testing
friend class TestEDPutToken;

static const unsigned int s_uninitializedValue = 0xFFFFFFFF;
static constexpr unsigned int s_uninitializedValue = 0xFFFFFFFF;

explicit EDPutToken(unsigned int iValue) : m_value(iValue) {}
explicit constexpr EDPutToken(unsigned int iValue) noexcept : m_value(iValue) {}

// ---------- member data --------------------------------
value_type m_value;
Expand All @@ -71,19 +71,38 @@ namespace edm {
public:
using value_type = EDPutToken::value_type;

EDPutTokenT() : m_value{s_uninitializedValue} {}
constexpr EDPutTokenT() noexcept : m_value{s_uninitializedValue} {}

constexpr EDPutTokenT(const EDPutTokenT<T>&) noexcept = default;
constexpr EDPutTokenT(EDPutTokenT<T>&&) noexcept = default;
constexpr EDPutTokenT(EDPutTokenT<T>& iToken) noexcept : EDPutTokenT(const_cast<EDPutTokenT<T> const&>(iToken)) {}

template <typename ADAPTER>
constexpr explicit EDPutTokenT(ADAPTER&& iAdapter) noexcept : EDPutTokenT(iAdapter.template produces<T>()) {}

constexpr EDPutTokenT& operator=(const EDPutTokenT<T>&) noexcept = default;
constexpr EDPutTokenT& operator=(EDPutTokenT<T>&&) noexcept = default;

template <typename ADAPTER>
constexpr EDPutTokenT& operator=(ADAPTER&& iAdapter) noexcept {
EDPutTokenT<T> temp(iAdapter.template produces<T>());
m_value = temp.m_value;

return *this;
}

// ---------- const member functions ---------------------
value_type index() const { return m_value; }
bool isUninitialized() const { return m_value == s_uninitializedValue; }
constexpr value_type index() const noexcept { return m_value; }
constexpr bool isUninitialized() const noexcept { return m_value == s_uninitializedValue; }

private:
//for testing
friend class TestEDPutToken;

static const unsigned int s_uninitializedValue = 0xFFFFFFFF;
static constexpr unsigned int s_uninitializedValue = 0xFFFFFFFF;

explicit EDPutTokenT(unsigned int iValue) : m_value(iValue) {}
constexpr explicit EDPutTokenT(unsigned int iValue) noexcept : m_value(iValue) {}
constexpr explicit EDPutTokenT(unsigned long int iValue) noexcept : m_value(iValue) {}

// ---------- member data --------------------------------
value_type m_value;
Expand Down

0 comments on commit 07f14de

Please sign in to comment.