LIEF: Library to Instrument Executable Formats Version 0.15.1
Loading...
Searching...
No Matches
canbe_unique.hpp
Go to the documentation of this file.
1/* Copyright 2024 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
19namespace LIEF {
20namespace 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 std::swap(ptr_, other.ptr_);
35 owned_ = other.owned_;
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 owned_(false)
49 {}
50
51 canbe_unique(const T& ptr) :
52 ptr_(const_cast<T*>(&ptr)),
53 owned_(false)
54 {}
55
56 canbe_unique(std::unique_ptr<T> unique_ptr) :
57 ptr_(unique_ptr.release()),
58 owned_(true)
59 {}
60
61 T* get() {
62 return ptr_;
63 }
64
65 const T* get() const {
66 return ptr_;
67 }
68
70 return ptr_;
71 }
72
73 const T* operator->() const {
74 return ptr_;
75 }
76
77 T& operator*() {
78 return *ptr_;
79 }
80
81 const T& operator*() const {
82 return *ptr_;
83 }
84
85 void reset() {
86 if (!owned_) {
87 return;
88 }
89 if (ptr_ != nullptr) {
90 delete ptr_;
91 }
92 ptr_ = nullptr;
93 }
94
95 operator bool() const {
96 return ptr_ != nullptr;
97 }
98
100 reset();
101 }
102
103 private:
104 T* ptr_ = nullptr;
105 bool owned_ = false;
106};
107
108template<class T>
109inline bool operator==(const canbe_unique<T>& lhs, std::nullptr_t) {
110 return !lhs;
111}
112
113template<class T>
114inline bool operator==(std::nullptr_t, const canbe_unique<T>& lhs) {
115 return !lhs;
116}
117
118}
119}
120#endif
Definition canbe_unique.hpp:22
~canbe_unique()
Definition canbe_unique.hpp:99
canbe_unique & operator=(std::nullptr_t)
Definition canbe_unique.hpp:28
T * operator->()
Definition canbe_unique.hpp:69
void reset()
Definition canbe_unique.hpp:85
const T & operator*() const
Definition canbe_unique.hpp:81
T & operator*()
Definition canbe_unique.hpp:77
const T * operator->() const
Definition canbe_unique.hpp:73
canbe_unique & operator=(canbe_unique &&other) noexcept
Definition canbe_unique.hpp:38
const T * get() const
Definition canbe_unique.hpp:65
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:56
canbe_unique & operator=(const canbe_unique &)=delete
canbe_unique(const T &ptr)
Definition canbe_unique.hpp:51
T * get()
Definition canbe_unique.hpp:61
bool operator==(const canbe_unique< T > &lhs, std::nullptr_t)
Definition canbe_unique.hpp:109
LIEF namespace.
Definition Abstract/Binary.hpp:32