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"
18 // Range<T> is a tuple containing a pointer and a length.
22 const RangedPtr
<T
> mStart
;
23 const RangedPtr
<T
> mEnd
;
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
);
41 class = typename EnableIf
<IsConvertible
<U (*)[], T (*)[]>::value
,
43 MOZ_IMPLICIT
Range(const Range
<U
>& aOther
)
44 : mStart(aOther
.mStart
),
48 MOZ_IMPLICIT
Range(Span
<T
> aSpan
)
49 : Range(aSpan
.Elements(), aSpan
.Length())
54 class = typename EnableIf
<IsConvertible
<U (*)[], T (*)[]>::value
,
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()); }
76 MakeSpan(Range
<T
>& aRange
)
83 MakeSpan(const Range
<T
>& aRange
)
88 } // namespace mozilla
90 #endif /* mozilla_Range_h */