michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_Range_h michael@0: #define mozilla_Range_h michael@0: michael@0: #include "mozilla/NullPtr.h" michael@0: #include "mozilla/RangedPtr.h" michael@0: michael@0: #include michael@0: michael@0: namespace mozilla { michael@0: michael@0: // Range is a tuple containing a pointer and a length. michael@0: template michael@0: class Range michael@0: { michael@0: RangedPtr mStart; michael@0: RangedPtr mEnd; michael@0: michael@0: typedef void (Range::* ConvertibleToBool)(); michael@0: void nonNull() {} michael@0: michael@0: public: michael@0: Range() : mStart(nullptr, 0), mEnd(nullptr, 0) {} michael@0: Range(T* p, size_t len) michael@0: : mStart(p, p, p + len), michael@0: mEnd(p + len, p, p + len) michael@0: {} michael@0: michael@0: RangedPtr start() const { return mStart; } michael@0: RangedPtr end() const { return mEnd; } michael@0: size_t length() const { return mEnd - mStart; } michael@0: michael@0: T& operator[](size_t offset) { michael@0: return mStart[offset]; michael@0: } michael@0: michael@0: const T& operator[](size_t offset) const { michael@0: return mStart[offset]; michael@0: } michael@0: michael@0: operator ConvertibleToBool() const { return mStart ? &Range::nonNull : 0; } michael@0: }; michael@0: michael@0: } // namespace mozilla michael@0: michael@0: #endif /* mozilla_Range_h */