Bug 917642 - [Helix] Please update the helix blobs. r=nhirata
[gecko.git] / mfbt / Range.h
blob4e02d962b5f9216d4b5a673d0bb2fdcec4a5d72f
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/NullPtr.h"
11 #include "mozilla/RangedPtr.h"
13 #include <stddef.h>
15 namespace mozilla {
17 // Range<T> is a tuple containing a pointer and a length.
18 template <typename T>
19 class Range
21 RangedPtr<T> mStart;
22 RangedPtr<T> mEnd;
24 typedef void (Range::* ConvertibleToBool)();
25 void nonNull() {}
27 public:
28 Range() : mStart(nullptr, 0), mEnd(nullptr, 0) {}
29 Range(T* p, size_t len)
30 : mStart(p, p, p + len),
31 mEnd(p + len, p, p + len)
34 RangedPtr<T> start() const { return mStart; }
35 RangedPtr<T> end() const { return mEnd; }
36 size_t length() const { return mEnd - mStart; }
38 T& operator[](size_t offset) {
39 return mStart[offset];
42 const T& operator[](size_t offset) const {
43 return mStart[offset];
46 operator ConvertibleToBool() const { return mStart ? &Range::nonNull : 0; }
49 } // namespace mozilla
51 #endif /* mozilla_Range_h */