Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-02-21 05:46:38

0001 #ifndef ServiceRegistry_Service_h
0002 #define ServiceRegistry_Service_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     ServiceRegistry
0006 // Class  :     Service
0007 //
0008 /**\class Service Service.h FWCore/ServiceRegistry/interface/Service.h
0009 
0010  Description: Smart pointer used to give easy access to Service's
0011 
0012  Usage:
0013     
0014 
0015 */
0016 //
0017 // Original Author:  Chris Jones
0018 //         Created:  Wed Sep  7 15:17:17 EDT 2005
0019 //
0020 
0021 // system include files
0022 #include <optional>
0023 
0024 // user include files
0025 #include "FWCore/ServiceRegistry/interface/ServiceRegistry.h"
0026 
0027 // forward declarations
0028 
0029 namespace edm {
0030   template <typename T>
0031   class Service {
0032   public:
0033     Service() {}
0034     //virtual ~Service();
0035 
0036     // ---------- const member functions ---------------------
0037     T* operator->() const { return &(ServiceRegistry::instance().template get<T>()); }
0038 
0039     T& operator*() const { return ServiceRegistry::instance().template get<T>(); }
0040 
0041     bool isAvailable() const { return ServiceRegistry::instance().template isAvailable<T>(); }
0042 
0043     operator bool() const { return isAvailable(); }
0044 
0045     ///iF should be a functor and will only be called if the Service is available
0046     template <typename F>
0047       requires(!requires(F&& iF, T* t) {
0048                 { iF(*t) } -> std::same_as<void>;
0049               })
0050     auto and_then(F&& iF, T* t) -> std::optional<decltype(iF(*t))> const {
0051       if (isAvailable()) {
0052         return iF(*(operator->()));
0053       }
0054       return std::nullopt;
0055     }
0056 
0057     template <typename F>
0058       requires(requires(F&& iF, T* t) {
0059         { iF(*t) } -> std::same_as<void>;
0060       })
0061     void and_then(F&& iF) const {
0062       if (isAvailable()) {
0063         iF(*(operator->()));
0064       }
0065     }
0066 
0067     // ---------- static member functions --------------------
0068 
0069     // ---------- member functions ---------------------------
0070 
0071   private:
0072   };
0073 
0074 }  // namespace edm
0075 
0076 #endif