Commit | Line | Data |
---|---|---|
a0a4f314 JG |
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 | explicit non_copyable_reference(ReferencedType& instance) noexcept : _value(&instance) | |
20 | { | |
21 | } | |
22 | ||
23 | non_copyable_reference(non_copyable_reference&& other) noexcept : _value(other._value) | |
24 | { | |
25 | other._value = nullptr; | |
26 | } | |
27 | ||
28 | non_copyable_reference() = delete; | |
29 | non_copyable_reference(const non_copyable_reference&) = delete; | |
30 | non_copyable_reference& operator=(non_copyable_reference&&) = delete; | |
31 | non_copyable_reference& operator=(const non_copyable_reference&) = delete; | |
32 | ||
33 | ReferencedType& get() const noexcept | |
34 | { | |
35 | return *_value; | |
36 | } | |
37 | ||
38 | ReferencedType *operator->() const noexcept | |
39 | { | |
40 | return _value; | |
41 | } | |
42 | ||
43 | ReferencedType& operator*() const noexcept | |
44 | { | |
45 | return *_value; | |
46 | } | |
47 | ||
48 | ~non_copyable_reference() | |
49 | { | |
50 | if (!_value) { | |
51 | return; | |
52 | } | |
53 | ||
54 | typename CustomDeleter::deleter del; | |
55 | del(_value); | |
56 | } | |
57 | ||
58 | private: | |
59 | ReferencedType *_value = nullptr; | |
60 | }; | |
61 | ||
62 | } /* namespace lttng */ | |
63 | ||
64 | #endif /* LTTNG_REFERENCE_H */ |