Skip to content

Commit

Permalink
Bug 1424103 - constexpr-ify mozilla::Span. r=hsivonen
Browse files Browse the repository at this point in the history
MozReview-Commit-ID: 4e98Aer9V1k

--HG--
extra : rebase_source : 44f87677a4918aea4abaf2721895e48045267c7b
  • Loading branch information
vyv03354 committed Dec 8, 2017
1 parent a214244 commit 4560984
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions mfbt/Span.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,19 @@
// 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 @@ -191,7 +199,7 @@ class span_iterator
return (*span_)[index_];
}

MOZ_SPAN_GCC_CONSTEXPR pointer operator->() const
constexpr pointer operator->() const
{
MOZ_RELEASE_ASSERT(span_);
return &((*span_)[index_]);
Expand All @@ -204,7 +212,7 @@ class span_iterator
return *this;
}

MOZ_SPAN_NON_CONST_CONSTEXPR span_iterator operator++(int)
constexpr span_iterator operator++(int)
{
auto ret = *this;
++(*this);
Expand All @@ -218,7 +226,7 @@ class span_iterator
return *this;
}

MOZ_SPAN_NON_CONST_CONSTEXPR span_iterator operator--(int)
constexpr span_iterator operator--(int)
{
auto ret = *this;
--(*this);
Expand All @@ -240,14 +248,14 @@ class span_iterator
return *this;
}

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

MOZ_SPAN_NON_CONST_CONSTEXPR span_iterator& operator-=(difference_type n)
constexpr span_iterator& operator-=(difference_type n)

{
return *this += -n;
Expand Down Expand Up @@ -284,19 +292,19 @@ class span_iterator
return lhs.index_ < rhs.index_;
}

MOZ_SPAN_GCC_CONSTEXPR friend bool operator<=(const span_iterator& lhs,
constexpr friend bool operator<=(const span_iterator& lhs,
const span_iterator& rhs)
{
return !(rhs < lhs);
}

MOZ_SPAN_GCC_CONSTEXPR friend bool operator>(const span_iterator& lhs,
constexpr friend bool operator>(const span_iterator& lhs,
const span_iterator& rhs)
{
return rhs < lhs;
}

MOZ_SPAN_GCC_CONSTEXPR friend bool operator>=(const span_iterator& lhs,
constexpr friend bool operator>=(const span_iterator& lhs,
const span_iterator& rhs)
{
return !(rhs > lhs);
Expand Down Expand Up @@ -648,7 +656,7 @@ class Span
* Subspan with first N elements with compile-time N.
*/
template<size_t Count>
MOZ_SPAN_GCC_CONSTEXPR Span<element_type, Count> First() const
constexpr Span<element_type, Count> First() const
{
MOZ_RELEASE_ASSERT(Count <= size());
return { data(), Count };
Expand All @@ -658,7 +666,7 @@ class Span
* Subspan with last N elements with compile-time N.
*/
template<size_t Count>
MOZ_SPAN_GCC_CONSTEXPR Span<element_type, Count> Last() const
constexpr Span<element_type, Count> Last() const
{
const size_t len = size();
MOZ_RELEASE_ASSERT(Count <= len);
Expand All @@ -669,7 +677,7 @@ class Span
* Subspan with compile-time start index and length.
*/
template<size_t Offset, size_t Count = dynamic_extent>
MOZ_SPAN_GCC_CONSTEXPR Span<element_type, Count> Subspan() const
constexpr Span<element_type, Count> Subspan() const
{
const size_t len = size();
MOZ_RELEASE_ASSERT(Offset <= len &&
Expand All @@ -681,7 +689,7 @@ class Span
/**
* Subspan with first N elements with run-time N.
*/
MOZ_SPAN_GCC_CONSTEXPR Span<element_type, dynamic_extent> First(
constexpr Span<element_type, dynamic_extent> First(
index_type aCount) const
{
MOZ_RELEASE_ASSERT(aCount <= size());
Expand All @@ -691,7 +699,7 @@ class Span
/**
* Subspan with last N elements with run-time N.
*/
MOZ_SPAN_GCC_CONSTEXPR Span<element_type, dynamic_extent> Last(
constexpr Span<element_type, dynamic_extent> Last(
index_type aCount) const
{
const size_t len = size();
Expand All @@ -702,7 +710,7 @@ class Span
/**
* Subspan with run-time start index and length.
*/
MOZ_SPAN_GCC_CONSTEXPR Span<element_type, dynamic_extent> Subspan(
constexpr Span<element_type, dynamic_extent> Subspan(
index_type aStart,
index_type aLength = dynamic_extent) const
{
Expand All @@ -717,7 +725,7 @@ class Span
/**
* Subspan with run-time start index. (Rust's &foo[start..])
*/
MOZ_SPAN_GCC_CONSTEXPR Span<element_type, dynamic_extent> From(
constexpr Span<element_type, dynamic_extent> From(
index_type aStart) const
{
return Subspan(aStart);
Expand All @@ -726,7 +734,7 @@ class Span
/**
* Subspan with run-time exclusive end index. (Rust's &foo[..end])
*/
MOZ_SPAN_GCC_CONSTEXPR Span<element_type, dynamic_extent> To(
constexpr Span<element_type, dynamic_extent> To(
index_type aEnd) const
{
return Subspan(0, aEnd);
Expand All @@ -736,7 +744,7 @@ class Span
* Subspan with run-time start index and exclusive end index.
* (Rust's &foo[start..end])
*/
MOZ_SPAN_GCC_CONSTEXPR Span<element_type, dynamic_extent> FromTo(
constexpr Span<element_type, dynamic_extent> FromTo(
index_type aStart,
index_type aEnd) const
{
Expand Down Expand Up @@ -780,7 +788,7 @@ class Span
constexpr bool empty() const { return size() == 0; }

// [Span.elem], Span element access
MOZ_SPAN_GCC_CONSTEXPR reference operator[](index_type idx) const
constexpr reference operator[](index_type idx) const
{
MOZ_RELEASE_ASSERT(idx < storage_.size());
return data()[idx];
Expand Down

0 comments on commit 4560984

Please sign in to comment.