Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:13:02

0001 #ifndef FWCore_ServiceRegistry_ServiceMaker_h
0002 #define FWCore_ServiceRegistry_ServiceMaker_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     ServiceRegistry
0006 // Class  :     ServiceMaker
0007 //
0008 /**\class ServiceMaker ServiceMaker.h FWCore/ServiceRegistry/interface/ServiceMaker.h
0009 
0010  Description: Used to make an instance of a Service
0011 
0012  Usage:
0013     <usage>
0014 
0015 */
0016 //
0017 // Original Author:  Chris Jones
0018 //         Created:  Mon Sep  5 13:33:00 EDT 2005
0019 //
0020 
0021 // user include files
0022 #include "FWCore/ParameterSet/interface/ParameterSetDescriptionFillerPluginFactory.h"
0023 #include "FWCore/ServiceRegistry/interface/ServiceMakerBase.h"
0024 #include "FWCore/ServiceRegistry/interface/ServicePluginFactory.h"
0025 #include "FWCore/ServiceRegistry/interface/ServiceWrapper.h"
0026 #include "FWCore/ServiceRegistry/interface/ServicesManager.h"
0027 #include "FWCore/Utilities/interface/stringize.h"
0028 
0029 // system include files
0030 #include <memory>
0031 #include <typeinfo>
0032 
0033 // forward declarations
0034 
0035 namespace edm {
0036   class ActivityRegistry;
0037   class ParameterSet;
0038 
0039   namespace serviceregistry {
0040 
0041     template <typename T, typename TConcrete>
0042     struct MakerBase {
0043       typedef T interface_t;
0044       typedef TConcrete concrete_t;
0045     };
0046 
0047     template <typename T, typename TConcrete = T>
0048     struct AllArgsMaker : public MakerBase<T, TConcrete> {
0049       std::unique_ptr<T> make(ParameterSet const& iPS, ActivityRegistry& iAR) const {
0050         return std::make_unique<TConcrete>(iPS, iAR);
0051       }
0052     };
0053 
0054     template <typename T, typename TConcrete = T>
0055     struct ParameterSetMaker : public MakerBase<T, TConcrete> {
0056       std::unique_ptr<T> make(ParameterSet const& iPS, ActivityRegistry& /* iAR */) const {
0057         return std::make_unique<TConcrete>(iPS);
0058       }
0059     };
0060 
0061     template <typename T, typename TConcrete = T>
0062     struct NoArgsMaker : public MakerBase<T, TConcrete> {
0063       std::unique_ptr<T> make(ParameterSet const& /* iPS */, ActivityRegistry& /* iAR */) const {
0064         return std::make_unique<TConcrete>();
0065       }
0066     };
0067 
0068     template <typename T, typename TMaker = AllArgsMaker<T> >
0069     class ServiceMaker : public ServiceMakerBase {
0070     public:
0071       ServiceMaker() {}
0072 
0073       ServiceMaker(ServiceMaker const&) = delete;
0074       ServiceMaker const& operator=(ServiceMaker const&) = delete;
0075 
0076       // ---------- const member functions ---------------------
0077       std::type_info const& serviceType() const override { return typeid(T); }
0078 
0079       bool make(ParameterSet const& iPS, ActivityRegistry& iAR, ServicesManager& oSM) const override {
0080         TMaker maker;
0081         std::unique_ptr<T> pService(maker.make(iPS, iAR));
0082         auto ptr = std::make_shared<ServiceWrapper<T> >(std::move(pService));
0083         return oSM.put(ptr);
0084       }
0085 
0086       bool saveConfiguration() const override {
0087         return ServiceMakerBase::testSaveConfiguration(static_cast<typename TMaker::concrete_t const*>(nullptr));
0088       }
0089 
0090       bool processWideService() const override {
0091         return service::isProcessWideService(static_cast<typename TMaker::concrete_t const*>(nullptr));
0092       }
0093     };
0094   }  // namespace serviceregistry
0095 }  // namespace edm
0096 
0097 #define DEFINE_FWK_SERVICE(type)                                                                                  \
0098   DEFINE_EDM_PLUGIN(                                                                                              \
0099       edm::serviceregistry::ServicePluginFactory, edm::serviceregistry::ServiceMaker<type>, EDM_STRINGIZE(type)); \
0100   DEFINE_DESC_FILLER_FOR_SERVICES(type, type)
0101 
0102 #define DEFINE_FWK_SERVICE_MAKER(concrete, maker)                                          \
0103   typedef edm::serviceregistry::ServiceMaker<maker::interface_t, maker> concrete##__t;     \
0104   DEFINE_EDM_PLUGIN(edm::serviceregistry::ServicePluginFactory, concrete##__t, #concrete); \
0105   typedef maker::concrete_t concrete##___t;                                                \
0106   DEFINE_DESC_FILLER_FOR_SERVICES(concrete, concrete##___t)
0107 
0108 #endif