Skip to content

Commit

Permalink
Bug 1451722 - Constexpr-ify mozilla::Span more. r=hsivonen
Browse files Browse the repository at this point in the history
MozReview-Commit-ID: B7sxH5BAdp6
  • Loading branch information
vyv03354 committed Apr 5, 2018
1 parent 8a6934c commit 951d1d0
Showing 1 changed file with 14 additions and 37 deletions.
51 changes: 14 additions & 37 deletions mfbt/Span.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,6 @@
#include <cstring>
#include <iterator>

// Classifications for reasons why constexpr was removed in C++14 to C++11
// conversion. Once we upgrade compilers, we can try defining each of these
// to constexpr to restore a category of constexprs at a time.
#if !defined(__clang__) && defined(__GNUC__) && __cpp_constexpr < 201304
#define MOZ_SPAN_ASSERTION_CONSTEXPR
#define MOZ_SPAN_GCC_CONSTEXPR
#define MOZ_SPAN_EXPLICITLY_DEFAULTED_CONSTEXPR
#define MOZ_SPAN_CONSTEXPR_NOT_JUST_RETURN
#define MOZ_SPAN_NON_CONST_CONSTEXPR
#else
#define MOZ_SPAN_ASSERTION_CONSTEXPR constexpr
#define MOZ_SPAN_GCC_CONSTEXPR constexpr
#define MOZ_SPAN_EXPLICITLY_DEFAULTED_CONSTEXPR constexpr
#define MOZ_SPAN_CONSTEXPR_NOT_JUST_RETURN constexpr
#define MOZ_SPAN_NON_CONST_CONSTEXPR constexpr
#endif

#ifdef _MSC_VER
#pragma warning(push)

Expand Down Expand Up @@ -175,7 +158,7 @@ class span_iterator

constexpr span_iterator() : span_iterator(nullptr, 0) {}

MOZ_SPAN_ASSERTION_CONSTEXPR span_iterator(const Span* span,
constexpr span_iterator(const Span* span,
typename Span::index_type index)
: span_(span)
, index_(index)
Expand All @@ -190,10 +173,10 @@ class span_iterator
{
}

MOZ_SPAN_EXPLICITLY_DEFAULTED_CONSTEXPR span_iterator<Span, IsConst>&
constexpr span_iterator<Span, IsConst>&
operator=(const span_iterator<Span, IsConst>&) = default;

MOZ_SPAN_GCC_CONSTEXPR reference operator*() const
constexpr reference operator*() const
{
MOZ_RELEASE_ASSERT(span_);
return (*span_)[index_];
Expand All @@ -205,7 +188,7 @@ class span_iterator
return &((*span_)[index_]);
}

MOZ_SPAN_NON_CONST_CONSTEXPR span_iterator& operator++()
constexpr span_iterator& operator++()
{
MOZ_RELEASE_ASSERT(span_ && index_ >= 0 && index_ < span_->Length());
++index_;
Expand All @@ -219,7 +202,7 @@ class span_iterator
return ret;
}

MOZ_SPAN_NON_CONST_CONSTEXPR span_iterator& operator--()
constexpr span_iterator& operator--()
{
MOZ_RELEASE_ASSERT(span_ && index_ > 0 && index_ <= span_->Length());
--index_;
Expand All @@ -233,14 +216,14 @@ class span_iterator
return ret;
}

MOZ_SPAN_CONSTEXPR_NOT_JUST_RETURN span_iterator
constexpr span_iterator
operator+(difference_type n) const
{
auto ret = *this;
return ret += n;
}

MOZ_SPAN_GCC_CONSTEXPR span_iterator& operator+=(difference_type n)
constexpr span_iterator& operator+=(difference_type n)
{
MOZ_RELEASE_ASSERT(span_ && (index_ + n) >= 0 &&
(index_ + n) <= span_->Length());
Expand All @@ -261,7 +244,7 @@ class span_iterator
return *this += -n;
}

MOZ_SPAN_GCC_CONSTEXPR difference_type
constexpr difference_type
operator-(const span_iterator& rhs) const
{
MOZ_RELEASE_ASSERT(span_ == rhs.span_);
Expand All @@ -285,7 +268,7 @@ class span_iterator
return !(lhs == rhs);
}

MOZ_SPAN_GCC_CONSTEXPR friend bool operator<(const span_iterator& lhs,
constexpr friend bool operator<(const span_iterator& lhs,
const span_iterator& rhs)
{
MOZ_RELEASE_ASSERT(lhs.span_ == rhs.span_);
Expand Down Expand Up @@ -340,15 +323,15 @@ class extent_type
constexpr extent_type() {}

template<index_type Other>
MOZ_SPAN_ASSERTION_CONSTEXPR MOZ_IMPLICIT extent_type(extent_type<Other> ext)
constexpr MOZ_IMPLICIT extent_type(extent_type<Other> ext)
{
static_assert(
Other == Ext || Other == dynamic_extent,
"Mismatch between fixed-size extent and size of initializing data.");
MOZ_RELEASE_ASSERT(ext.size() == Ext);
}

MOZ_SPAN_ASSERTION_CONSTEXPR MOZ_IMPLICIT extent_type(index_type length)
constexpr MOZ_IMPLICIT extent_type(index_type length)
{
MOZ_RELEASE_ASSERT(length == Ext);
}
Expand Down Expand Up @@ -646,10 +629,10 @@ class Span
}

~Span() = default;
MOZ_SPAN_EXPLICITLY_DEFAULTED_CONSTEXPR Span& operator=(const Span& other)
constexpr Span& operator=(const Span& other)
= default;

MOZ_SPAN_EXPLICITLY_DEFAULTED_CONSTEXPR Span& operator=(Span&& other)
constexpr Span& operator=(Span&& other)
= default;

// [Span.sub], Span subviews
Expand Down Expand Up @@ -853,7 +836,7 @@ class Span
{
public:
template<class OtherExtentType>
MOZ_SPAN_ASSERTION_CONSTEXPR storage_type(pointer elements,
constexpr storage_type(pointer elements,
OtherExtentType ext)
: ExtentType(ext)
// Replace nullptr with 0x1 for Rust slice compatibility. See
Expand Down Expand Up @@ -1099,10 +1082,4 @@ MakeStringSpan(const char16_t* aZeroTerminated)
#pragma warning(pop)
#endif // _MSC_VER

#undef MOZ_SPAN_ASSERTION_CONSTEXPR
#undef MOZ_SPAN_GCC_CONSTEXPR
#undef MOZ_SPAN_EXPLICITLY_DEFAULTED_CONSTEXPR
#undef MOZ_SPAN_CONSTEXPR_NOT_JUST_RETURN
#undef MOZ_SPAN_NON_CONST_CONSTEXPR

#endif // mozilla_Span_h

0 comments on commit 951d1d0

Please sign in to comment.