Added global data() overloads to complement etl::size()

This commit is contained in:
John Wellbelove 2026-03-06 11:07:27 +00:00
parent 1ed1e3ce42
commit fe701f9dc6
2 changed files with 76 additions and 5 deletions

View File

@ -67,7 +67,7 @@ namespace etl
typedef typename TIterator::pointer pointer;
typedef typename TIterator::reference reference;
};
// For pointers.
template <typename T>
struct iterator_traits<T*, void>
@ -606,7 +606,7 @@ namespace etl
ETL_CONSTEXPR14 back_insert_iterator& operator =(const typename TContainer::value_type& value)
{
container->push_back(value);
return (*this);
}
@ -617,7 +617,7 @@ namespace etl
ETL_CONSTEXPR14 back_insert_iterator& operator =(typename TContainer::value_type&& value)
{
container->push_back(etl::move(value));
return (*this);
}
#endif // ETL_USING_CPP11
@ -655,8 +655,8 @@ namespace etl
/// Creates a back_insert_iterator from a container.
//***************************************************************************
template <typename TContainer>
ETL_NODISCARD
ETL_CONSTEXPR14
ETL_NODISCARD
ETL_CONSTEXPR14
etl::back_insert_iterator<TContainer> back_inserter(TContainer& container)
{
return etl::back_insert_iterator<TContainer>(container);
@ -1211,6 +1211,48 @@ namespace etl
char(&array_size(T(&array)[Array_Size]))[Array_Size];
#define ETL_ARRAY_SIZE(a) sizeof(etl::array_size(a))
#if ETL_NOT_USING_STL || ETL_CPP17_NOT_SUPPORTED
//**************************************************************************
/// Returns a pointer to the block of memory containing the elements of the range.
///\ingroup container
//**************************************************************************
template<typename TContainer>
ETL_CONSTEXPR typename TContainer::pointer data(TContainer& container)
{
return container.data();
}
//**************************************************************************
/// Returns a const_pointer to the block of memory containing the elements of the range.
///\ingroup container
//**************************************************************************
template<typename TContainer>
ETL_CONSTEXPR typename TContainer::const_pointer data(const TContainer& container)
{
return container.data();
}
///**************************************************************************
/// Returns a pointer to the block of memory containing the elements of the range.
///\ingroup container
///**************************************************************************
template<typename TValue, size_t Array_Size>
ETL_CONSTEXPR TValue* data(TValue(&a)[Array_Size])
{
return a;
}
///**************************************************************************
/// Returns a const pointer to the block of memory containing the elements of the range.
///\ingroup container
///**************************************************************************
template<typename TValue, size_t Array_Size>
ETL_CONSTEXPR const TValue* data(const TValue(&a)[Array_Size])
{
return a;
}
#endif
}
#endif

View File

@ -31,6 +31,7 @@ SOFTWARE.
#include "etl/container.h"
#include <list>
#include <vector>
#if ETL_NOT_USING_STL
@ -145,6 +146,34 @@ namespace
size_t compiletime_size = sizeof(etl::array_size(data));
CHECK_EQUAL(SIZE, compiletime_size);
}
//*************************************************************************
TEST(test_stl_style_container_data)
{
const size_t SIZE = 10UL;
std::vector<int> data(SIZE);
const std::vector<int> cdata(SIZE);
int* pdata = ETL_OR_STD17::data(data);
const int* pcdata = ETL_OR_STD17::data(cdata);
CHECK(data.data() == pdata);
CHECK(cdata.data() == pcdata);
}
//*************************************************************************
TEST(test_c_array_data)
{
const size_t SIZE = 10UL;
int data[SIZE];
const int cdata[SIZE] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int* pdata = ETL_OR_STD17::data(data);
const int* pcdata = ETL_OR_STD17::data(cdata);
CHECK(&data[0] == pdata);
CHECK(&cdata[0] == pcdata);
}
}
}