libfilezilla
Toggle main menu visibility
Loading...
Searching...
No Matches
lib
libfilezilla
optional.hpp
Go to the documentation of this file.
1
#ifndef LIBFILEZILLA_OPTIONAL_HEADER
2
#define LIBFILEZILLA_OPTIONAL_HEADER
3
4
8
9
namespace
fz
{
10
18
template
<
typename
T>
19
class
sparse_optional final
20
{
21
public
:
22
sparse_optional()
noexcept
=
default
;
23
explicit
sparse_optional(T
const
& v);
24
26
explicit
sparse_optional(T * v)
noexcept
;
27
28
sparse_optional(sparse_optional<T>
const
& v);
29
sparse_optional(sparse_optional<T> && v)
noexcept
;
30
~sparse_optional();
31
32
void
clear();
33
34
explicit
operator
bool()
const
{
return
v_ !=
nullptr
; };
35
36
T& operator*() {
return
*v_; }
37
T
const
& operator*()
const
{
return
*v_; }
38
39
T* operator->() {
return
v_; }
40
T
const
* operator->()
const
{
return
v_; }
41
42
bool
operator==(sparse_optional<T>
const
& cmp)
const
;
43
inline
bool
operator!=(sparse_optional<T>
const
& cmp)
const
{
return
!(*
this
== cmp); }
44
bool
operator<(sparse_optional<T>
const
& cmp)
const
;
45
46
sparse_optional<T>& operator=(sparse_optional<T>
const
& v);
47
sparse_optional<T>& operator=(sparse_optional<T> && v)
noexcept
;
48
private
:
49
T* v_{};
50
};
51
52
template
<
typename
T>
53
sparse_optional<T>::sparse_optional(T
const
& v)
54
: v_(new T(v))
55
{
56
}
57
58
template
<
typename
T>
59
sparse_optional<T>::sparse_optional(T * v) noexcept
60
: v_(v)
61
{
62
}
63
64
template
<
typename
T>
65
sparse_optional<T>::sparse_optional(
sparse_optional<T>
const
& v)
66
{
67
if
(v) {
68
v_ =
new
T(*v);
69
}
70
else
{
71
v_ =
nullptr
;
72
}
73
}
74
75
template
<
typename
T>
76
sparse_optional<T>::sparse_optional(
sparse_optional<T>
&& v)
noexcept
77
{
78
v_ = v.v_;
79
v.v_ =
nullptr
;
80
}
81
82
template
<
typename
T>
83
sparse_optional<T>::~sparse_optional()
84
{
85
delete
v_;
86
}
87
88
template
<
typename
T>
89
void
sparse_optional<T>::clear()
90
{
91
delete
v_;
92
v_ =
nullptr
;
93
}
94
95
template
<
typename
T>
96
sparse_optional<T>
& sparse_optional<T>::operator=(
sparse_optional<T>
const
& v)
97
{
98
if
(
this
!= &v) {
99
T*
value
{};
100
if
(v.v_) {
101
value
=
new
T(*v.v_);
102
}
103
delete
v_;
104
v_ =
value
;
105
}
106
107
return
*
this
;
108
}
109
110
template
<
typename
T>
111
sparse_optional<T>
& sparse_optional<T>::operator=(
sparse_optional<T>
&& v)
noexcept
112
{
113
if
(
this
!= &v) {
114
delete
v_;
115
v_ = v.v_;
116
v.v_ =
nullptr
;
117
}
118
119
return
*
this
;
120
}
121
122
template
<
typename
T>
123
bool
sparse_optional<T>::operator==(
sparse_optional<T>
const
& cmp)
const
124
{
125
if
(!v_ && !cmp.v_) {
126
return
true
;
127
}
128
129
if
(!v_ || !cmp.v_) {
130
return
false
;
131
}
132
133
return
*v_ == *cmp.v_;
134
}
135
136
template
<
typename
T>
137
bool
sparse_optional<T>::operator<(
sparse_optional<T>
const
& cmp)
const
138
{
139
if
(!v_ || !cmp.v_) {
140
return
cmp.v_ !=
nullptr
;
141
}
142
143
return
*v_ < *cmp.v_;
144
}
145
146
}
147
148
#endif
fz::sparse_optional
Similar to C++17's std::optional, but stores the data in dynamic memory.
Definition
optional.hpp:20
fz
The namespace used by libfilezilla.
Definition
apply.hpp:17
fz::xml::callback_event::value
@ value
Definition
xml.hpp:36
Generated by
1.17.0