Skip to content

Commit

Permalink
Add typedefs and size() function to enum_range
Browse files Browse the repository at this point in the history
This makes enum_range partially compatible with the STL container
requirements.
  • Loading branch information
jasujm committed Dec 10, 2019
1 parent 555a331 commit 38f2057
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cxx/include/enhanced_enum/details/ranges.hh
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,13 @@ 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 :
first {first},
Expand All @@ -156,6 +162,11 @@ struct enum_range
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);
}

private:
iterator first;
iterator last;
Expand Down
1 change: 1 addition & 0 deletions cxx/tests/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ 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

0 comments on commit 38f2057

Please sign in to comment.