Skip to content

Commit

Permalink
Make all() return a bare range
Browse files Browse the repository at this point in the history
Changed my mind since the last commit :) . I don't intend for
enum_range to remain compatible with STL containers, as the view
concepts from Ranges STL likely to be standardized in the near future
are a better match for the intended use.
  • Loading branch information
jasujm committed Dec 11, 2019
1 parent 38f2057 commit f0c47bd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
25 changes: 7 additions & 18 deletions cxx/include/enhanced_enum/details/ranges.hh
Original file line number Diff line number Diff line change
Expand Up @@ -146,30 +146,19 @@ constexpr enum_iterator<EnhancedEnum> operator-(
template<typename EnhancedEnum>
struct enum_range
{
using value_type = EnhancedEnum;
using reference = const EnhancedEnum&;
using const_reference = const EnhancedEnum&;
using iterator = enum_iterator<EnhancedEnum>;
using const_iterator = enum_iterator<EnhancedEnum>;
using difference_type = std::ptrdiff_t;
using size_type = std::size_t;

constexpr enum_range(iterator first, iterator last) noexcept :
constexpr enum_range(
enum_iterator<EnhancedEnum> first,
enum_iterator<EnhancedEnum> last) noexcept :
first {first},
last {last}
{};

constexpr iterator begin() const noexcept { return first; }
constexpr iterator end() const noexcept { return last; }

constexpr size_type size() const noexcept
{
return static_cast<size_type>(last - first);
}
constexpr auto begin() const noexcept { return first; }
constexpr auto end() const noexcept { return last; }

private:
iterator first;
iterator last;
enum_iterator<EnhancedEnum> first;
enum_iterator<EnhancedEnum> last;
};

}
Expand Down
1 change: 0 additions & 1 deletion cxx/tests/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ static_assert( StatusLabel::BUSY >= enhance(StatusLabel::BUSY) );

static_assert( std::distance(EnhancedStatus::begin(), EnhancedStatus::end()) == 3 );
static_assert( std::distance(EnhancedStatus::end(), EnhancedStatus::begin()) == -3 );
static_assert( EnhancedStatus::all().size() == 3u );
static_assert( EnhancedStatus::begin() == EnhancedStatus::begin()++ );
static_assert( EnhancedStatus::begin() != ++EnhancedStatus::begin() );
static_assert( EnhancedStatus::begin() < EnhancedStatus::end() );
Expand Down
16 changes: 13 additions & 3 deletions docs/enhancedenumlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,19 @@ For interfaces consuming iterator pairs, using :cpp:func:`begin()` and
EnhancedStatus::begin(), EnhancedStatus::end(),
[](const auto status) { /* use status */ });
The user should not assume an underlying type returned by the
:cpp:func:`all()`, :cpp:func:`begin()` and :cpp:func:`end()`
functions, except that the iterators supports random access.
.. note::

The user should not assume an underlying type returned by the
:cpp:func:`all()`, :cpp:func:`begin()` and :cpp:func:`end()`
functions, except that the iterators supports random access.

The iterators model the C++17 random access iterator concepts. The
range returned by :cpp:func:`all()` *doesn't* model STL
container. The intention is to remain forward-compatible with the
view concepts from the `Ranges TS
<http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0896r3.pdf>`_. Unlike
STL containers, views don't define type aliases etc. The other
functions in the view interface should be implemented later.

Library reference
-----------------
Expand Down

0 comments on commit f0c47bd

Please sign in to comment.