|
11 | 11 | #ifndef O2_FRAMEWORK_SERVICEREGISTRYREF_H_ |
12 | 12 | #define O2_FRAMEWORK_SERVICEREGISTRYREF_H_ |
13 | 13 |
|
14 | | -// Until we merge the proper ServiceRegistryRef... |
15 | 14 | #include "Framework/ServiceRegistry.h" |
16 | | -#endif // O2_FRAMEWORK_SERVICEREGISTRYREF_H_ |
| 15 | +#include "Framework/Logger.h" |
17 | 16 |
|
| 17 | +namespace o2::framework |
| 18 | +{ |
| 19 | + |
| 20 | +class ServiceRegistryRef |
| 21 | +{ |
| 22 | + public: |
| 23 | + // The streamId is used to identify the stream in case we have multiple |
| 24 | + // threads. We cannot merely used the thread id because that does not |
| 25 | + // work in case the thread is created ad-hoc, like it appears to happen |
| 26 | + // for both libuv and FairMQ. This behaviour, BTW, makes usage |
| 27 | + // of thread local storage basically impossible (i.e. you lose state). |
| 28 | + // We use the following convention: |
| 29 | + // - streamId == 0 means the main thread |
| 30 | + // - streamId > 0 means one of the libuv worker threads |
| 31 | + // - streamId == -1 means the region callback thread |
| 32 | + // - streamId < -1 means some other worker thread of FairMQ which |
| 33 | + // we do not know about. |
| 34 | + // |
| 35 | + // The getter will also make sure that a service of kind Stream |
| 36 | + // cannot be accessed if the streamId is <= 0 and complain accordingly. |
| 37 | + // The dataProcessorId will be used to distinguish between different |
| 38 | + // data processors when |
| 39 | + ServiceRegistryRef(ServiceRegistry& registry, short streamId = 0, short dataProcessorId = 0) |
| 40 | + : mRegistry(registry), |
| 41 | + mContext{streamId, dataProcessorId} |
| 42 | + { |
| 43 | + } |
| 44 | + |
| 45 | + /// Check if service of type T is currently active. |
| 46 | + template <typename T> |
| 47 | + std::enable_if_t<std::is_const_v<T> == false, bool> active() const |
| 48 | + { |
| 49 | + return mRegistry.active<T>(); |
| 50 | + } |
| 51 | + |
| 52 | + /// Get a service for the given interface T. The returned reference exposed to |
| 53 | + /// the user is actually of the last concrete type C registered, however this |
| 54 | + /// should not be a problem. |
| 55 | + template <typename T> |
| 56 | + T& get() const |
| 57 | + { |
| 58 | + return mRegistry.get<T>(); |
| 59 | + } |
| 60 | + |
| 61 | + /// Invoke before sending messages @a parts on a channel @a channelindex |
| 62 | + void preSendingMessagesCallbacks(fair::mq::Parts& parts, ChannelIndex channelindex) |
| 63 | + { |
| 64 | + mRegistry.preSendingMessagesCallbacks(mRegistry, parts, channelindex); |
| 65 | + } |
| 66 | + |
| 67 | + private: |
| 68 | + ServiceRegistry& mRegistry; |
| 69 | + ServiceRegistry::Context mContext; |
| 70 | +}; |
| 71 | + |
| 72 | +} // namespace o2::framework |
| 73 | + |
| 74 | +#endif // O2_FRAMEWORK_SERVICEREGISTRY_H_ |
0 commit comments