Project
Loading...
Searching...
No Matches
ServiceRegistryHelpers.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11#ifndef O2_FRAMEWORK_SERVICEREGISTRYHELPERS_H_
12#define O2_FRAMEWORK_SERVICEREGISTRYHELPERS_H_
13
16
17#include <algorithm>
18#include <array>
19#include <functional>
20#include <string>
21#include <type_traits>
22#include <typeinfo>
23#include <thread>
24
25namespace o2::framework
26{
27
30 public:
31 // Create an handle for a service for the given interface T
32 // with actual implementation C, i.e. C is derived from T.
33 // Only one instance of type C can be registered per type T.
34 // The fact we use a bare pointer indicates that the ownership
35 // of the service still belongs to whatever created it, and is
36 // not passed to the registry. It's therefore responsibility of
37 // the creator of the service to properly dispose it.
38 template <class I, class C, enum ServiceKind K = ServiceKind::Serial>
39 static auto handleForService(C* service) -> ServiceHandle
40 {
41 // This only works for concrete implementations of the type T.
42 // We need type elision as we do not want to know all the services in
43 // advance
44 static_assert(std::is_const_v<I> == false,
45 "Service interface must not be const if service object is not const");
46 static_assert(std::is_base_of<I, C>::value == true,
47 "Registered service is not derived from declared interface");
48 constexpr auto typeHash = TypeIdHelpers::uniqueId<I>();
49 return ServiceHandle{typeHash, reinterpret_cast<void*>(service), K, typeid(C).name()};
50 }
51
53 template <class I, class C, enum ServiceKind K = ServiceKind::Serial>
54 static auto handleForService(C const* service) -> ServiceHandle
55 {
56 // This only works for concrete implementations of the type T.
57 // We need type elision as we do not want to know all the services in
58 // advance
59 static_assert(std::is_const_v<I> == true,
60 "Service interface must be const if service object is const");
61 static_assert(std::is_base_of<I, C>::value == true,
62 "Registered service is not derived from declared interface");
63 constexpr auto typeHash = TypeIdHelpers::uniqueId<I>();
64 return ServiceHandle{typeHash, reinterpret_cast<void*>(const_cast<C*>(service)), K, typeid(C).name()};
65 }
66};
67
68} // namespace o2::framework
69
70#endif // O2_FRAMEWORK_SERVICEREGISTRYHELPERS_H_
GLuint const GLchar * name
Definition glcorearb.h:781
Defining PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
std::string name
Mnemonic name to use for the service.
Helpers for ServiceRegistry manipulations.
static auto handleForService(C *service) -> ServiceHandle
static auto handleForService(C const *service) -> ServiceHandle
Same as above, but for const instances.