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
canbe_unique
(
const
T& ptr) :
51
ptr_(const_cast<T*>(&ptr)),
52
owned_(false) {}
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
const
T*
get
()
const
{
63
return
ptr_;
64
}
65
66
T*
operator->
() {
67
return
ptr_;
68
}
69
70
const
T*
operator->
()
const
{
71
return
ptr_;
72
}
73
74
T&
operator*
() {
75
return
*ptr_;
76
}
77
78
const
T&
operator*
()
const
{
79
return
*ptr_;
80
}
81
82
void
reset
() {
83
if
(!owned_) {
84
return
;
85
}
86
if
(ptr_ !=
nullptr
) {
87
delete
ptr_;
88
}
89
ptr_ =
nullptr
;
90
}
91
92
operator
bool()
const
{
93
return
ptr_ !=
nullptr
;
94
}
95
96
~canbe_unique
() {
97
reset
();
98
}
99
100
private
:
101
T* ptr_ =
nullptr
;
102
bool
owned_ =
false
;
103
};
104
105
template
<
class
T>
106
inline
bool
operator==
(
const
canbe_unique<T>
& lhs, std::nullptr_t) {
107
return
!lhs;
108
}
109
110
template
<
class
T>
111
inline
bool
operator==
(std::nullptr_t,
const
canbe_unique<T>
& lhs) {
112
return
!lhs;
113
}
114
115
}
116
}
117
#endif
LIEF::details::canbe_unique
Definition
canbe_unique.hpp:22
LIEF::details::canbe_unique::~canbe_unique
~canbe_unique()
Definition
canbe_unique.hpp:96
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:66
LIEF::details::canbe_unique::reset
void reset()
Definition
canbe_unique.hpp:82
LIEF::details::canbe_unique::operator*
const T & operator*() const
Definition
canbe_unique.hpp:78
LIEF::details::canbe_unique::operator*
T & operator*()
Definition
canbe_unique.hpp:74
LIEF::details::canbe_unique::operator->
const T * operator->() const
Definition
canbe_unique.hpp:70
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:62
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:54
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:50
LIEF::details::canbe_unique::get
T * get()
Definition
canbe_unique.hpp:58
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:106
LIEF
LIEF namespace.
Definition
Abstract/Binary.hpp:40
Generated by
1.17.0