Bug 1535487 - determine rootUrl directly in buglist creator r=tomprince
[gecko.git] / mfbt / Array.h
blob5ae502b12aff419a5aebe2c77682302de5d6de9a
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* A compile-time constant-length array with bounds-checking assertions. */
9 #ifndef mozilla_Array_h
10 #define mozilla_Array_h
12 #include "mozilla/Assertions.h"
13 #include "mozilla/Attributes.h"
14 #include "mozilla/Move.h"
15 #include "mozilla/ReverseIterator.h"
17 #include <stddef.h>
19 namespace mozilla {
21 template <typename T, size_t Length>
22 class Array {
23 T mArr[Length];
25 public:
26 Array() {}
28 template <typename... Args>
29 MOZ_IMPLICIT constexpr Array(Args&&... aArgs)
30 : mArr{std::forward<Args>(aArgs)...} {
31 static_assert(sizeof...(aArgs) == Length,
32 "The number of arguments should be equal to the template "
33 "parameter Length");
36 T& operator[](size_t aIndex) {
37 MOZ_ASSERT(aIndex < Length);
38 return mArr[aIndex];
41 const T& operator[](size_t aIndex) const {
42 MOZ_ASSERT(aIndex < Length);
43 return mArr[aIndex];
46 bool operator==(const Array<T, Length>& aOther) const {
47 for (size_t i = 0; i < Length; i++) {
48 if (mArr[i] != aOther[i]) {
49 return false;
52 return true;
55 typedef T* iterator;
56 typedef const T* const_iterator;
57 typedef ReverseIterator<T*> reverse_iterator;
58 typedef ReverseIterator<const T*> const_reverse_iterator;
60 // Methods for range-based for loops.
61 iterator begin() { return mArr; }
62 const_iterator begin() const { return mArr; }
63 const_iterator cbegin() const { return begin(); }
64 iterator end() { return mArr + Length; }
65 const_iterator end() const { return mArr + Length; }
66 const_iterator cend() const { return end(); }
68 // Methods for reverse iterating.
69 reverse_iterator rbegin() { return reverse_iterator(end()); }
70 const_reverse_iterator rbegin() const {
71 return const_reverse_iterator(end());
73 const_reverse_iterator crbegin() const { return rbegin(); }
74 reverse_iterator rend() { return reverse_iterator(begin()); }
75 const_reverse_iterator rend() const {
76 return const_reverse_iterator(begin());
78 const_reverse_iterator crend() const { return rend(); }
81 template <typename T>
82 class Array<T, 0> {
83 public:
84 T& operator[](size_t aIndex) { MOZ_CRASH("indexing into zero-length array"); }
86 const T& operator[](size_t aIndex) const {
87 MOZ_CRASH("indexing into zero-length array");
91 } /* namespace mozilla */
93 #endif /* mozilla_Array_h */