Skip to content

Commit

Permalink
Merge branch 'pull-request/#733-Fix-etl--expected-assignment-from-etl…
Browse files Browse the repository at this point in the history
…--unexpected' into development
  • Loading branch information
jwellbelove committed Jul 18, 2023
2 parents 6da22a6 + 8d1219b commit 75be75b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 16 deletions.
34 changes: 18 additions & 16 deletions include/etl/expected.h
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ namespace etl
{
ETL_STATIC_ASSERT(etl::is_copy_constructible<TValue>::value, "Value not copy assignable");

storage.template emplace<Value_Type>(value);
storage = value;
return *this;
}

Expand All @@ -444,31 +444,33 @@ namespace etl
{
ETL_STATIC_ASSERT(etl::is_move_constructible<TValue>::value, "Value not move assignable");

storage.template emplace<Value_Type>(etl::move(value));
storage = etl::move(value);
return *this;
}
#endif

//*******************************************
/// Copy assign from error
/// Copy assign from unexpected
//*******************************************
expected& operator =(const unexpected_type& error)
expected& operator =(const unexpected_type& ue)
{
ETL_STATIC_ASSERT(etl::is_copy_constructible<TError>::value, "Error not copy assignable");

storage.template emplace<Error_Type>(error);
storage = ue.error();

return *this;
}

#if ETL_USING_CPP11
//*******************************************
/// Move assign from error
/// Move assign from unexpected
//*******************************************
expected& operator =(unexpected_type&& error)
expected& operator =(unexpected_type&& ue)
{
ETL_STATIC_ASSERT(etl::is_move_constructible<TError>::value, "Error not move assignable");

storage.template emplace<Error_Type>(etl::move(error));
storage = etl::move(ue.error());

return *this;
}
#endif
Expand Down Expand Up @@ -620,7 +622,7 @@ namespace etl
template <typename... Args>
ETL_CONSTEXPR14 value_type& emplace(Args&&... args) ETL_NOEXCEPT
{
storage.emplace(args...);
storage.emplace(etl::forward<Args>(args)...);
}

//*******************************************
Expand All @@ -629,7 +631,7 @@ namespace etl
template <typename U, typename... Args>
ETL_CONSTEXPR14 value_type& emplace(std::initializer_list<U>& il, Args&&... args) ETL_NOEXCEPT
{
storage.emplace(il, args...);
storage.emplace(il, etl::forward<Args>(args)...);
}
#else
//*******************************************
Expand Down Expand Up @@ -830,25 +832,25 @@ namespace etl
#endif

//*******************************************
/// Copy assign from error
/// Copy assign from unexpected
//*******************************************
expected& operator =(const unexpected_type& error)
expected& operator =(const unexpected_type& ue)
{
ETL_STATIC_ASSERT(etl::is_copy_constructible<TError>::value, "Error not copy assignable");

storage.template emplace<Error_Type>(error);
storage.template emplace<Error_Type>(ue.error());
return *this;
}

#if ETL_USING_CPP11
//*******************************************
/// Move assign from error
/// Move assign from unexpected
//*******************************************
expected& operator =(unexpected_type&& error)
expected& operator =(unexpected_type&& ue)
{
ETL_STATIC_ASSERT(etl::is_move_constructible<TError>::value, "Error not move assignable");

storage.template emplace<Error_Type>(etl::move(error));
storage.template emplace<Error_Type>(etl::move(ue.error()));
return *this;
}
#endif
Expand Down
39 changes: 39 additions & 0 deletions test/test_expected.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ namespace
{
}

operator std::string() const
{
return e;
}

ErrorM(ErrorM&&) = default;
ErrorM& operator =(ErrorM&&) = default;

Expand Down Expand Up @@ -322,6 +327,23 @@ namespace
//CHECK(output2.v == input2.v);
}

//*************************************************************************
TEST(test_copy_assign_from_error)
{
Value input = { "value 1" };
Expected expected(input);

Error error = { "error 1" };
Unexpected unexpected(error);

expected = unexpected;

Error output = expected.error();

CHECK_FALSE(expected.has_value());
CHECK_EQUAL(std::string(error), std::string(expected.error()));
}

//*************************************************************************
TEST(test_move_construct)
{
Expand Down Expand Up @@ -360,6 +382,23 @@ namespace
CHECK_EQUAL("value 1", output2.v);
}

//*************************************************************************
TEST(test_move_assign_from_error)
{
ValueM input = { "value 1" };
ExpectedM expected(etl::move(input));

ErrorM error = { "error 1" };
UnexpectedM unexpected(etl::move(error));

expected = etl::move(unexpected);

ErrorM output = etl::move(expected.error());

CHECK_FALSE(expected.has_value());
CHECK_EQUAL(std::string(error), std::string(expected.error()));
}

//*************************************************************************
TEST(test_copy_construct_void_value)
{
Expand Down

0 comments on commit 75be75b

Please sign in to comment.