mfbt/Range.h

branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
equal deleted inserted replaced
-1:000000000000 0:41bc24cebccb
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/. */
6
7 #ifndef mozilla_Range_h
8 #define mozilla_Range_h
9
10 #include "mozilla/NullPtr.h"
11 #include "mozilla/RangedPtr.h"
12
13 #include <stddef.h>
14
15 namespace mozilla {
16
17 // Range<T> is a tuple containing a pointer and a length.
18 template <typename T>
19 class Range
20 {
21 RangedPtr<T> mStart;
22 RangedPtr<T> mEnd;
23
24 typedef void (Range::* ConvertibleToBool)();
25 void nonNull() {}
26
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)
32 {}
33
34 RangedPtr<T> start() const { return mStart; }
35 RangedPtr<T> end() const { return mEnd; }
36 size_t length() const { return mEnd - mStart; }
37
38 T& operator[](size_t offset) {
39 return mStart[offset];
40 }
41
42 const T& operator[](size_t offset) const {
43 return mStart[offset];
44 }
45
46 operator ConvertibleToBool() const { return mStart ? &Range::nonNull : 0; }
47 };
48
49 } // namespace mozilla
50
51 #endif /* mozilla_Range_h */

mercurial