diff --git a/include/etl/expected.h b/include/etl/expected.h index f2a91997..2c1b96ef 100644 --- a/include/etl/expected.h +++ b/include/etl/expected.h @@ -240,7 +240,6 @@ namespace etl TError error_value; }; -#if ETL_USING_CPP17 template bool operator ==(const etl::unexpected& lhs, const etl::unexpected& rhs) { @@ -265,7 +264,6 @@ namespace etl { } }; -#endif #if ETL_USING_CPP17 inline constexpr unexpect_t unexpect{}; @@ -863,385 +861,3 @@ void swap(etl::unexpected& lhs, etl::unexpected& rhs) } #endif - //******************************************* - /// Equivalence operator. - //******************************************* - template - bool operator ==(const etl::unexpected& lhs, const etl::unexpected& rhs) - { - return lhs.error_value == rhs.error_value; - } - - //******************************************* - /// Swap etl::unexpected. - //******************************************* - template - void swap(etl::unexpected& lhs, etl::unexpected& rhs) - { - lhs.swap(rhs); - } - - //***************************************************************************** - /// unexpect_t - //***************************************************************************** - struct unexpect_t - { - ETL_CONSTEXPR14 explicit unexpect_t() - { - } - }; - -#if ETL_CPP14_SUPPORTED - inline ETL_CONSTEXPR14 unexpect_t unexpect{}; -#endif - - //***************************************************************************** - /// Expected type. - //***************************************************************************** - template - class expected - { - public: - - typename TValue value_type; - typename TError error_type; - typename etl::unexpected unexpected_type; - -#if ETL_CPP11_SUPPORTED - template - using rebind = etl::expected; -#endif - - //******************************************* - /// Default constructor - //******************************************* - ETL_CONSTEXPR14 expected() ETL_NOEXCEPT - : data(TValue()) - { - } - - //******************************************* - /// Copy constructor - //******************************************* - ETL_CONSTEXPR14 expected(const expected& other) ETL_NOEXCEPT - : data(other.data) - { - } - -#if ETL_CPP11_SUPPORTED - //******************************************* - /// Move constructor - //******************************************* - ETL_CONSTEXPR14 expected(expected&& other) ETL_NOEXCEPT - : data(etl::move(other.data)) - { - } -#endif - - template - ETL_CONSTEXPR14 explicit expected(const etl::unexpected& ue) - : data(ue) - { - } - -#if ETL_CPP11_SUPPORTED - template - ETL_CONSTEXPR14 explicit expected(etl::unexpected&& ue) - : data(etl::move(ue)) - { - } -#endif - - ETL_CONSTEXPR14 explicit expected(etl::in_place_t) ETL_NOEXCEPT - : data(TValue()) - { - } - - template - ETL_CONSTEXPR14 explicit expected(etl::in_place_t, Args&&... args) - : data(etl::forward(args)...) - { - } - - template - ETL_CONSTEXPR14 explicit expected(etl::in_place_t, std::initializer_list il, Args&&... args) - : data(il, etl::forward(args)...) - { - } - - template - ETL_CONSTEXPR14 explicit expected(etl::unexpect_t, Args&&... args) - : data(etl::unexpected(args...)) - { - } - - template - ETL_CONSTEXPR14 explicit expected(etl::unexpect_t, std::initializer_list il, Args&&... args) - : data(etl::unexpected(il, args...)) - { - } - - //******************************************* - /// Copy assign from value - //******************************************* - expected& operator =(const TValue& value) - { - data = value; - return *this; - } - - //******************************************* - /// Move assign from value - //******************************************* - expected& operator =(TValue&& value) - { - data = etl::move(value); - return *this; - } - - //******************************************* - /// Copy assign from error - //******************************************* - expected& operator =(const TError& error) - { - data = error; - return *this; - } - - //******************************************* - /// Move assign from error - //******************************************* - expected& operator =(TError&& error) - { - data = etl::move(error); - return *this; - } - -#if ETL_CPP11_SUPPORTED - //******************************************* - /// - //******************************************* - ETL_CONSTEXPR14 T& value() & - { - return etl::get(data); - } - - //******************************************* - /// - //******************************************* - ETL_CONSTEXPR14 const T& value() const& - { - return etl::get(data); - } - - //******************************************* - /// - //******************************************* - ETL_CONSTEXPR14 T&& value() && - { - return etl::move(etl::get(data)); - } - - //******************************************* - /// - //******************************************* - ETL_CONSTEXPR14 const T&& value() const&& - { - return etl::move(etl::get(data)); - } -#else - //******************************************* - /// Returns a const reference to the value. - /// Undefined if the expected does not contain an value. - //******************************************* - ETL_CONSTEXPR14 const TValue& value() const - { - return etl::get(data); - } -#endif - -#if ETL_CPP11_SUPPORTED - //******************************************* - /// - //******************************************* - template - ETL_CONSTEXPR14 TValue value_or(U&& default_value) const& - { - if (has_value()) - { - return value(); - } - else - { - return default_value; - } - } - - //******************************************* - /// - //******************************************* - template - ETL_CONSTEXPR14 TValue value_or(U&& default_value) && - { - if (has_value()) - { - return etl::move(value()); - } - else - { - return etl::move(default_value); - } - } -#else - //******************************************* - /// - //******************************************* - template - TValue value_or(const U& default_value) const - { - if (has_value()) - { - return value(); - } - else - { - return default_value; - } - } -#endif - -#if ETL_CPP11_SUPPORTED - //******************************************* - /// - //******************************************* - ETL_CONSTEXPR14 const TError& error() const& ETL_NOEXCEPT - { - return etl::get(data); - } - - //******************************************* - /// - //******************************************* - ETL_CONSTEXPR14 TError& error() & ETL_NOEXCEPT - { - return etl::get(data); - } - - //******************************************* - /// - //******************************************* - ETL_CONSTEXPR14 const TError&& error() const&& ETL_NOEXCEPT - { - return etl::move(etl::get(data)); - } - - //******************************************* - /// - //******************************************* - ETL_CONSTEXPR14 TError&& error() && ETL_NOEXCEPT - { - return etl::move(etl::get(data)); - } -#else - //******************************************* - /// Returns a const reference to the error. - /// Undefined if the expected does not contain an error. - //******************************************* - ETL_CONSTEXPR14 const TError& error() const - { - return etl::get(data); - } -#endif - -#if ETL_CPP11_SUPPORTED - //******************************************* - /// - //******************************************* - template - ETL_CONSTEXPR14 T& emplace(Args&&... args) ETL_NOEXCEPT - { - data.emplace(args...); - } - - //******************************************* - /// - //******************************************* - template - ETL_CONSTEXPR14 T& emplace(std::initializer_list& il, Args&&... args) ETL_NOEXCEPT - { - data.emplace(il, args...); - } -#endif - - //******************************************* - /// - //******************************************* - TValue* operator ->() - { -#if ETL_IS_DEBUG_BUILD - ETL_ASSERT(valid, ETL_ERROR(expected_invalid)); -#endif - - return etl::addressof(data.get()); - } - - //******************************************* - /// - //******************************************* - const TValue* operator ->() const - { -#if ETL_IS_DEBUG_BUILD - ETL_ASSERT(valid, ETL_ERROR(expected_invalid)); -#endif - - return etl::addressof(data.get()); - } - - //******************************************* - /// - //******************************************* - TValue& operator *() - { -#if ETL_IS_DEBUG_BUILD - ETL_ASSERT(valid, ETL_ERROR(expected_invalid)); -#endif - - return data.get(); - } - - //******************************************* - /// - //******************************************* - const TValue& operator *() const - { -#if ETL_IS_DEBUG_BUILD - ETL_ASSERT(valid, ETL_ERROR(expected_invalid)); -#endif - - return data.get(); - } - - private: - - etl::variant data; - }; - - //***************************************************************************** - /// Specialisation for void value type. - //***************************************************************************** - template - class expected - { - public: - - - - private: - - TError error; - }; -} - - -#endif - diff --git a/test/test_expected.cpp b/test/test_expected.cpp index 3f67f6f8..3e43e04c 100644 --- a/test/test_expected.cpp +++ b/test/test_expected.cpp @@ -185,6 +185,7 @@ namespace TEST(test_constructor_for_result_with_error) { Error input = { "error 1" }; + Unexpected unexpected(input); Expected expected(unexpected);