From 664bdac4bddd105925f8cc7d4b910af4c72d1151 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sun, 21 Jan 2024 15:02:01 +0000 Subject: [PATCH] Initial refactor --- include/etl/optional.h | 437 +---------------------------------------- 1 file changed, 1 insertion(+), 436 deletions(-) diff --git a/include/etl/optional.h b/include/etl/optional.h index d85763d2..c638427c 100644 --- a/include/etl/optional.h +++ b/include/etl/optional.h @@ -103,15 +103,8 @@ namespace etl /// See http://en.cppreference.com/w/cpp/utility/optional ///\tparam T The type to store. ///\ingroup utilities - //***************************************************************************** - template ::value> - class optional; - - //***************************************************************************** - /// For non POD types. - //***************************************************************************** template - class optional + 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 - class optional - { - 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 - ETL_CONSTEXPR20_STL - etl::enable_if_t::value, T> - value_or(U&& default_value) const& - { - if (has_value()) - { - return value(); - } - else - { - return static_cast(etl::forward(default_value)); - } - } - - //*************************************************************************** - /// Gets the value or a default if not valid. - //*************************************************************************** - template - ETL_CONSTEXPR20_STL - etl::enable_if_t::value, T> - value_or(U&& default_value) && - { - if (has_value()) - { - return etl::move(value()); - } - else - { - return static_cast(etl::forward(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 - ETL_CONSTEXPR14 void emplace(Args && ... args) - { - storage.value = T(ETL_OR_STD::forward(args)...); - valid = true; - } -#else - //************************************************************************* - /// Emplaces a value. - /// 0 parameters. - //************************************************************************* - void emplace() - { - storage.value = value_type(); - valid = true; - } - - //************************************************************************* - /// Emplaces a value. - /// 1 parameter. - //************************************************************************* - template - void emplace(const T1& value1) - { - storage.value = value1; - valid = true; - } - - //************************************************************************* - /// Emplaces a value. - /// 2 parameters. - //************************************************************************* - template - void emplace(const T1& value1, const T2& value2) - { - storage.value = T(value1, value2); - valid = true; - } - - //************************************************************************* - /// Emplaces a value. - /// 3 parameters. - //************************************************************************* - template - 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 - 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" //***************************************************************************