implement .begin() and .end() for etl::optional (#1246)

Co-authored-by: nikdelgado <nikdelgado@icloud.com>
This commit is contained in:
Nik Delgado 2025-12-26 00:01:58 -07:00 committed by John Wellbelove
parent 94d960748d
commit af8ec168c4
2 changed files with 122 additions and 0 deletions

View File

@ -1326,6 +1326,8 @@ namespace etl
public:
typedef T value_type;
typedef T* iterator;
typedef const T* const_iterator;
#if ETL_USING_CPP11
//***************************************************************************
@ -1698,6 +1700,42 @@ namespace etl
return *this;
}
#endif
//***************************************************************************
/// Returns an iterator to the beginning of the optional.
//***************************************************************************
ETL_CONSTEXPR20_STL
iterator begin() ETL_NOEXCEPT
{
return this->has_value() ? this->operator->() : ETL_NULLPTR;
}
//***************************************************************************
/// Returns a const iterator to the beginning of the optional.
//***************************************************************************
ETL_CONSTEXPR20_STL
const_iterator begin() const ETL_NOEXCEPT
{
return this->has_value() ? this->operator->() : ETL_NULLPTR;
}
//***************************************************************************
/// Returns an iterator to the end of the optional.
//***************************************************************************
ETL_CONSTEXPR20_STL
iterator end() ETL_NOEXCEPT
{
return this->has_value() ? this->operator->() + 1 : ETL_NULLPTR;
}
//***************************************************************************
/// Returns a const iterator to the end of the optional.
//***************************************************************************
ETL_CONSTEXPR20_STL
const_iterator end() const ETL_NOEXCEPT
{
return this->has_value() ? this->operator->() + 1 : ETL_NULLPTR;
}
};
#include "private/diagnostic_uninitialized_push.h"

View File

@ -1034,5 +1034,89 @@ namespace
CHECK_EQUAL(1, (*opt3)[0]);
CHECK_EQUAL(20, (*opt3)[1]);
}
//*************************************************************************
TEST(test_begin_end_with_value)
{
etl::optional<int> opt = 4;
CHECK(opt.begin() != opt.end());
CHECK_EQUAL(4, *opt.begin());
CHECK_EQUAL(1, std::distance(opt.begin(), opt.end()));
}
TEST(test_begin_end_empty)
{
etl::optional<int> opt;
CHECK(opt.begin() == ETL_NULLPTR);
CHECK(opt.begin() == opt.end());
}
TEST(test_const_begin_end_with_value)
{
const etl::optional<int> opt = 4;
CHECK(opt.begin() != opt.end());
CHECK_EQUAL(4, *opt.begin());
CHECK_EQUAL(1, std::distance(opt.begin(), opt.end()));
}
TEST(test_const_begin_end_empty)
{
const etl::optional<int> opt;
CHECK(opt.begin() == ETL_NULLPTR);
CHECK(opt.begin() == opt.end());
}
TEST(range_based_for_loop_with_value)
{
etl::optional<int> opt = 4;
int sum = 0;
for (int value : opt)
{
sum += value;
}
CHECK_EQUAL(4, sum);
}
TEST(range_based_for_loop_empty)
{
etl::optional<int> opt;
int sum = 0;
for (int value : opt)
{
sum += value;
}
CHECK_EQUAL(0, sum);
}
TEST(test_range_based_for_loop_non_trivial)
{
etl::optional<Data> opt = Data("TEST");
int count= 0;
for (const Data& value : opt)
{
CHECK_EQUAL(Data("TEST"), value);
count++;
}
CHECK_EQUAL(1, count);
}
TEST(test_begin_end_modify_value)
{
etl::optional<int> opt = 4;
*opt.begin() = 42;
CHECK_EQUAL(42, *opt);
}
}
}