Skip to content

Commit

Permalink
Bug 1295611 - Add mozilla::Span. r=froydnj,gerv.
Browse files Browse the repository at this point in the history
MozReview-Commit-ID: HGNDClVctbE
  • Loading branch information
hsivonen committed Mar 31, 2017
1 parent aa83288 commit 8f4b0cb
Show file tree
Hide file tree
Showing 10 changed files with 3,386 additions and 0 deletions.
13 changes: 13 additions & 0 deletions mfbt/Casting.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,19 @@ AssertedCast(const From aFrom)
return static_cast<To>(aFrom);
}

/**
* Cast a value of integral type |From| to a value of integral type |To|,
* release asserting that the cast will be a safe cast per C++ (that is, that
* |to| is in the range of values permitted for the type |From|).
*/
template<typename To, typename From>
inline To
ReleaseAssertedCast(const From aFrom)
{
MOZ_RELEASE_ASSERT((detail::IsInBounds<From, To>(aFrom)));
return static_cast<To>(aFrom);
}

} // namespace mozilla

#endif /* mozilla_Casting_h */
32 changes: 32 additions & 0 deletions mfbt/Range.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "mozilla/RangedPtr.h"
#include "mozilla/TypeTraits.h"
#include "mozilla/Span.h"

#include <stddef.h>

Expand Down Expand Up @@ -44,15 +45,46 @@ class Range
mEnd(aOther.mEnd)
{}

MOZ_IMPLICIT Range(Span<T> aSpan)
: Range(aSpan.Elements(), aSpan.Length())
{
}

template<typename U,
class = typename EnableIf<IsConvertible<U (*)[], T (*)[]>::value,
int>::Type>
MOZ_IMPLICIT Range(const Span<U>& aSpan)
: Range(aSpan.Elements(), aSpan.Length())
{
}

RangedPtr<T> begin() const { return mStart; }
RangedPtr<T> end() const { return mEnd; }
size_t length() const { return mEnd - mStart; }

T& operator[](size_t aOffset) const { return mStart[aOffset]; }

explicit operator bool() const { return mStart != nullptr; }

operator Span<T>() { return Span<T>(mStart.get(), length()); }

operator Span<const T>() const { return Span<T>(mStart.get(), length()); }
};

template<class T>
Span<T>
MakeSpan(Range<T>& aRange)
{
return aRange;
}

template<class T>
Span<const T>
MakeSpan(const Range<T>& aRange)
{
return aRange;
}

} // namespace mozilla

#endif /* mozilla_Range_h */
Loading

0 comments on commit 8f4b0cb

Please sign in to comment.