LIEF: Library to Instrument Executable Formats
Version 1.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
namespace
LIEF
{
20
namespace
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
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
69
T*
operator->
() {
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
99
~canbe_unique
() {
100
reset
();
101
}
102
103
private
:
104
T* ptr_ =
nullptr
;
105
bool
owned_ =
false
;
106
};
107
108
template
<
class
T>
109
inline
bool
operator==
(
const
canbe_unique<T>
& lhs, std::nullptr_t) {
110
return
!lhs;
111
}
112
113
template
<
class
T>
114
inline
bool
operator==
(std::nullptr_t,
const
canbe_unique<T>
& lhs) {
115
return
!lhs;
116
}
117
118
}
119
}
120
#endif
LIEF::details::canbe_unique
Definition
canbe_unique.hpp:22
LIEF::details::canbe_unique::~canbe_unique
~canbe_unique()
Definition
canbe_unique.hpp:99
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:69
LIEF::details::canbe_unique::reset
void reset()
Definition
canbe_unique.hpp:85
LIEF::details::canbe_unique::operator*
const T & operator*() const
Definition
canbe_unique.hpp:81
LIEF::details::canbe_unique::operator*
T & operator*()
Definition
canbe_unique.hpp:77
LIEF::details::canbe_unique::operator->
const T * operator->() const
Definition
canbe_unique.hpp:73
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:65
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:56
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:51
LIEF::details::canbe_unique::get
T * get()
Definition
canbe_unique.hpp:61
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:109
LIEF
LIEF namespace.
Definition
Abstract/Binary.hpp:40
Generated by
1.17.0