Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-08-23 03:25:06

0001 #ifndef FWCore_Framework_ProducesCollector_h
0002 #define FWCore_Framework_ProducesCollector_h
0003 
0004 // Package:     FWCore/Framework
0005 // Class  :     edm::ProducesCollector
0006 //
0007 /**\class edm::ProducesCollector
0008 
0009  Description: Helper class to gather produces information for the ProducerBase class.
0010 
0011  Usage:
0012     The constructor of a module can get an instance of edm::ProducesCollector by calling its
0013 producesCollector() method. This instance can then be passed to helper classes in order to register
0014 the data the helper will put into the Event, LuminosityBlock or Run.
0015 
0016     Note this only supports use of the Transition enum. The produce template functions using
0017 the BranchType enum are not supported by this class. We still support the BranchType enum in
0018 ProducerBase for backward compatibility reasons, but even there they are deprecated and we
0019 are trying to migrate client code to use the template functions using the Transition enum.
0020 
0021      WARNING: The ProducesCollector should be used during the time that modules are being
0022 constructed. It should not be saved and used later. It will not work if it is used to call
0023 the produces function during beginJob, beginRun, beginLuminosity block, event processing or
0024 at any later time. It can be used while the module constructor is running or be contained in
0025 a functor passed to the Framework with a call to callWhenNewProductsRegistered.
0026 
0027 */
0028 //
0029 // Original Author:  W. David Dagenhart
0030 //         Created:  24 September 2019
0031 
0032 #include "FWCore/Framework/interface/ProductRegistryHelper.h"
0033 #include "FWCore/Utilities/interface/propagate_const.h"
0034 #include "FWCore/Utilities/interface/Transition.h"
0035 
0036 #include <string>
0037 #include <utility>
0038 
0039 namespace edm {
0040 
0041   class TypeID;
0042   template <Transition B>
0043   class ProducesCollectorAdaptor;
0044 
0045   class ProducesCollector {
0046   public:
0047     ProducesCollector() = delete;
0048     ProducesCollector(ProducesCollector const&);
0049     ProducesCollector(ProducesCollector&&) = default;
0050     ProducesCollector& operator=(ProducesCollector const&);
0051     ProducesCollector& operator=(ProducesCollector&&) = default;
0052 
0053     template <class ProductType>
0054     ProductRegistryHelper::BranchAliasSetterT<ProductType> produces() {
0055       return helper_->produces<ProductType>();
0056     }
0057 
0058     template <class ProductType>
0059     ProductRegistryHelper::BranchAliasSetterT<ProductType> produces(std::string instanceName) {
0060       return helper_->produces<ProductType>(std::move(instanceName));
0061     }
0062 
0063     template <typename ProductType, Transition B>
0064     ProductRegistryHelper::BranchAliasSetterT<ProductType> produces() {
0065       return helper_->produces<ProductType, B>();
0066     }
0067 
0068     template <typename ProductType, Transition B>
0069     ProductRegistryHelper::BranchAliasSetterT<ProductType> produces(std::string instanceName) {
0070       return helper_->produces<ProductType, B>(std::move(instanceName));
0071     }
0072 
0073     template <Transition Tr = Transition::Event>
0074     [[nodiscard]] auto produces(std::string instanceName) noexcept {
0075       return ProducesCollectorAdaptor<Tr>(*this, std::move(instanceName));
0076     }
0077     template <Transition Tr = Transition::Event>
0078     [[nodiscard]] auto produces() noexcept {
0079       return ProducesCollectorAdaptor<Tr>(*this);
0080     }
0081 
0082     ProductRegistryHelper::BranchAliasSetter produces(const TypeID& id,
0083                                                       std::string instanceName = std::string(),
0084                                                       bool recordProvenance = true);
0085 
0086     template <Transition B>
0087     ProductRegistryHelper::BranchAliasSetter produces(const TypeID& id,
0088                                                       std::string instanceName = std::string(),
0089                                                       bool recordProvenance = true) {
0090       return helper_->produces<B>(id, std::move(instanceName), recordProvenance);
0091     }
0092 
0093   private:
0094     friend class ProducerBase;
0095     ProducesCollector(ProductRegistryHelper* helper);
0096 
0097     propagate_const<ProductRegistryHelper*> helper_;
0098   };
0099 
0100   template <Transition B>
0101   class ProducesCollectorAdaptor {
0102   public:
0103     using Adapter = ProducesCollectorAdaptor<B>;
0104 
0105     template <typename TYPE>
0106     EDPutTokenT<TYPE> produces() {
0107       return m_producer.template produces<TYPE, B>(m_label);
0108     }
0109 
0110   private:
0111     //only ProducesCollector is allowed to make an instance of this class
0112     friend class ProducesCollector;
0113 
0114     ProducesCollectorAdaptor(ProducesCollector iBase, std::string iLabel)
0115         : m_producer(iBase), m_label(std::move(iLabel)) {}
0116     ProducesCollectorAdaptor(ProducesCollector iBase) : m_producer(iBase), m_label() {}
0117 
0118     ProducesCollector m_producer;
0119     std::string const m_label;
0120   };
0121 
0122 }  // namespace edm
0123 #endif