Work in progress

This commit is contained in:
John Wellbelove 2023-02-08 16:40:15 +01:00
parent a710c77d79
commit 1d12a86114
2 changed files with 1 additions and 384 deletions

View File

@ -240,7 +240,6 @@ namespace etl
TError error_value;
};
#if ETL_USING_CPP17
template <typename TError>
bool operator ==(const etl::unexpected<TError>& lhs, const etl::unexpected<TError>& 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<TError>& lhs, etl::unexpected<TError>& rhs)
}
#endif
//*******************************************
/// Equivalence operator.
//*******************************************
template <typename TError>
bool operator ==(const etl::unexpected<TError>& lhs, const etl::unexpected<TError>& rhs)
{
return lhs.error_value == rhs.error_value;
}
//*******************************************
/// Swap etl::unexpected.
//*******************************************
template <typename TError>
void swap(etl::unexpected<TError>& lhs, etl::unexpected<TError>& 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 <typename TValue, typename TError>
class expected
{
public:
typename TValue value_type;
typename TError error_type;
typename etl::unexpected<TError> unexpected_type;
#if ETL_CPP11_SUPPORTED
template <typename U>
using rebind = etl::expected<U, TError>;
#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 <typename F>
ETL_CONSTEXPR14 explicit expected(const etl::unexpected<F>& ue)
: data(ue)
{
}
#if ETL_CPP11_SUPPORTED
template <typename F>
ETL_CONSTEXPR14 explicit expected(etl::unexpected<F>&& ue)
: data(etl::move(ue))
{
}
#endif
ETL_CONSTEXPR14 explicit expected(etl::in_place_t) ETL_NOEXCEPT
: data(TValue())
{
}
template <typename... Args>
ETL_CONSTEXPR14 explicit expected(etl::in_place_t, Args&&... args)
: data(etl::forward<Args>(args)...)
{
}
template <typename U, typename... Args>
ETL_CONSTEXPR14 explicit expected(etl::in_place_t, std::initializer_list<U> il, Args&&... args)
: data(il, etl::forward<Args>(args)...)
{
}
template <typename... Args>
ETL_CONSTEXPR14 explicit expected(etl::unexpect_t, Args&&... args)
: data(etl::unexpected<TError>(args...))
{
}
template <typename U, typename... Args>
ETL_CONSTEXPR14 explicit expected(etl::unexpect_t, std::initializer_list<U> il, Args&&... args)
: data(etl::unexpected<TError>(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<TValue>(data);
}
//*******************************************
///
//*******************************************
ETL_CONSTEXPR14 const T& value() const&
{
return etl::get<TValue>(data);
}
//*******************************************
///
//*******************************************
ETL_CONSTEXPR14 T&& value() &&
{
return etl::move(etl::get<TValue>(data));
}
//*******************************************
///
//*******************************************
ETL_CONSTEXPR14 const T&& value() const&&
{
return etl::move(etl::get<TValue>(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<TValue>(data);
}
#endif
#if ETL_CPP11_SUPPORTED
//*******************************************
///
//*******************************************
template <typename U>
ETL_CONSTEXPR14 TValue value_or(U&& default_value) const&
{
if (has_value())
{
return value();
}
else
{
return default_value;
}
}
//*******************************************
///
//*******************************************
template <typename U>
ETL_CONSTEXPR14 TValue value_or(U&& default_value) &&
{
if (has_value())
{
return etl::move(value());
}
else
{
return etl::move(default_value);
}
}
#else
//*******************************************
///
//*******************************************
template <typename U>
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<TError>(data);
}
//*******************************************
///
//*******************************************
ETL_CONSTEXPR14 TError& error() & ETL_NOEXCEPT
{
return etl::get<TError>(data);
}
//*******************************************
///
//*******************************************
ETL_CONSTEXPR14 const TError&& error() const&& ETL_NOEXCEPT
{
return etl::move(etl::get<TError>(data));
}
//*******************************************
///
//*******************************************
ETL_CONSTEXPR14 TError&& error() && ETL_NOEXCEPT
{
return etl::move(etl::get<TError>(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<TError>(data);
}
#endif
#if ETL_CPP11_SUPPORTED
//*******************************************
///
//*******************************************
template <typename... Args>
ETL_CONSTEXPR14 T& emplace(Args&&... args) ETL_NOEXCEPT
{
data.emplace(args...);
}
//*******************************************
///
//*******************************************
template <typename U, typename... Args>
ETL_CONSTEXPR14 T& emplace(std::initializer_list<U>& 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<TValue>());
}
//*******************************************
///
//*******************************************
const TValue* operator ->() const
{
#if ETL_IS_DEBUG_BUILD
ETL_ASSERT(valid, ETL_ERROR(expected_invalid));
#endif
return etl::addressof(data.get<TValue>());
}
//*******************************************
///
//*******************************************
TValue& operator *()
{
#if ETL_IS_DEBUG_BUILD
ETL_ASSERT(valid, ETL_ERROR(expected_invalid));
#endif
return data.get<TValue>();
}
//*******************************************
///
//*******************************************
const TValue& operator *() const
{
#if ETL_IS_DEBUG_BUILD
ETL_ASSERT(valid, ETL_ERROR(expected_invalid));
#endif
return data.get<TValue>();
}
private:
etl::variant<TValue, TError> data;
};
//*****************************************************************************
/// Specialisation for void value type.
//*****************************************************************************
template<typename TError>
class expected<void, TError>
{
public:
private:
TError error;
};
}
#endif

View File

@ -185,6 +185,7 @@ namespace
TEST(test_constructor_for_result_with_error)
{
Error input = { "error 1" };
Unexpected unexpected(input);
Expected expected(unexpected);