Changed etl::expected_invalid to non-template type

Added tests for rereference operators
This commit is contained in:
John Wellbelove 2023-11-14 10:34:38 +00:00
parent f1dd0d4490
commit 6c3eddaf76
2 changed files with 52 additions and 24 deletions

View File

@ -57,12 +57,7 @@ namespace etl
//***************************************************************************
/// expected_invalid
//***************************************************************************
template <typename TError>
class expected_invalid;
//*******************************************
template<>
class expected_invalid<void> : public etl::expected_exception
class expected_invalid : public etl::expected_exception
{
public:
@ -72,18 +67,6 @@ namespace etl
}
};
//*******************************************
template <typename TError>
class expected_invalid : etl::expected_invalid<void>
{
public:
expected_invalid(string_type file_name_, numeric_type line_number_)
: expected_invalid<void>(file_name_, line_number_)
{
}
};
//***************************************************************************
/// Unexpected type.
/// etl::unexpected represents an unexpected value stored in etl::expected.
@ -668,7 +651,7 @@ namespace etl
value_type* operator ->()
{
#if ETL_IS_DEBUG_BUILD
ETL_ASSERT(storage.index() == Value_Type, ETL_ERROR(expected_invalid<TError>));
ETL_ASSERT(has_value(), ETL_ERROR(expected_invalid));
#endif
return etl::addressof(etl::get<value_type>(storage));
@ -680,7 +663,7 @@ namespace etl
const value_type* operator ->() const
{
#if ETL_IS_DEBUG_BUILD
ETL_ASSERT(storage.index() == Value_Type, ETL_ERROR(expected_invalid<TError>));
ETL_ASSERT(has_value(), ETL_ERROR(expected_invalid));
#endif
return etl::addressof(etl::get<value_type>(storage));
@ -692,7 +675,7 @@ namespace etl
value_type& operator *() ETL_LVALUE_REF_QUALIFIER
{
#if ETL_IS_DEBUG_BUILD
ETL_ASSERT(storage.index() == Value_Type, ETL_ERROR(expected_invalid<TError>));
ETL_ASSERT(has_value(), ETL_ERROR(expected_invalid));
#endif
return etl::get<value_type>(storage);
@ -704,7 +687,7 @@ namespace etl
const value_type& operator *() const ETL_LVALUE_REF_QUALIFIER
{
#if ETL_IS_DEBUG_BUILD
ETL_ASSERT(storage.index() == Value_Type, ETL_ERROR(expected_invalid<TError>));
ETL_ASSERT(has_value(), ETL_ERROR(expected_invalid));
#endif
return etl::get<value_type>(storage);
@ -717,7 +700,7 @@ namespace etl
value_type&& operator *()&&
{
#if ETL_IS_DEBUG_BUILD
ETL_ASSERT(storage.index() == Value_Type, ETL_ERROR(expected_invalid<TError>));
ETL_ASSERT(has_value(), ETL_ERROR(expected_invalid));
#endif
return etl::move(etl::get<value_type>(storage));
@ -729,7 +712,7 @@ namespace etl
const value_type&& operator *() const&&
{
#if ETL_IS_DEBUG_BUILD
ETL_ASSERT(storage.index() == Value_Type, ETL_ERROR(expected_invalid<TError>));
ETL_ASSERT(has_value(), ETL_ERROR(expected_invalid));
#endif
return etl::move(etl::get<value_type>(storage));

View File

@ -483,6 +483,28 @@ namespace
CHECK_EQUAL("error 1", output2.e);
}
//*************************************************************************
TEST(test_dereference_operators)
{
struct ExpectedType
{
ExpectedType(int i_)
: i(i_)
{
}
int i;
};
etl::expected<ExpectedType, int> exp = etl::unexpected<int>(0);
const etl::expected<ExpectedType, int> cexp = etl::unexpected<int>(0);
CHECK_THROW({ int i = (*exp).i; (void)i; }, etl::expected_invalid);
CHECK_THROW({ int i = (*cexp).i; (void)i; }, etl::expected_invalid);
CHECK_THROW({ int i = exp->i; (void)i; }, etl::expected_invalid);
CHECK_THROW({ int i = cexp->i; (void)i; }, etl::expected_invalid);
}
//*************************************************************************
struct value_or_helper
{
@ -519,5 +541,28 @@ namespace
test_exp = etl::unexpected<int>(2);
CHECK_FALSE(test_exp.has_value());
}
//*************************************************************************
TEST(test_expected_does_not_compile_with_ETL_LOG_ERRORS_bug_787)
{
etl::expected<int, int> test_exp = etl::unexpected<int>(0);
bool thrown = false;
std::string thrown_what;
std::string exception_what = etl::expected_invalid(__FILE__, __LINE__).what();
try
{
int i = *test_exp;
(void)i;
}
catch (etl::exception& e)
{
thrown = true;
thrown_what = e.what(); // what() should be accessible
}
CHECK_TRUE(thrown);
CHECK_TRUE(exception_what == thrown_what);
}
};
}