Bug 835381 - Unit tests for the MediaSniffer to make sure Matroska files are not...
[gecko.git] / mfbt / Range.h
blobe14594d09d7d75f3e0540f486fe362377dc595f5
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sw=4 et tw=78:
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #ifndef mozilla_Range_h_
9 #define mozilla_Range_h_
11 #include "mozilla/NullPtr.h"
12 #include "mozilla/RangedPtr.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 RangedPtr<T> mStart;
23 RangedPtr<T> mEnd;
25 typedef void (Range::* ConvertibleToBool)();
26 void nonNull() {}
28 public:
29 Range() : mStart(nullptr, 0), mEnd(nullptr, 0) {}
30 Range(T* p, size_t len)
31 : mStart(p, p, p + len),
32 mEnd(p + len, p, p + len)
35 RangedPtr<T> start() const { return mStart; }
36 RangedPtr<T> end() const { return mEnd; }
37 size_t length() const { return mEnd - mStart; }
39 T& operator[](size_t offset) {
40 return mStart[offset];
43 operator ConvertibleToBool() const { return mStart ? &Range::nonNull : 0; }
46 } // namespace mozilla
48 #endif // mozilla_Range_h_