Go to the documentation of this file.
14 #include <type_traits>
19 struct NontrivialEmptyType final
21 constexpr NontrivialEmptyType() noexcept {}
33 _detail::NontrivialEmptyType m_disengaged;
44 constexpr
Nullable() : m_disengaged{}, m_hasValue(
false) {}
51 constexpr
Nullable(T initialValue) noexcept(std::is_nothrow_move_constructible<T>::value)
52 : m_value(std::move(initialValue)), m_hasValue(true)
62 : m_disengaged{}, m_hasValue(other.m_hasValue)
66 ::new (
static_cast<void*
>(&m_value)) T(other.m_value);
76 : m_disengaged{}, m_hasValue(other.m_hasValue)
80 ::new (
static_cast<void*
>(&m_value)) T(std::move(other.m_value));
117 void Swap(
Nullable& other) noexcept(std::is_nothrow_move_constructible<T>::value)
121 if (other.m_hasValue)
124 swap(m_value, other.m_value);
128 ::new (
static_cast<void*
>(&other.m_value)) T(std::move(m_value));
129 other.m_hasValue =
true;
133 else if (other.m_hasValue)
135 ::new (
static_cast<void*
>(&m_value)) T(std::move(other.m_value));
148 std::is_nothrow_move_constructible<T>::value)
180 typename std::enable_if<
183 typename std::remove_cv<typename std::remove_reference<U>::type>::type>::
186 std::is_scalar<U>::value
187 && std::is_same<T, typename std::decay<U>::type>::value)
191 && std::is_constructible<T, U>::value
192 && std::is_assignable<T&, U>::value,
196 std::is_nothrow_constructible<T, U>::value&& std::is_nothrow_assignable<T&, U>::value)
200 m_value = std::forward<U>(other);
204 ::new (
static_cast<void*
>(&m_value)) T(std::forward<U>(other));
216 template <
class... U>
217 T&
Emplace(U&&... Args) noexcept(std::is_nothrow_constructible<T, U...>::value)
220 ::new (
static_cast<void*
>(&m_value)) T(std::forward<U>(Args)...);
229 bool HasValue() const noexcept {
return m_hasValue; }
237 AZURE_ASSERT_MSG(m_hasValue,
"Empty Nullable, check HasValue() first.");
248 AZURE_ASSERT_MSG(m_hasValue,
"Empty Nullable, check HasValue() first.");
259 AZURE_ASSERT_MSG(m_hasValue,
"Empty Nullable, check HasValue() first.");
261 return std::move(m_value);
270 constexpr
explicit operator bool() const noexcept {
return HasValue(); }
280 constexpr
const T*
operator->()
const {
return std::addressof(m_value); }
290 constexpr T*
operator->() {
return std::addressof(m_value); }
300 constexpr
const T&
operator*() const& {
return m_value; }
320 constexpr T&&
operator*() && {
return std::move(m_value); }
330 constexpr
const T&&
operator*() const&& {
return std::move(m_value); }
339 typename std::enable_if<
340 std::is_convertible<const T&, typename std::remove_cv<T>::type>::value
341 && std::is_convertible<U, T>::value,
344 constexpr
typename std::remove_cv<T>::type
ValueOr(U&& other)
const&
351 return static_cast<typename std::remove_cv<T>::type
>(std::forward<U>(other));
361 typename std::enable_if<
362 std::is_convertible<T, typename std::remove_cv<T>::type>::value
363 && std::is_convertible<U, T>::value,
366 constexpr
typename std::remove_cv<T>::type
ValueOr(U&& other) &&
370 return std::move(m_value);
373 return static_cast<typename std::remove_cv<T>::type
>(std::forward<U>(other));
bool HasValue() const noexcept
Check whether a value is contained.
Definition: nullable.hpp:229
const T & Value() const &noexcept
Get the contained value.
Definition: nullable.hpp:235
void Reset() noexcept
Destructs the contained value, if there is one.
Definition: nullable.hpp:101
~Nullable()
Destructs the Nullable, calling the destructor for the contained value if there is one.
Definition: nullable.hpp:89
void Swap(Nullable &other) noexcept(std::is_nothrow_move_constructible< T >::value)
Exchanges the contents.
Definition: nullable.hpp:117
constexpr T & operator*() &
Accesses the contained value.
Definition: nullable.hpp:310
friend void swap(Nullable &lhs, Nullable &rhs) noexcept(std::is_nothrow_move_constructible< T >::value)
Invokes Azure::Nullable::Swap while having a lowercase name that satisfies swappable requirements (se...
Definition: nullable.hpp:147
constexpr T * operator->()
Accesses the contained value.
Definition: nullable.hpp:290
constexpr Nullable()
Constructs a Nullable that represents the absence of value.
Definition: nullable.hpp:44
constexpr const T * operator->() const
Accesses the contained value.
Definition: nullable.hpp:280
Manages an optional contained value, i.e. a value that may or may not be present.
Definition: nullable.hpp:30
T & Value() &noexcept
Get the contained value reference.
Definition: nullable.hpp:246
Nullable(const Nullable &other) noexcept(std::is_nothrow_copy_constructible< T >::value)
Constructs a Nullable by copying another Nullable.
Definition: nullable.hpp:61
Azure SDK abstractions.
Definition: azure_assert.hpp:55
Nullable & operator=(const Nullable &other)
Assignment operator.
Definition: nullable.hpp:154
constexpr const T && operator*() const &&
Accesses the contained value.
Definition: nullable.hpp:330
T & Emplace(U &&... Args) noexcept(std::is_nothrow_constructible< T, U... >::value)
Construct the contained value in-place.
Definition: nullable.hpp:217
constexpr const T & operator*() const &
Accesses the contained value.
Definition: nullable.hpp:300
Provide assert macros to use with pre-conditions.
Nullable & operator=(Nullable &&other) noexcept(std::is_nothrow_move_constructible< T >::value)
Assignment operator with move semantics.
Definition: nullable.hpp:163
T && Value() &&noexcept
Get the contained value (as rvalue reference).
Definition: nullable.hpp:257
constexpr std::remove_cv< T >::type ValueOr(U &&other) &&
Get the contained value, returns other if value is absent.
Definition: nullable.hpp:366
constexpr std::remove_cv< T >::type ValueOr(U &&other) const &
Get the contained value, returns other if value is absent.
Definition: nullable.hpp:344
constexpr T && operator*() &&
Accesses the contained value.
Definition: nullable.hpp:320
Nullable(Nullable &&other) noexcept(std::is_nothrow_move_constructible< T >::value)
Constructs a Nullable by moving in another Nullable.
Definition: nullable.hpp:75
constexpr Nullable(T initialValue) noexcept(std::is_nothrow_move_constructible< T >::value)
Constructs a Nullable having an initialValue.
Definition: nullable.hpp:51