LIEF: Library to Instrument Executable Formats
Version 2.0.0
Toggle main menu visibility
Loading...
Searching...
No Matches
lief-install
x86_64
static
include
LIEF
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
20
namespace
LIEF::details
{
21
template
<
class
T>
22
class
canbe_unique
{
23
public
:
24
canbe_unique
() =
default
;
25
canbe_unique
(
const
canbe_unique
&) =
delete
;
26
canbe_unique
&
operator=
(
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
64
T*
operator->
() {
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
94
~canbe_unique
() {
95
reset
();
96
}
97
98
private
:
99
T* ptr_ =
nullptr
;
100
bool
owned_ =
false
;
101
};
102
103
template
<
class
T>
104
inline
bool
operator==
(
const
canbe_unique<T>
& lhs, std::nullptr_t) {
105
return
!lhs;
106
}
107
108
template
<
class
T>
109
inline
bool
operator==
(std::nullptr_t,
const
canbe_unique<T>
& lhs) {
110
return
!lhs;
111
}
112
113
}
114
115
#endif
LIEF::details::canbe_unique
Definition
canbe_unique.hpp:22
LIEF::details::canbe_unique::~canbe_unique
~canbe_unique()
Definition
canbe_unique.hpp:94
LIEF::details::canbe_unique::operator=
canbe_unique & operator=(std::nullptr_t)
Definition
canbe_unique.hpp:28
LIEF::details::canbe_unique::operator->
T * operator->()
Definition
canbe_unique.hpp:64
LIEF::details::canbe_unique::reset
void reset()
Definition
canbe_unique.hpp:80
LIEF::details::canbe_unique::operator*
const T & operator*() const
Definition
canbe_unique.hpp:76
LIEF::details::canbe_unique::operator*
T & operator*()
Definition
canbe_unique.hpp:72
LIEF::details::canbe_unique::operator->
const T * operator->() const
Definition
canbe_unique.hpp:68
LIEF::details::canbe_unique::operator=
canbe_unique & operator=(canbe_unique &&other) noexcept
Definition
canbe_unique.hpp:38
LIEF::details::canbe_unique::get
const T * get() const
Definition
canbe_unique.hpp:60
LIEF::details::canbe_unique::canbe_unique
canbe_unique(const canbe_unique &)=delete
LIEF::details::canbe_unique::canbe_unique
canbe_unique(T &ptr)
Definition
canbe_unique.hpp:46
LIEF::details::canbe_unique::canbe_unique
canbe_unique(canbe_unique &&other) noexcept
Definition
canbe_unique.hpp:33
LIEF::details::canbe_unique::canbe_unique
canbe_unique(std::unique_ptr< T > unique_ptr)
Definition
canbe_unique.hpp:52
LIEF::details::canbe_unique::operator=
canbe_unique & operator=(const canbe_unique &)=delete
LIEF::details::canbe_unique::canbe_unique
canbe_unique(const T &ptr)
Definition
canbe_unique.hpp:49
LIEF::details::canbe_unique::get
T * get()
Definition
canbe_unique.hpp:56
LIEF::details::canbe_unique::canbe_unique
canbe_unique()=default
LIEF::details
Definition
Abstract/DebugInfo.hpp:27
LIEF::details::operator==
bool operator==(const canbe_unique< T > &lhs, std::nullptr_t)
Definition
canbe_unique.hpp:104
Generated by
1.18.0