Skip to content
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

Simplify setting EDPutTokenT via call to produces #34926

Merged
merged 3 commits into from
Aug 21, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be

Suggested change
//only ConsumesCollector is allowed to make an instance of this class
//only ProducesCollector 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
38 changes: 38 additions & 0 deletions FWCore/Framework/interface/ProductRegistryHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ namespace edm {
class ProductRegistry;
struct DoNotRecordParents;

template <Transition B>
class ProductRegistryHelperAdaptor;

class ProductRegistryHelper {
public:
virtual ~ProductRegistryHelper() noexcept(false);
Expand Down Expand Up @@ -79,6 +82,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 All @@ -104,6 +113,15 @@ namespace edm {
\endcode
should be added to the producer ctor for every product */

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

template <class ProductType>
BranchAliasSetterT<ProductType> produces() {
return produces<ProductType, InEvent>(std::string());
Expand Down Expand Up @@ -177,6 +195,26 @@ namespace edm {
std::vector<bool> recordProvenanceList_;
};

template <Transition B>
class ProductRegistryHelperAdaptor {
public:
template <typename TYPE>
EDPutTokenT<TYPE> produces() {
return m_helper.template produces<TYPE, B>(m_label);
}

private:
//only ConsumesCollector is allowed to make an instance of this class
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//only ConsumesCollector is allowed to make an instance of this class
//only ProducesCollector is allowed to make an instance of this class

friend class ProductRegistryHelper;

ProductRegistryHelperAdaptor(ProductRegistryHelper& iBase, std::string iLabel)
: m_helper(iBase), m_label(std::move(iLabel)) {}
explicit ProductRegistryHelperAdaptor(ProductRegistryHelper& iBase) : m_helper(iBase), m_label() {}

ProductRegistryHelper& m_helper;
std::string const m_label;
};

} // namespace edm

#endif
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
45 changes: 34 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,42 @@ 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;
constexpr EDPutTokenT& operator=(EDPutTokenT<T>& iOther) noexcept {
m_value = iOther.m_value;
return *this;
}

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