sessiond: open_packets: use user_space_consumer_channel_keys util
[lttng-tools.git] / src / common / reference.hpp
1 /*
2 * Copyright (C) 2024 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #ifndef LTTNG_REFERENCE_H
9 #define LTTNG_REFERENCE_H
10
11 #include <algorithm>
12 #include <utility>
13
14 namespace lttng {
15
16 template <typename ReferencedType, typename CustomDeleter>
17 class non_copyable_reference {
18 public:
19 using referenced_type = ReferencedType;
20 using deleter = CustomDeleter;
21
22 non_copyable_reference(non_copyable_reference&& other) noexcept : _value(other._value)
23 {
24 _value = other._value;
25 other.release();
26 }
27
28 non_copyable_reference() = delete;
29 non_copyable_reference(const non_copyable_reference&) = delete;
30 non_copyable_reference& operator=(non_copyable_reference&& other) noexcept
31 {
32 if (this != &other) {
33 _clean_up();
34 _value = other._value;
35 other.release();
36 }
37
38 return *this;
39 }
40
41 non_copyable_reference& operator=(const non_copyable_reference&) = delete;
42
43 ReferencedType& get() const noexcept
44 {
45 return *_value;
46 }
47
48 ReferencedType *operator->() const noexcept
49 {
50 return _value;
51 }
52
53 ReferencedType& operator*() const noexcept
54 {
55 return *_value;
56 }
57
58 void release() noexcept
59 {
60 _value = nullptr;
61 }
62
63 ~non_copyable_reference()
64 {
65 _clean_up();
66 }
67
68 private:
69 explicit non_copyable_reference(ReferencedType& instance) noexcept : _value(&instance)
70 {
71 }
72
73 void _clean_up()
74 {
75 if (!_value) {
76 return;
77 }
78
79 const typename CustomDeleter::deleter del;
80 del(_value);
81 release();
82 }
83
84 template <typename FactoryReferencedType, typename FactoryCustomDeleter>
85 friend non_copyable_reference<FactoryReferencedType, FactoryCustomDeleter>
86 make_non_copyable_reference(FactoryReferencedType&);
87
88 ReferencedType *_value = nullptr;
89 };
90
91 template <typename ReferencedType, typename CustomDeleter>
92 non_copyable_reference<ReferencedType, CustomDeleter>
93 make_non_copyable_reference(ReferencedType& instance)
94 {
95 return non_copyable_reference<ReferencedType, CustomDeleter>(instance);
96 }
97
98 } /* namespace lttng */
99
100 #endif /* LTTNG_REFERENCE_H */
This page took 0.040926 seconds and 4 git commands to generate.