Skip to content

Commit

Permalink
Add enum_base::ssize()
Browse files Browse the repository at this point in the history
- Add method to query the number of enumerators as sized integer

- Add documentation and tests for both size() and ssize()
  • Loading branch information
jasujm committed Dec 7, 2019
1 parent eae5765 commit 50b7208
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ They can be iterated:

.. code-block:: c++

std::cout << "Listing " << Status::size() << " enumerators:\n";
for (const auto status : Status::all()) {
std::cout << status.value() << "\n";
}
Expand Down
9 changes: 9 additions & 0 deletions cxx/include/enhanced_enum/enhanced_enum.hh
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ struct enum_base {
return EnhancedEnum::values.size();
}

/** \brief Get the number of enumerators as signed number
*
* \return The number of enumerators in this enum
*/
static constexpr std::ptrdiff_t ssize() noexcept
{
return static_cast<std::ptrdiff_t>(size());
}

/** \brief Get range over all enumerators
*
* \return A random accessible range containing all enumerators in
Expand Down
4 changes: 3 additions & 1 deletion cxx/tests/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ static_assert( enhanced_enum::is_label_enum_v<StatusLabel> );
static_assert( std::is_same_v<enhanced_enum::make_enhanced_t<StatusLabel>, EnhancedStatus> );
static_assert( std::is_same_v<enhanced_enum::make_enhanced_t<EnhancedStatus>, EnhancedStatus> );

// enhance(), .get() and .value(), .from() are constexpr
// Basic functions

static_assert( EnhancedStatus::size() == 3u );
static_assert( EnhancedStatus::ssize() == 3 );
static_assert( enhance(StatusLabel::BUSY).get() == StatusLabel::BUSY );
static_assert( enhance(StatusLabel::BUSY).value() == Statuses::BUSY_VALUE );
static_assert( EnhancedStatus::from(Statuses::BUSY_VALUE) == Statuses::BUSY );
Expand Down
8 changes: 6 additions & 2 deletions docs/enhancedenumlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,12 @@ deliberately explicit.
static_assert( Statuses::INITIALIZING.get() == StatusLabel::INITIALIZING )
static_assert( static_cast<StatusLabel>(Statuses::INITIALIZING) == StatusLabel::INITIALIZING );

Iterating over enumerators
..........................
Enumerators as range
....................

The number of enumerators in an enhanced enum type can be queries by
using the :cpp:func:`size()` and :cpp:func:`ssize()`, for unsigned and
signed sizes, respectively.

A range containing all enumerators of a given enum type can be
constructed with the static :cpp:func:`all()` method:
Expand Down

0 comments on commit 50b7208

Please sign in to comment.