File indexing completed on 2025-02-14 03:16:30
0001
0002 #ifndef FWCore_Framework_ESProducer_h
0003 #define FWCore_Framework_ESProducer_h
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072 #include <memory>
0073 #include <string>
0074 #include <optional>
0075 #include <vector>
0076
0077
0078 #include "FWCore/Framework/interface/ESConsumesCollector.h"
0079 #include "FWCore/Framework/interface/es_impl/MayConsumeChooserBase.h"
0080 #include "FWCore/Framework/interface/es_impl/ReturnArgumentTypes.h"
0081 #include "FWCore/Framework/interface/ESProductResolverFactoryProducer.h"
0082 #include "FWCore/Framework/interface/ESProductResolverArgumentFactoryTemplate.h"
0083
0084 #include "FWCore/Framework/interface/CallbackProductResolver.h"
0085 #include "FWCore/Framework/interface/Callback.h"
0086 #include "FWCore/Framework/interface/produce_helpers.h"
0087 #include "FWCore/Framework/interface/eventsetup_dependsOn.h"
0088 #include "FWCore/Framework/interface/es_Label.h"
0089
0090 #include "FWCore/Framework/interface/SharedResourcesAcquirer.h"
0091 #include "FWCore/ServiceRegistry/interface/ServiceRegistryfwd.h"
0092
0093
0094 namespace edm {
0095 namespace eventsetup {
0096 struct ComponentDescription;
0097 class ESRecordsToProductResolverIndices;
0098
0099
0100
0101
0102 template <typename T, typename TRecord, typename TDecorator>
0103 inline const TDecorator& createDecoratorFrom(T*, const TRecord*, const TDecorator& iDec) {
0104 return iDec;
0105 }
0106 }
0107
0108 class ESProducer : public ESProductResolverFactoryProducer {
0109 public:
0110 ESProducer();
0111 ~ESProducer() noexcept(false) override;
0112 ESProducer(const ESProducer&) = delete;
0113 ESProducer& operator=(const ESProducer&) = delete;
0114 ESProducer(ESProducer&&) = delete;
0115 ESProducer& operator=(ESProducer&&) = delete;
0116
0117 void updateLookup(eventsetup::ESRecordsToProductResolverIndices const&) final;
0118 ESResolverIndex const* getTokenIndices(unsigned int iIndex) const {
0119 if (itemsToGetFromRecords_.empty()) {
0120 return nullptr;
0121 }
0122 return (itemsToGetFromRecords_[iIndex].empty()) ? static_cast<ESResolverIndex const*>(nullptr)
0123 : &(itemsToGetFromRecords_[iIndex].front());
0124 }
0125 ESRecordIndex const* getTokenRecordIndices(unsigned int iIndex) const {
0126 if (recordsUsedDuringGet_.empty()) {
0127 return nullptr;
0128 }
0129 return (recordsUsedDuringGet_[iIndex].empty()) ? static_cast<ESRecordIndex const*>(nullptr)
0130 : &(recordsUsedDuringGet_[iIndex].front());
0131 }
0132 size_t numberOfTokenIndices(unsigned int iIndex) const {
0133 if (itemsToGetFromRecords_.empty()) {
0134 return 0;
0135 }
0136 return itemsToGetFromRecords_[iIndex].size();
0137 }
0138
0139 bool hasMayConsumes() const noexcept { return hasMayConsumes_; }
0140
0141 template <typename Record>
0142 std::optional<std::vector<ESResolverIndex>> updateFromMayConsumes(unsigned int iIndex,
0143 const Record& iRecord) const {
0144 if (not hasMayConsumes()) {
0145 return {};
0146 }
0147 std::vector<ESResolverIndex> ret = itemsToGetFromRecords_[iIndex];
0148 auto const info = consumesInfos_[iIndex].get();
0149 for (size_t i = 0; i < info->size(); ++i) {
0150 auto chooserBase = (*info)[i].chooser_.get();
0151 if (chooserBase) {
0152 auto chooser = static_cast<eventsetup::impl::MayConsumeChooserBase<Record>*>(chooserBase);
0153 ret[i] = chooser->makeChoice(iRecord);
0154 }
0155 }
0156 return ret;
0157 }
0158
0159 SerialTaskQueueChain& queue() { return acquirer_.serialQueueChain(); }
0160
0161 void esModulesWhoseProductsAreConsumed(std::vector<eventsetup::ComponentDescription const*>& esModules,
0162 eventsetup::ESRecordsToProductResolverIndices const&) const;
0163
0164 std::vector<std::vector<ESModuleConsumesInfo>> esModuleConsumesInfos(
0165 eventsetup::ESRecordsToProductResolverIndices const&) const;
0166
0167 protected:
0168
0169 void usesResources(std::vector<std::string> const&);
0170
0171
0172
0173
0174
0175 template <typename T>
0176 auto setWhatProduced(T* iThis, const es::Label& iLabel = {}) {
0177 return setWhatProduced(iThis, &T::produce, iLabel);
0178 }
0179
0180 template <typename T>
0181 auto setWhatProduced(T* iThis, const char* iLabel) {
0182 return setWhatProduced(iThis, es::Label(iLabel));
0183 }
0184
0185 template <typename T>
0186 auto setWhatProduced(T* iThis, const std::string& iLabel) {
0187 return setWhatProduced(iThis, es::Label(iLabel));
0188 }
0189
0190 template <typename T, typename TDecorator>
0191 auto setWhatProduced(T* iThis, const TDecorator& iDec, const es::Label& iLabel = {}) {
0192 return setWhatProduced(iThis, &T::produce, iDec, iLabel);
0193 }
0194
0195
0196
0197
0198
0199 template <typename T, typename TReturn, typename TRecord>
0200 auto setWhatProduced(T* iThis, TReturn (T::*iMethod)(const TRecord&), const es::Label& iLabel = {}) {
0201 return setWhatProduced(iThis, iMethod, eventsetup::CallbackSimpleDecorator<TRecord>(), iLabel);
0202 }
0203
0204
0205
0206
0207 template <typename T, typename TReturn, typename TRecord, typename TDecorator>
0208 auto setWhatProduced(T* iThis,
0209 TReturn (T ::*iMethod)(const TRecord&),
0210 const TDecorator& iDec,
0211 const es::Label& iLabel = {}) {
0212 return setWhatProduced<TReturn, TRecord>(
0213 [iThis, iMethod](TRecord const& iRecord) { return (iThis->*iMethod)(iRecord); },
0214 createDecoratorFrom(iThis, static_cast<const TRecord*>(nullptr), iDec),
0215 iLabel);
0216 }
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228 template <typename TFunc>
0229 auto setWhatProduced(TFunc&& func, const es::Label& iLabel = {}) {
0230 using Types = eventsetup::impl::ReturnArgumentTypes<TFunc>;
0231 using TReturn = typename Types::return_type;
0232 using TRecord = typename Types::argument_type;
0233 using DecoratorType = eventsetup::CallbackSimpleDecorator<TRecord>;
0234 return setWhatProduced<TReturn, TRecord>(std::forward<TFunc>(func), DecoratorType(), iLabel);
0235 }
0236
0237 template <typename TReturn, typename TRecord, typename TFunc, typename TDecorator>
0238 ESConsumesCollectorT<TRecord> setWhatProduced(TFunc&& func, TDecorator&& iDec, const es::Label& iLabel = {}) {
0239 const auto produceMethodID = consumesInfoSize();
0240 using DecoratorType = std::decay_t<TDecorator>;
0241 using CallbackType = eventsetup::Callback<ESProducer, TFunc, TReturn, TRecord, DecoratorType>;
0242 unsigned int iovIndex = 0;
0243 auto temp = std::make_shared<CallbackType>(
0244 this, std::forward<TFunc>(func), produceMethodID, std::forward<TDecorator>(iDec));
0245 auto callback =
0246 std::make_shared<std::pair<unsigned int, std::shared_ptr<CallbackType>>>(iovIndex, std::move(temp));
0247 registerProducts(std::move(callback),
0248 static_cast<const typename eventsetup::produce::product_traits<TReturn>::type*>(nullptr),
0249 static_cast<const TRecord*>(nullptr),
0250 iLabel);
0251 return ESConsumesCollectorT<TRecord>(consumesInfoPushBackNew(), produceMethodID);
0252 }
0253
0254
0255
0256
0257 unsigned int consumesInfoSize() const { return consumesInfos_.size(); }
0258
0259 ESConsumesInfo* consumesInfoPushBackNew() {
0260 consumesInfos_.push_back(std::make_unique<ESConsumesInfo>());
0261 return consumesInfos_.back().get();
0262 }
0263
0264 template <typename CallbackT, typename TList, typename TRecord>
0265 void registerProducts(std::shared_ptr<std::pair<unsigned int, std::shared_ptr<CallbackT>>> iCallback,
0266 const TList*,
0267 const TRecord* iRecord,
0268 const es::Label& iLabel) {
0269 registerProduct(iCallback, static_cast<const typename TList::tail_type*>(nullptr), iRecord, iLabel);
0270 registerProducts(std::move(iCallback), static_cast<const typename TList::head_type*>(nullptr), iRecord, iLabel);
0271 }
0272
0273 template <typename CallbackT, typename TRecord>
0274 void registerProducts(std::shared_ptr<std::pair<unsigned int, std::shared_ptr<CallbackT>>>,
0275 const eventsetup::produce::Null*,
0276 const TRecord*,
0277 const es::Label&) {
0278
0279 }
0280
0281 private:
0282 template <typename CallbackT, typename TProduct, typename TRecord>
0283 void registerProduct(std::shared_ptr<std::pair<unsigned int, std::shared_ptr<CallbackT>>> iCallback,
0284 const TProduct*,
0285 const TRecord*,
0286 const es::Label& iLabel) {
0287 using ResolverType = eventsetup::CallbackProductResolver<CallbackT, TRecord, TProduct>;
0288 using FactoryType = eventsetup::ESProductResolverArgumentFactoryTemplate<ResolverType, CallbackT>;
0289 registerFactory(std::make_unique<FactoryType>(std::move(iCallback)), iLabel.default_);
0290 }
0291
0292 template <typename CallbackT, typename TProduct, typename TRecord, int IIndex>
0293 void registerProduct(std::shared_ptr<std::pair<unsigned int, std::shared_ptr<CallbackT>>> iCallback,
0294 const es::L<TProduct, IIndex>*,
0295 const TRecord*,
0296 const es::Label& iLabel) {
0297 if (iLabel.labels_.size() <= IIndex || iLabel.labels_[IIndex] == es::Label::def()) {
0298 Exception::throwThis(errors::Configuration,
0299 "Unnamed Label\nthe index ",
0300 IIndex,
0301 " was never assigned a name in the 'setWhatProduced' method");
0302 }
0303 using ResolverType = eventsetup::CallbackProductResolver<CallbackT, TRecord, es::L<TProduct, IIndex>>;
0304 using FactoryType = eventsetup::ESProductResolverArgumentFactoryTemplate<ResolverType, CallbackT>;
0305 registerFactory(std::make_unique<FactoryType>(std::move(iCallback)), iLabel.labels_[IIndex]);
0306 }
0307
0308 std::vector<std::unique_ptr<ESConsumesInfo>> consumesInfos_;
0309 std::vector<std::vector<ESResolverIndex>> itemsToGetFromRecords_;
0310
0311
0312 std::vector<std::vector<ESRecordIndex>> recordsUsedDuringGet_;
0313
0314 SharedResourcesAcquirer acquirer_;
0315 std::unique_ptr<std::vector<std::string>> sharedResourceNames_;
0316 bool hasMayConsumes_ = false;
0317 };
0318 }
0319 #endif