Bug 1456906 [wpt PR 10641] - Subtract scrollbar in PerpendicularContainingBlockLogica...
[gecko.git] / mfbt / Range.h
blob753fe07f897e4a37f0833a877903e267115662ba
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_Range_h
8 #define mozilla_Range_h
10 #include "mozilla/RangedPtr.h"
11 #include "mozilla/TypeTraits.h"
12 #include "mozilla/Span.h"
14 #include <stddef.h>
16 namespace mozilla {
18 // Range<T> is a tuple containing a pointer and a length.
19 template <typename T>
20 class Range
22 const RangedPtr<T> mStart;
23 const RangedPtr<T> mEnd;
25 public:
26 Range() : mStart(nullptr, 0), mEnd(nullptr, 0) {}
27 Range(T* aPtr, size_t aLength)
28 : mStart(aPtr, aPtr, aPtr + aLength),
29 mEnd(aPtr + aLength, aPtr, aPtr + aLength)
31 Range(const RangedPtr<T>& aStart, const RangedPtr<T>& aEnd)
32 : mStart(aStart.get(), aStart.get(), aEnd.get()),
33 mEnd(aEnd.get(), aStart.get(), aEnd.get())
35 // Only accept two RangedPtrs within the same range.
36 aStart.checkIdenticalRange(aEnd);
37 MOZ_ASSERT(aStart <= aEnd);
40 template<typename U,
41 class = typename EnableIf<IsConvertible<U (*)[], T (*)[]>::value,
42 int>::Type>
43 MOZ_IMPLICIT Range(const Range<U>& aOther)
44 : mStart(aOther.mStart),
45 mEnd(aOther.mEnd)
48 MOZ_IMPLICIT Range(Span<T> aSpan)
49 : Range(aSpan.Elements(), aSpan.Length())
53 template<typename U,
54 class = typename EnableIf<IsConvertible<U (*)[], T (*)[]>::value,
55 int>::Type>
56 MOZ_IMPLICIT Range(const Span<U>& aSpan)
57 : Range(aSpan.Elements(), aSpan.Length())
61 RangedPtr<T> begin() const { return mStart; }
62 RangedPtr<T> end() const { return mEnd; }
63 size_t length() const { return mEnd - mStart; }
65 T& operator[](size_t aOffset) const { return mStart[aOffset]; }
67 explicit operator bool() const { return mStart != nullptr; }
69 operator Span<T>() { return Span<T>(mStart.get(), length()); }
71 operator Span<const T>() const { return Span<T>(mStart.get(), length()); }
74 template<class T>
75 Span<T>
76 MakeSpan(Range<T>& aRange)
78 return aRange;
81 template<class T>
82 Span<const T>
83 MakeSpan(const Range<T>& aRange)
85 return aRange;
88 } // namespace mozilla
90 #endif /* mozilla_Range_h */