mfbt/Range.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mfbt/Range.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,51 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef mozilla_Range_h
    1.11 +#define mozilla_Range_h
    1.12 +
    1.13 +#include "mozilla/NullPtr.h"
    1.14 +#include "mozilla/RangedPtr.h"
    1.15 +
    1.16 +#include <stddef.h>
    1.17 +
    1.18 +namespace mozilla {
    1.19 +
    1.20 +// Range<T> is a tuple containing a pointer and a length.
    1.21 +template <typename T>
    1.22 +class Range
    1.23 +{
    1.24 +    RangedPtr<T> mStart;
    1.25 +    RangedPtr<T> mEnd;
    1.26 +
    1.27 +    typedef void (Range::* ConvertibleToBool)();
    1.28 +    void nonNull() {}
    1.29 +
    1.30 +  public:
    1.31 +    Range() : mStart(nullptr, 0), mEnd(nullptr, 0) {}
    1.32 +    Range(T* p, size_t len)
    1.33 +      : mStart(p, p, p + len),
    1.34 +        mEnd(p + len, p, p + len)
    1.35 +    {}
    1.36 +
    1.37 +    RangedPtr<T> start() const { return mStart; }
    1.38 +    RangedPtr<T> end() const { return mEnd; }
    1.39 +    size_t length() const { return mEnd - mStart; }
    1.40 +
    1.41 +    T& operator[](size_t offset) {
    1.42 +      return mStart[offset];
    1.43 +    }
    1.44 +
    1.45 +    const T& operator[](size_t offset) const {
    1.46 +      return mStart[offset];
    1.47 +    }
    1.48 +
    1.49 +    operator ConvertibleToBool() const { return mStart ? &Range::nonNull : 0; }
    1.50 +};
    1.51 +
    1.52 +} // namespace mozilla
    1.53 +
    1.54 +#endif /* mozilla_Range_h */

mercurial