File indexing completed on 2025-02-21 05:46:38
0001 #ifndef ServiceRegistry_Service_h
0002 #define ServiceRegistry_Service_h
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 #include <optional>
0023
0024
0025 #include "FWCore/ServiceRegistry/interface/ServiceRegistry.h"
0026
0027
0028
0029 namespace edm {
0030 template <typename T>
0031 class Service {
0032 public:
0033 Service() {}
0034
0035
0036
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
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
0068
0069
0070
0071 private:
0072 };
0073
0074 }
0075
0076 #endif