Provides a contiguous iterator as defined by the C++ specification, with the follwing differences:
More...
#include <contiguous_iterator.hpp>
template<typename T>
class bsl::contiguous_iterator< T >
Provides a contiguous iterator as defined by the C++ specification, with the follwing differences:
- The difference type that we use is a bsl::uintmax instead of a signed type, which causes a lot of problems with AUTOSAR compliance as signed/unsigned conversions and overflow are a huge problem with the standard library. This iterator type is used by all of the "view" type containers including the bsl::span, bsl::array and bsl::string_view
- We do not provide any of the *, -> or [] accessors as none of these accessors are compliant with AUTOSAR. Instead, we provide a get_if() function, which returns a pointer to the element being accessed by the iterator, or a nullptr if the iterator is invalid or is the same as end(). As a result, ranged based for loops are not supported, and instead, use a view's for_each function which will perform the same action, with less overhead, and better safety.
- The iterator is always inbounds, equal to end() or is invalid. Traditional iterators can be anything, they can overrun, underrun, and everyting in between. If this iterator is valid, the index is always bounded by the size of the array it is pointing to, or is equal to end(). Wrapping, overruns, and underruns are not possible.
namespace bsl
{
inline void
example_contiguous_iterator_overview() noexcept
{
constexpr bsl::string_view str{"Hello"};
});
}
}
- Template Parameters
-
T | the type of element being iterated. |
◆ contiguous_iterator()
Creates a contiguous iterator given a ptr to an array and the total number of elements in the array. Note that you should not use this directly but instead, should use the container's begin() function.
- Parameters
-
ptr | a pointer to the array being iterated |
count | the number of elements in the array being iterated |
i | the initial index of the iterator |
◆ data() [1/2]
Returns a pointer to the array being iterated.
namespace bsl
{
inline void
example_contiguous_iterator_data() noexcept
{
constexpr bsl::string_view str{"Hello"};
constexpr bsl::string_view::iterator_type iter{str.begin()};
if (str.data() == iter.data()) {
}
}
}
- Returns
- Returns a pointer to the array being iterated
◆ data() [2/2]
Returns a pointer to the array being iterated.
namespace bsl
{
inline void
example_contiguous_iterator_data() noexcept
{
constexpr bsl::string_view str{"Hello"};
constexpr bsl::string_view::iterator_type iter{str.begin()};
if (str.data() == iter.data()) {
}
}
}
- Returns
- Returns a pointer to the array being iterated
◆ size()
Returns the number of elements in the array being iterated.
namespace bsl
{
inline void
example_contiguous_iterator_size() noexcept
{
constexpr bsl::string_view str{"Hello"};
constexpr bsl::string_view::iterator_type iter{str.begin()};
if (str.size() == iter.size()) {
}
}
}
- Returns
- Returns the number of elements in the array being iterated
◆ index()
Returns the iterator's current index. If the iterator is at the end, this function returns size().
namespace bsl
{
inline void
example_contiguous_iterator_index() noexcept
{
constexpr bsl::string_view str{"Hello"};
constexpr bsl::string_view::iterator_type iter{str.begin()};
if (0U == iter.index()) {
}
}
}
- Returns
- Returns the iterator's current index
◆ empty()
Returns nullptr == data()
namespace bsl
{
inline void
example_contiguous_iterator_empty() noexcept
{
constexpr bsl::string_view str{"Hello"};
constexpr bsl::string_view::iterator_type iter{str.begin()};
if (!iter.empty()) {
}
}
}
- Returns
- Returns nullptr == data()
◆ is_end()
Returns index() == size()
namespace bsl
{
inline void
example_contiguous_iterator_is_end() noexcept
{
constexpr bsl::string_view str{"Hello"};
constexpr bsl::string_view::iterator_type iter{str.end()};
if (iter.is_end()) {
}
}
}
- Returns
- Returns index() == size()
◆ get_if()
Returns a pointer to the instance of T stored at the iterator's current index. If the index is out of bounds, or the iterator is invalid, this function returns a nullptr.
namespace bsl
{
inline void
example_contiguous_iterator_get_if() noexcept
{
constexpr bsl::string_view str{"Hello"};
bsl::string_view::iterator_type iter{str.begin()};
if (auto const *const ptr = iter.get_if()) {
}
}
}
SUPPRESSION: PRQA 4024 - false positive
- We suppress this because A9-3-1 states that pointer we should not provide a non-const reference or pointer to private member function, unless the class mimics a smart pointer or a containter. This class mimics a container.
- Returns
- Returns a pointer to the instance of T stored at the iterator's current index. If the index is out of bounds, or the iterator is invalid, this function returns a nullptr.
◆ operator++()
Increments the iterator.
namespace bsl
{
inline void
example_contiguous_iterator_increment() noexcept
{
constexpr bsl::string_view str{"Hello"};
bsl::string_view::iterator_type iter{str.begin()};
++iter;
if (auto const *const ptr = iter.get_if()) {
}
}
}
- Returns
- returns *this
◆ operator--()
Decrements the iterator.
namespace bsl
{
inline void
example_contiguous_iterator_decrement() noexcept
{
constexpr bsl::string_view str{"Hello"};
bsl::string_view::iterator_type iter{str.end()};
--iter;
if (auto const *const ptr = iter.get_if()) {
}
}
}
- Returns
- returns *this
◆ operator==()
Returns true if the provided contiguous iterators point to the same array and the same index.
namespace bsl
{
inline void
example_contiguous_iterator_equals() noexcept
{
constexpr bsl::string_view str{"Hello"};
constexpr bsl::string_view::iterator_type iter1{str.begin()};
constexpr bsl::string_view::iterator_type iter2{str.begin()};
constexpr bsl::string_view::iterator_type iter3{str.end()};
if (iter1 == iter2) {
}
if (iter1 != iter3) {
}
if (iter1 < iter3) {
}
if (iter1 <= iter2) {
}
if (iter3 > iter1) {
}
if (iter3 >= iter1) {
}
}
}
- Template Parameters
-
T | the type of element being iterated. |
- Parameters
-
lhs | the left hand side of the operation |
rhs | the rhs hand side of the operation |
- Returns
- Returns true if the provided contiguous iterators point to the same array and the same index.
◆ operator!=()
Returns true if the provided contiguous iterators do not point to the same array or the same index.
namespace bsl
{
inline void
example_contiguous_iterator_not_equals() noexcept
{
constexpr bsl::string_view str{"Hello"};
constexpr bsl::string_view::iterator_type iter1{str.begin()};
constexpr bsl::string_view::iterator_type iter2{str.begin()};
constexpr bsl::string_view::iterator_type iter3{str.end()};
if (iter1 == iter2) {
}
if (iter1 != iter3) {
}
if (iter1 < iter3) {
}
if (iter1 <= iter2) {
}
if (iter3 > iter1) {
}
if (iter3 >= iter1) {
}
}
}
- Template Parameters
-
T | the type of element being iterated. |
- Parameters
-
lhs | the left hand side of the operation |
rhs | the rhs hand side of the operation |
- Returns
- Returns true if the provided contiguous iterators do not point to the same array or the same index.
◆ operator<()
Returns lhs.index() < rhs.index()
namespace bsl
{
inline void
example_contiguous_iterator_lt() noexcept
{
constexpr bsl::string_view str{"Hello"};
constexpr bsl::string_view::iterator_type iter1{str.begin()};
constexpr bsl::string_view::iterator_type iter2{str.begin()};
constexpr bsl::string_view::iterator_type iter3{str.end()};
if (iter1 == iter2) {
}
if (iter1 != iter3) {
}
if (iter1 < iter3) {
}
if (iter1 <= iter2) {
}
if (iter3 > iter1) {
}
if (iter3 >= iter1) {
}
}
}
- Template Parameters
-
T | the type of element being iterated. |
- Parameters
-
lhs | the left hand side of the operation |
rhs | the rhs hand side of the operation |
- Returns
- Returns lhs.index() < rhs.index()
◆ operator<=()
Returns lhs.index() <= rhs.index()
namespace bsl
{
inline void
example_contiguous_iterator_lt_equals() noexcept
{
constexpr bsl::string_view str{"Hello"};
constexpr bsl::string_view::iterator_type iter1{str.begin()};
constexpr bsl::string_view::iterator_type iter2{str.begin()};
constexpr bsl::string_view::iterator_type iter3{str.end()};
if (iter1 == iter2) {
}
if (iter1 != iter3) {
}
if (iter1 < iter3) {
}
if (iter1 <= iter2) {
}
if (iter3 > iter1) {
}
if (iter3 >= iter1) {
}
}
}
- Template Parameters
-
T | the type of element being iterated. |
- Parameters
-
lhs | the left hand side of the operation |
rhs | the rhs hand side of the operation |
- Returns
- Returns lhs.index() <= rhs.index()
◆ operator>()
Returns lhs.index() > rhs.index()
namespace bsl
{
inline void
example_contiguous_iterator_gt() noexcept
{
constexpr bsl::string_view str{"Hello"};
constexpr bsl::string_view::iterator_type iter1{str.begin()};
constexpr bsl::string_view::iterator_type iter2{str.begin()};
constexpr bsl::string_view::iterator_type iter3{str.end()};
if (iter1 == iter2) {
}
if (iter1 != iter3) {
}
if (iter1 < iter3) {
}
if (iter1 <= iter2) {
}
if (iter3 > iter1) {
}
if (iter3 >= iter1) {
}
}
}
- Template Parameters
-
T | the type of element being iterated. |
- Parameters
-
lhs | the left hand side of the operation |
rhs | the rhs hand side of the operation |
- Returns
- Returns lhs.index() > rhs.index()
◆ operator>=()
Returns lhs.index() >= rhs.index()
namespace bsl
{
inline void
example_contiguous_iterator_gt_equals() noexcept
{
constexpr bsl::string_view str{"Hello"};
constexpr bsl::string_view::iterator_type iter1{str.begin()};
constexpr bsl::string_view::iterator_type iter2{str.begin()};
constexpr bsl::string_view::iterator_type iter3{str.end()};
if (iter1 == iter2) {
}
if (iter1 != iter3) {
}
if (iter1 < iter3) {
}
if (iter1 <= iter2) {
}
if (iter3 > iter1) {
}
if (iter3 >= iter1) {
}
}
}
- Template Parameters
-
T | the type of element being iterated. |
- Parameters
-
lhs | the left hand side of the operation |
rhs | the rhs hand side of the operation |
- Returns
- Returns lhs.index() >= rhs.index()
The documentation for this class was generated from the following file: