Initial refactor

This commit is contained in:
John Wellbelove 2024-01-21 15:02:01 +00:00
parent 0f1840a70d
commit 664bdac4bd

View File

@ -103,15 +103,8 @@ namespace etl
/// See http://en.cppreference.com/w/cpp/utility/optional
///\tparam T The type to store.
///\ingroup utilities
//*****************************************************************************
template <typename T, bool is_pod = etl::is_pod<T>::value>
class optional;
//*****************************************************************************
/// For non POD types.
//*****************************************************************************
template <typename T>
class optional<T, false>
class optional
{
public:
@ -682,434 +675,6 @@ namespace etl
storage_type storage;
};
//*****************************************************************************
/// For arithmetic or pointer types.
///\tparam T The type to store.
///\ingroup utilities
//*****************************************************************************
template <typename T>
class optional<T, true>
{
public:
typedef T value_type;
//***************************************************************************
/// Constructor.
//***************************************************************************
ETL_CONSTEXPR14 optional()
: valid(false)
, storage()
{
}
//***************************************************************************
/// Constructor with nullopt.
//***************************************************************************
ETL_CONSTEXPR14 optional(etl::nullopt_t)
: valid(false)
, storage()
{
}
//***************************************************************************
/// Copy constructor.
//***************************************************************************
ETL_CONSTEXPR14 optional(const optional& other)
: valid(bool(other))
, storage(other.storage)
{
}
#if ETL_USING_CPP11
//***************************************************************************
/// Move constructor.
//***************************************************************************
ETL_CONSTEXPR14 optional(optional&& other)
: valid(bool(other))
, storage(etl::move(other.storage))
{
}
#endif
//***************************************************************************
/// Constructor from value type.
//***************************************************************************
ETL_CONSTEXPR14 optional(const T& value_)
: valid(true)
, storage(value_)
{
}
#if ETL_USING_CPP11
//***************************************************************************
/// Constructor from value type.
//***************************************************************************
ETL_CONSTEXPR14 optional(T&& value_)
: valid(true)
, storage(etl::move(value_))
{
}
#endif
//***************************************************************************
/// Assignment operator from nullopt.
//***************************************************************************
ETL_CONSTEXPR14 optional& operator =(etl::nullopt_t)
{
valid = false;
return *this;
}
//***************************************************************************
/// Assignment operator from optional.
//***************************************************************************
ETL_CONSTEXPR14 optional& operator =(const optional& other)
{
if (this != &other)
{
storage.value = other.storage.value;
valid = other.valid;
}
return *this;
}
#if ETL_USING_CPP11
//***************************************************************************
/// Assignment operator from optional.
//***************************************************************************
ETL_CONSTEXPR14 optional& operator =(optional&& other)
{
if (this != &other)
{
storage.value = etl::move(other.storage.value);
valid = other.valid;
}
return *this;
}
#endif
//***************************************************************************
/// Assignment operator from value type.
//***************************************************************************
ETL_CONSTEXPR14 optional& operator =(const T& value_)
{
storage.value = value_;
valid = true;
return *this;
}
#if ETL_USING_CPP11
//***************************************************************************
/// Assignment operator from value type.
//***************************************************************************
ETL_CONSTEXPR14 optional& operator =(T&& value_)
{
storage.value = etl::move(value_);
valid = true;
return *this;
}
#endif
//***************************************************************************
/// Pointer operator.
//***************************************************************************
ETL_CONSTEXPR14 T* operator ->()
{
#if ETL_IS_DEBUG_BUILD
ETL_ASSERT(valid, ETL_ERROR(optional_invalid));
#endif
return &storage.value;
}
//***************************************************************************
/// Pointer operator.
//***************************************************************************
ETL_CONSTEXPR14 const T* operator ->() const
{
#if ETL_IS_DEBUG_BUILD
ETL_ASSERT(valid, ETL_ERROR(optional_invalid));
#endif
return &storage.value;
}
//***************************************************************************
/// Dereference operator.
//***************************************************************************
ETL_CONSTEXPR14 T& operator *() ETL_LVALUE_REF_QUALIFIER
{
#if ETL_IS_DEBUG_BUILD
ETL_ASSERT(valid, ETL_ERROR(optional_invalid));
#endif
return storage.value;
}
//***************************************************************************
/// Dereference operator.
//***************************************************************************
ETL_CONSTEXPR14 const T& operator *() const ETL_LVALUE_REF_QUALIFIER
{
#if ETL_IS_DEBUG_BUILD
ETL_ASSERT(valid, ETL_ERROR(optional_invalid));
#endif
return storage.value;
}
#if ETL_USING_CPP11
//***************************************************************************
/// Dereference operator.
//***************************************************************************
ETL_CONSTEXPR14 T&& operator *()&&
{
#if ETL_IS_DEBUG_BUILD
ETL_ASSERT(valid, ETL_ERROR(optional_invalid));
#endif
return etl::move(storage.value);
}
//***************************************************************************
/// Dereference operator.
//***************************************************************************
ETL_CONSTEXPR14 const T&& operator *() const&&
{
#if ETL_IS_DEBUG_BUILD
ETL_ASSERT(valid, ETL_ERROR(optional_invalid));
#endif
return etl::move(storage.value);
}
#endif
//***************************************************************************
/// Bool conversion operator.
//***************************************************************************
ETL_CONSTEXPR14
ETL_EXPLICIT operator bool() const
{
return valid;
}
//***************************************************************************
// Check whether optional contains value
//***************************************************************************
ETL_CONSTEXPR14 bool has_value() const ETL_NOEXCEPT
{
return valid;
}
//***************************************************************************
/// Get a reference to the value.
//***************************************************************************
ETL_CONSTEXPR14 T& value() ETL_LVALUE_REF_QUALIFIER
{
#if ETL_IS_DEBUG_BUILD
ETL_ASSERT(valid, ETL_ERROR(optional_invalid));
#endif
return storage.value;
}
//***************************************************************************
/// Get a const reference to the value.
//***************************************************************************
ETL_CONSTEXPR14 const T& value() const ETL_LVALUE_REF_QUALIFIER
{
#if ETL_IS_DEBUG_BUILD
ETL_ASSERT(valid, ETL_ERROR(optional_invalid));
#endif
return storage.value;
}
//***************************************************************************
/// Gets the value or a default if no valid.
//***************************************************************************
ETL_CONSTEXPR14 T value_or(const T& default_value) const ETL_LVALUE_REF_QUALIFIER
{
return valid ? value() : default_value;
}
#if ETL_USING_CPP11
//***************************************************************************
/// Get an rvalue reference to the value.
//***************************************************************************
ETL_CONSTEXPR14 T&& value()&&
{
#if ETL_IS_DEBUG_BUILD
ETL_ASSERT(valid, ETL_ERROR(optional_invalid));
#endif
return etl::move(storage.value);
}
//***************************************************************************
/// Get a const rvalue reference to the value.
//***************************************************************************
ETL_CONSTEXPR14 const T&& value() const&&
{
#if ETL_IS_DEBUG_BUILD
ETL_ASSERT(valid, ETL_ERROR(optional_invalid));
#endif
return etl::move(storage.value);
}
//***************************************************************************
/// Gets the value or a default if not valid.
//***************************************************************************
template <typename U>
ETL_CONSTEXPR20_STL
etl::enable_if_t<etl::is_convertible<U, T>::value, T>
value_or(U&& default_value) const&
{
if (has_value())
{
return value();
}
else
{
return static_cast<T>(etl::forward<U>(default_value));
}
}
//***************************************************************************
/// Gets the value or a default if not valid.
//***************************************************************************
template <typename U>
ETL_CONSTEXPR20_STL
etl::enable_if_t<etl::is_convertible<U, T>::value, T>
value_or(U&& default_value) &&
{
if (has_value())
{
return etl::move(value());
}
else
{
return static_cast<T>(etl::forward<U>(default_value));
}
}
#endif
//***************************************************************************
/// Swaps this value with another.
//***************************************************************************
ETL_CONSTEXPR14 void swap(optional& other)
{
optional temp(*this);
*this = other;
other = temp;
}
//***************************************************************************
/// Reset back to invalid.
//***************************************************************************
ETL_CONSTEXPR14 void reset()
{
valid = false;
}
#if ETL_USING_CPP11 && ETL_NOT_USING_STLPORT && !defined(ETL_OPTIONAL_FORCE_CPP03_IMPLEMENTATION)
//*************************************************************************
/// Emplaces a value.
///\param args The arguments to construct with.
//*************************************************************************
template <typename ... Args>
ETL_CONSTEXPR14 void emplace(Args && ... args)
{
storage.value = T(ETL_OR_STD::forward<Args>(args)...);
valid = true;
}
#else
//*************************************************************************
/// Emplaces a value.
/// 0 parameters.
//*************************************************************************
void emplace()
{
storage.value = value_type();
valid = true;
}
//*************************************************************************
/// Emplaces a value.
/// 1 parameter.
//*************************************************************************
template <typename T1>
void emplace(const T1& value1)
{
storage.value = value1;
valid = true;
}
//*************************************************************************
/// Emplaces a value.
/// 2 parameters.
//*************************************************************************
template <typename T1, typename T2>
void emplace(const T1& value1, const T2& value2)
{
storage.value = T(value1, value2);
valid = true;
}
//*************************************************************************
/// Emplaces a value.
/// 3 parameters.
//*************************************************************************
template <typename T1, typename T2, typename T3>
void emplace(const T1& value1, const T2& value2, const T3& value3)
{
storage.value = T(value1, value2, value3);
valid = true;
}
//*************************************************************************
/// Emplaces a value.
/// 4 parameters.
//*************************************************************************
template <typename T1, typename T2, typename T3, typename T4>
void emplace(const T1& value1, const T2& value2, const T3& value3, const T4& value4)
{
storage.value = T(value1, value2, value3, value4);
valid = true;
}
#endif
private:
bool valid;
struct dummy_t {};
union storage_type
{
dummy_t dummy;
T value;
ETL_CONSTEXPR storage_type()
: dummy()
{
}
ETL_CONSTEXPR storage_type(const T& v)
: value(v)
{
}
};
storage_type storage;
};
#include "private/diagnostic_uninitialized_push.h"
//***************************************************************************