dxx-rebirth/common/include/null_sentinel_iterator.h

78 lines
1.1 KiB
C
Raw Normal View History

2015-04-26 20:15:50 +00:00
#pragma once
#include <iterator>
template <typename I>
2020-05-02 21:18:43 +00:00
class null_sentinel_iterator
2015-04-26 20:15:50 +00:00
{
public:
2020-05-02 21:18:43 +00:00
using iterator_category = std::forward_iterator_tag;
using value_type = I;
using difference_type = std::ptrdiff_t;
using pointer = I *;
using reference = I &;
2019-04-28 00:53:40 +00:00
null_sentinel_iterator() = default;
2020-05-02 21:18:43 +00:00
null_sentinel_iterator(const pointer i) :
2015-04-26 20:15:50 +00:00
p(i)
{
}
2020-05-02 21:18:43 +00:00
pointer get() const
2015-04-26 20:15:50 +00:00
{
return p;
}
2020-05-02 21:18:43 +00:00
value_type operator*() const
2015-04-26 20:15:50 +00:00
{
return *p;
}
2020-05-02 21:18:43 +00:00
reference operator*()
2015-04-26 20:15:50 +00:00
{
return *p;
}
null_sentinel_iterator &operator++()
{
++p;
return *this;
}
bool operator==(null_sentinel_iterator rhs) const
{
if (rhs.p)
return p == rhs.p;
if (!p)
/* Should never happen */
return true;
return !**this;
}
bool operator!=(null_sentinel_iterator rhs) const
{
return !(*this == rhs);
}
2020-05-02 21:18:43 +00:00
private:
pointer p = nullptr;
2015-04-26 20:15:50 +00:00
};
2015-07-18 21:01:55 +00:00
template <typename I>
class null_sentinel_array
{
typedef null_sentinel_iterator<I> iterator;
I *b;
public:
null_sentinel_array(I *i) :
b(i)
{
}
iterator begin() const
{
return b;
}
iterator end() const
{
return {};
}
};
template <typename I>
static inline null_sentinel_array<I> make_null_sentinel_array(I *i)
{
return i;
}