diff --git a/include/etl/iterator.h b/include/etl/iterator.h index 80535132..d3b47d20 100644 --- a/include/etl/iterator.h +++ b/include/etl/iterator.h @@ -67,7 +67,7 @@ namespace etl typedef typename TIterator::pointer pointer; typedef typename TIterator::reference reference; }; - + // For pointers. template struct iterator_traits @@ -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 - ETL_NODISCARD - ETL_CONSTEXPR14 + ETL_NODISCARD + ETL_CONSTEXPR14 etl::back_insert_iterator back_inserter(TContainer& container) { return etl::back_insert_iterator(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 + 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 + 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 + 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 + ETL_CONSTEXPR const TValue* data(const TValue(&a)[Array_Size]) + { + return a; + } +#endif } #endif diff --git a/test/test_container.cpp b/test/test_container.cpp index ece1afba..3583be25 100644 --- a/test/test_container.cpp +++ b/test/test_container.cpp @@ -31,6 +31,7 @@ SOFTWARE. #include "etl/container.h" #include +#include #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 data(SIZE); + const std::vector 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); + } } }