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 <memory>
18
19
20namespace LIEF::details {
21template<class T>
23 public:
24 canbe_unique() = default;
25 canbe_unique(const canbe_unique&) = delete;
27
28 canbe_unique& operator=(std::nullptr_t) {
29 reset();
30 return *this;
31 }
32
33 canbe_unique(canbe_unique&& other) noexcept :
34 owned_(other.owned_) {
35 std::swap(ptr_, other.ptr_);
36 }
37
38 canbe_unique& operator=(canbe_unique&& other) noexcept {
39 if (&other != this) {
40 std::swap(ptr_, other.ptr_);
41 owned_ = other.owned_;
42 }
43 return *this;
44 }
45
46 canbe_unique(T& ptr) :
47 ptr_(&ptr) {}
48
49 canbe_unique(const T& ptr) :
50 ptr_(const_cast<T*>(&ptr)) {}
51
52 canbe_unique(std::unique_ptr<T> unique_ptr) :
53 ptr_(unique_ptr.release()),
54 owned_(true) {}
55
56 T* get() {
57 return ptr_;
58 }
59
60 const T* get() const {
61 return ptr_;
62 }
63
65 return ptr_;
66 }
67
68 const T* operator->() const {
69 return ptr_;
70 }
71
72 T& operator*() {
73 return *ptr_;
74 }
75
76 const T& operator*() const {
77 return *ptr_;
78 }
79
80 void reset() {
81 if (!owned_) {
82 return;
83 }
84 if (ptr_ != nullptr) {
85 delete ptr_;
86 }
87 ptr_ = nullptr;
88 }
89
90 operator bool() const {
91 return ptr_ != nullptr;
92 }
93
95 reset();
96 }
97
98 private:
99 T* ptr_ = nullptr;
100 bool owned_ = false;
101};
102
103template<class T>
104inline bool operator==(const canbe_unique<T>& lhs, std::nullptr_t) {
105 return !lhs;
106}
107
108template<class T>
109inline bool operator==(std::nullptr_t, const canbe_unique<T>& lhs) {
110 return !lhs;
111}
112
113}
114
115#endif
Definition canbe_unique.hpp:22
~canbe_unique()
Definition canbe_unique.hpp:94
canbe_unique & operator=(std::nullptr_t)
Definition canbe_unique.hpp:28
T * operator->()
Definition canbe_unique.hpp:64
void reset()
Definition canbe_unique.hpp:80
const T & operator*() const
Definition canbe_unique.hpp:76
T & operator*()
Definition canbe_unique.hpp:72
const T * operator->() const
Definition canbe_unique.hpp:68
canbe_unique & operator=(canbe_unique &&other) noexcept
Definition canbe_unique.hpp:38
const T * get() const
Definition canbe_unique.hpp:60
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:33
canbe_unique(std::unique_ptr< T > unique_ptr)
Definition canbe_unique.hpp:52
canbe_unique & operator=(const canbe_unique &)=delete
canbe_unique(const T &ptr)
Definition canbe_unique.hpp:49
T * get()
Definition canbe_unique.hpp:56
Definition Abstract/DebugInfo.hpp:27
bool operator==(const canbe_unique< T > &lhs, std::nullptr_t)
Definition canbe_unique.hpp:104