mfbt/Range.h
author J. Ryan Stinnett <jryans@gmail.com>
Tue, 28 Jun 2016 17:57:24 -0500
changeset 303370 9b0be364ac5445e5712e79651720ad70269ca9b9
parent 227292 de42116d5ef3aa8a26c1a601675786faa9bb5179
child 304615 3695acc329e68a33ad30063f97b9ea5b148faa88
permissions -rw-r--r--
Bug 1283583 - Clean up code style in devtools/server/child.js. r=ochameau MozReview-Commit-ID: GoHv6jvX6M1

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_Range_h
#define mozilla_Range_h

#include "mozilla/RangedPtr.h"

#include <stddef.h>

namespace mozilla {

// Range<T> is a tuple containing a pointer and a length.
template <typename T>
class Range
{
  const RangedPtr<T> mStart;
  const RangedPtr<T> mEnd;

public:
  Range() : mStart(nullptr, 0), mEnd(nullptr, 0) {}
  Range(T* aPtr, size_t aLength)
    : mStart(aPtr, aPtr, aPtr + aLength),
      mEnd(aPtr + aLength, aPtr, aPtr + aLength)
  {}

  RangedPtr<T> start() const { return mStart; }
  RangedPtr<T> end() const { return mEnd; }
  size_t length() const { return mEnd - mStart; }

  T& operator[](size_t aOffset) const { return mStart[aOffset]; }

  explicit operator bool() const { return mStart != nullptr; }
};

} // namespace mozilla

#endif /* mozilla_Range_h */