Toggle navigation
PlayRho
0.9.0
GitHub
Main Page
Modules
Classes
Files
Examples
File List
File Members
Common
OptionalValue.hpp
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2017 Louis Langholtz https://github.com/louis-langholtz/PlayRho
3
*
4
* This software is provided 'as-is', without any express or implied
5
* warranty. In no event will the authors be held liable for any damages
6
* arising from the use of this software.
7
*
8
* Permission is granted to anyone to use this software for any purpose,
9
* including commercial applications, and to alter it and redistribute it
10
* freely, subject to the following restrictions:
11
*
12
* 1. The origin of this software must not be misrepresented; you must not
13
* claim that you wrote the original software. If you use this software
14
* in a product, an acknowledgment in the product documentation would be
15
* appreciated but is not required.
16
* 2. Altered source versions must be plainly marked as such, and must not be
17
* misrepresented as being the original software.
18
* 3. This notice may not be removed or altered from any source distribution.
19
*/
20
21
#ifndef PLAYRHO_COMMON_OPTIONALVALUE_HPP
22
#define PLAYRHO_COMMON_OPTIONALVALUE_HPP
23
24
#include <
PlayRho/Defines.hpp
>
25
#include <cassert>
26
#include <utility>
27
28
namespace
playrho
{
29
34
template
<
typename
T>
35
class
OptionalValue
36
{
37
public
:
38
40
using
value_type
= T;
41
42
PLAYRHO_CONSTEXPR
inline
OptionalValue
() =
default
;
43
45
PLAYRHO_CONSTEXPR
inline
OptionalValue
(
const
OptionalValue
& other) =
default
;
46
48
PLAYRHO_CONSTEXPR
inline
OptionalValue
(
OptionalValue
&& other) noexcept:
49
m_value{std::move(other.m_value)}, m_set{other.m_set}
50
{
51
// Intentionally empty.
52
// Note that the exception specification of this constructor
53
// doesn't match the defaulted one (when built with boost units).
54
}
55
57
PLAYRHO_CONSTEXPR
inline
explicit
OptionalValue
(T v);
58
59
~OptionalValue
() =
default
;
60
62
PLAYRHO_CONSTEXPR
const
T&
operator*
()
const
;
63
65
PLAYRHO_CONSTEXPR
inline
T&
operator*
();
66
68
PLAYRHO_CONSTEXPR
const
T*
operator->
()
const
;
69
71
PLAYRHO_CONSTEXPR
inline
T*
operator->
();
72
74
PLAYRHO_CONSTEXPR
inline
explicit
operator
bool() const noexcept;
75
77
PLAYRHO_CONSTEXPR
inline
bool
has_value
() const noexcept;
78
80
OptionalValue
& operator= (const
OptionalValue
& other) = default;
81
83
OptionalValue
& operator= (
OptionalValue
&& other) noexcept
84
{
85
// Note that the exception specification of this method
86
// doesn't match the defaulted one (when built with boost units).
87
m_value = std::move(other.m_value);
88
m_set = other.m_set;
89
return
*
this
;
90
}
91
93
OptionalValue
&
operator=
(T v);
94
96
PLAYRHO_CONSTEXPR
inline
T&
value
();
97
99
PLAYRHO_CONSTEXPR
const
T&
value
()
const
;
100
102
PLAYRHO_CONSTEXPR
inline
T
value_or
(
const
T& alt)
const
;
103
105
void
reset
() noexcept
106
{
107
m_value =
value_type
{};
108
m_set =
false
;
109
}
110
111
private
:
112
value_type
m_value =
value_type
{};
113
bool
m_set =
false
;
114
};
115
116
template
<
typename
T>
117
PLAYRHO_CONSTEXPR
inline
OptionalValue<T>::OptionalValue
(T v): m_value{v}, m_set{
true
} {}
118
119
template
<
typename
T>
120
PLAYRHO_CONSTEXPR
inline
bool
OptionalValue<T>::has_value
() const noexcept
121
{
122
return
m_set;
123
}
124
125
template
<
typename
T>
126
PLAYRHO_CONSTEXPR
inline
OptionalValue<T>::operator
bool() const noexcept
127
{
128
return
m_set;
129
}
130
131
template
<
typename
T>
132
OptionalValue<T>
&
OptionalValue<T>::operator=
(T v)
133
{
134
m_value = v;
135
m_set =
true
;
136
return
*
this
;
137
}
138
139
template
<
typename
T>
140
PLAYRHO_CONSTEXPR
const
T*
OptionalValue<T>::operator->
()
const
141
{
142
assert(m_set);
143
return
&m_value;
144
}
145
146
template
<
typename
T>
147
PLAYRHO_CONSTEXPR
inline
T*
OptionalValue<T>::operator->
()
148
{
149
assert(m_set);
150
return
&m_value;
151
}
152
153
template
<
typename
T>
154
PLAYRHO_CONSTEXPR
const
T&
OptionalValue<T>::operator*
()
const
155
{
156
assert(m_set);
157
return
m_value;
158
}
159
160
template
<
typename
T>
161
PLAYRHO_CONSTEXPR
inline
T&
OptionalValue<T>::operator*
()
162
{
163
assert(m_set);
164
return
m_value;
165
}
166
167
template
<
typename
T>
168
PLAYRHO_CONSTEXPR
inline
T&
OptionalValue<T>::value
()
169
{
170
return
m_value;
171
}
172
173
template
<
typename
T>
174
PLAYRHO_CONSTEXPR
const
T&
OptionalValue<T>::value
()
const
175
{
176
return
m_value;
177
}
178
179
template
<
typename
T>
180
PLAYRHO_CONSTEXPR
inline
T
OptionalValue<T>::value_or
(
const
T& alt)
const
181
{
182
return
m_set? m_value: alt;
183
}
184
189
template
<
typename
T>
190
using
Optional
=
OptionalValue<T>
;
191
192
}
// namespace playrho
193
194
#endif // PLAYRHO_COMMON_OPTIONALVALUE_HPP