2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-07-08 09:29:38 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Assertions.h>
|
2019-07-30 17:45:40 +00:00
|
|
|
#include <AK/Platform.h>
|
2020-02-06 10:55:06 +00:00
|
|
|
#include <AK/StdLibExtras.h>
|
2020-03-08 11:34:33 +00:00
|
|
|
#include <AK/Types.h>
|
2019-07-08 09:29:38 +00:00
|
|
|
|
2020-02-14 21:29:06 +00:00
|
|
|
namespace AK {
|
|
|
|
|
2019-07-08 09:29:38 +00:00
|
|
|
template<typename T>
|
2020-08-05 10:17:44 +00:00
|
|
|
class alignas(T) [[nodiscard]] Optional {
|
2019-07-08 09:29:38 +00:00
|
|
|
public:
|
|
|
|
Optional() {}
|
|
|
|
|
2019-08-08 16:34:59 +00:00
|
|
|
Optional(const T& value)
|
2019-07-08 09:29:38 +00:00
|
|
|
: m_has_value(true)
|
|
|
|
{
|
2019-08-08 16:34:59 +00:00
|
|
|
new (&m_storage) T(value);
|
2019-07-08 09:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
2019-08-08 16:34:59 +00:00
|
|
|
Optional(const U& value)
|
|
|
|
: m_has_value(true)
|
|
|
|
{
|
|
|
|
new (&m_storage) T(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional(T&& value)
|
2019-07-08 09:29:38 +00:00
|
|
|
: m_has_value(true)
|
|
|
|
{
|
|
|
|
new (&m_storage) T(move(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional(Optional&& other)
|
|
|
|
: m_has_value(other.m_has_value)
|
|
|
|
{
|
2019-08-07 05:17:52 +00:00
|
|
|
if (other.has_value()) {
|
2019-08-05 20:27:47 +00:00
|
|
|
new (&m_storage) T(other.release_value());
|
2019-07-08 09:29:38 +00:00
|
|
|
other.m_has_value = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional(const Optional& other)
|
|
|
|
: m_has_value(other.m_has_value)
|
|
|
|
{
|
|
|
|
if (m_has_value) {
|
2019-07-30 17:45:40 +00:00
|
|
|
new (&m_storage) T(other.value_without_consume_state());
|
2019-07-08 09:29:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional& operator=(const Optional& other)
|
|
|
|
{
|
|
|
|
if (this != &other) {
|
|
|
|
clear();
|
|
|
|
m_has_value = other.m_has_value;
|
|
|
|
if (m_has_value) {
|
|
|
|
new (&m_storage) T(other.value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional& operator=(Optional&& other)
|
|
|
|
{
|
|
|
|
if (this != &other) {
|
|
|
|
clear();
|
|
|
|
m_has_value = other.m_has_value;
|
2019-08-07 05:17:52 +00:00
|
|
|
if (other.has_value())
|
2019-07-08 09:29:38 +00:00
|
|
|
new (&m_storage) T(other.release_value());
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-04-30 09:43:25 +00:00
|
|
|
ALWAYS_INLINE ~Optional()
|
2019-07-08 09:29:38 +00:00
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2020-04-30 09:43:25 +00:00
|
|
|
ALWAYS_INLINE void clear()
|
2019-07-08 09:29:38 +00:00
|
|
|
{
|
|
|
|
if (m_has_value) {
|
|
|
|
value().~T();
|
|
|
|
m_has_value = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 09:43:25 +00:00
|
|
|
ALWAYS_INLINE bool has_value() const { return m_has_value; }
|
2019-07-08 09:29:38 +00:00
|
|
|
|
2020-04-30 09:43:25 +00:00
|
|
|
ALWAYS_INLINE T& value()
|
2019-07-08 09:29:38 +00:00
|
|
|
{
|
|
|
|
ASSERT(m_has_value);
|
|
|
|
return *reinterpret_cast<T*>(&m_storage);
|
|
|
|
}
|
|
|
|
|
2020-04-30 09:43:25 +00:00
|
|
|
ALWAYS_INLINE const T& value() const
|
2019-07-08 09:29:38 +00:00
|
|
|
{
|
2019-07-30 17:45:40 +00:00
|
|
|
return value_without_consume_state();
|
2019-07-08 09:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
T release_value()
|
|
|
|
{
|
|
|
|
ASSERT(m_has_value);
|
|
|
|
T released_value = move(value());
|
|
|
|
value().~T();
|
2019-08-05 19:47:36 +00:00
|
|
|
m_has_value = false;
|
2019-07-08 09:29:38 +00:00
|
|
|
return released_value;
|
|
|
|
}
|
|
|
|
|
2020-04-30 09:43:25 +00:00
|
|
|
ALWAYS_INLINE T value_or(const T& fallback) const
|
2019-07-24 05:26:02 +00:00
|
|
|
{
|
2019-07-30 17:45:40 +00:00
|
|
|
if (m_has_value)
|
2019-07-24 05:26:02 +00:00
|
|
|
return value();
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
|
2019-07-08 09:29:38 +00:00
|
|
|
private:
|
2019-07-30 17:45:40 +00:00
|
|
|
// Call when we don't want to alter the consume state
|
2020-04-30 09:43:25 +00:00
|
|
|
ALWAYS_INLINE const T& value_without_consume_state() const
|
2019-07-30 17:45:40 +00:00
|
|
|
{
|
|
|
|
ASSERT(m_has_value);
|
|
|
|
return *reinterpret_cast<const T*>(&m_storage);
|
|
|
|
}
|
2020-03-08 11:34:33 +00:00
|
|
|
u8 m_storage[sizeof(T)] { 0 };
|
2019-07-08 09:29:38 +00:00
|
|
|
bool m_has_value { false };
|
|
|
|
};
|
2020-02-14 21:29:06 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::Optional;
|