LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
canbe_unique.hpp
Go to the documentation of this file.
1/* Copyright 2024 - 2026 R. Thomas
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#ifndef LIEF_CAN_BE_UNIQUE_H
16#define LIEF_CAN_BE_UNIQUE_H
17#include <cstddef>
18#include <memory>
19#include <utility>
20
21
22namespace LIEF::details {
23template<class T>
25 public:
26 canbe_unique() = default;
27 canbe_unique(const canbe_unique&) = delete;
29
30 canbe_unique& operator=(std::nullptr_t) {
31 reset();
32 return *this;
33 }
34
35 canbe_unique(canbe_unique&& other) noexcept :
36 ptr_(std::exchange(other.ptr_, nullptr)),
37 owned_(std::exchange(other.owned_, false)) {}
38
39 canbe_unique& operator=(canbe_unique&& other) noexcept {
40 canbe_unique tmp(std::move(other));
41 std::swap(ptr_, tmp.ptr_);
42 std::swap(owned_, tmp.owned_);
43 return *this;
44 }
45
46 canbe_unique(T& ptr) :
47 ptr_(std::addressof(ptr)) {}
48
49 canbe_unique(const T& ptr) :
50 ptr_(const_cast<T*>(std::addressof(ptr))) {}
51
52 canbe_unique(const T&&) = delete;
53
54 canbe_unique(std::unique_ptr<T> unique_ptr) :
55 ptr_(unique_ptr.release()),
56 owned_(true) {}
57
58 T* get() {
59 return ptr_;
60 }
61
62 bool is_owned() const {
63 return owned_;
64 }
65
66 std::unique_ptr<T> take_ownership() {
67 if (!owned_) {
68 return nullptr;
69 }
70
71 std::unique_ptr<T> result(ptr_);
72 ptr_ = nullptr;
73 owned_ = false;
74 return result;
75 }
76
77 const T* get() const {
78 return ptr_;
79 }
80
82 return ptr_;
83 }
84
85 const T* operator->() const {
86 return ptr_;
87 }
88
89 T& operator*() {
90 return *ptr_;
91 }
92
93 const T& operator*() const {
94 return *ptr_;
95 }
96
97 void reset() {
98 T* ptr = std::exchange(ptr_, nullptr);
99 if (std::exchange(owned_, false)) {
100 std::default_delete<T>()(ptr);
101 }
102 }
103
104 explicit operator bool() const {
105 return ptr_ != nullptr;
106 }
107
109 reset();
110 }
111
112 private:
113 T* ptr_ = nullptr;
114 bool owned_ = false;
115};
116
117template<class T>
118inline bool operator==(const canbe_unique<T>& lhs, std::nullptr_t) {
119 return !lhs;
120}
121
122template<class T>
123inline bool operator==(std::nullptr_t, const canbe_unique<T>& lhs) {
124 return !lhs;
125}
126
127template<class T>
128inline bool operator!=(const canbe_unique<T>& lhs, std::nullptr_t) {
129 return static_cast<bool>(lhs);
130}
131
132template<class T>
133inline bool operator!=(std::nullptr_t, const canbe_unique<T>& lhs) {
134 return static_cast<bool>(lhs);
135}
136
137}
138
139#endif
Definition canbe_unique.hpp:24
~canbe_unique()
Definition canbe_unique.hpp:108
canbe_unique(const T &&)=delete
canbe_unique & operator=(std::nullptr_t)
Definition canbe_unique.hpp:30
T * operator->()
Definition canbe_unique.hpp:81
void reset()
Definition canbe_unique.hpp:97
const T & operator*() const
Definition canbe_unique.hpp:93
T & operator*()
Definition canbe_unique.hpp:89
const T * operator->() const
Definition canbe_unique.hpp:85
canbe_unique & operator=(canbe_unique &&other) noexcept
Definition canbe_unique.hpp:39
bool is_owned() const
Definition canbe_unique.hpp:62
std::unique_ptr< T > take_ownership()
Definition canbe_unique.hpp:66
const T * get() const
Definition canbe_unique.hpp:77
canbe_unique(const canbe_unique &)=delete
canbe_unique(T &ptr)
Definition canbe_unique.hpp:46
canbe_unique(canbe_unique &&other) noexcept
Definition canbe_unique.hpp:35
canbe_unique(std::unique_ptr< T > unique_ptr)
Definition canbe_unique.hpp:54
canbe_unique & operator=(const canbe_unique &)=delete
canbe_unique(const T &ptr)
Definition canbe_unique.hpp:49
T * get()
Definition canbe_unique.hpp:58
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:79
Definition Abstract/DebugInfo.hpp:27
bool operator!=(const canbe_unique< T > &lhs, std::nullptr_t)
Definition canbe_unique.hpp:128
bool operator==(const canbe_unique< T > &lhs, std::nullptr_t)
Definition canbe_unique.hpp:118