mfbt/Array.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mfbt/Array.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 +/* A compile-time constant-length array with bounds-checking assertions. */
    1.11 +
    1.12 +#ifndef mozilla_Array_h
    1.13 +#define mozilla_Array_h
    1.14 +
    1.15 +#include "mozilla/Assertions.h"
    1.16 +#include "mozilla/Attributes.h"
    1.17 +
    1.18 +#include <stddef.h>
    1.19 +
    1.20 +namespace mozilla {
    1.21 +
    1.22 +template<typename T, size_t Length>
    1.23 +class Array
    1.24 +{
    1.25 +    T arr[Length];
    1.26 +
    1.27 +  public:
    1.28 +    T& operator[](size_t i) {
    1.29 +      MOZ_ASSERT(i < Length);
    1.30 +      return arr[i];
    1.31 +    }
    1.32 +
    1.33 +    const T& operator[](size_t i) const {
    1.34 +      MOZ_ASSERT(i < Length);
    1.35 +      return arr[i];
    1.36 +    }
    1.37 +};
    1.38 +
    1.39 +template<typename T>
    1.40 +class Array<T, 0>
    1.41 +{
    1.42 +  public:
    1.43 +    T& operator[](size_t i) {
    1.44 +      MOZ_ASSUME_UNREACHABLE("indexing into zero-length array");
    1.45 +    }
    1.46 +
    1.47 +    const T& operator[](size_t i) const {
    1.48 +      MOZ_ASSUME_UNREACHABLE("indexing into zero-length array");
    1.49 +    }
    1.50 +};
    1.51 +
    1.52 +}  /* namespace mozilla */
    1.53 +
    1.54 +#endif /* mozilla_Array_h */

mercurial