Bug 1885489 - Part 9: Add SnapshotIterator::readObject(). r=iain
[gecko.git] / mfbt / Array.h
blob55b724a2882d177255a80d97cd071482ac8fe2fa
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 <stddef.h>
14 #include <iterator>
15 #include <ostream>
16 #include <utility>
18 #include "mozilla/Assertions.h"
19 #include "mozilla/Attributes.h"
20 #include "mozilla/Likely.h"
22 namespace mozilla {
24 template <typename T, size_t _Length>
25 class Array {
26 T mArr[_Length];
28 public:
29 using ElementType = T;
30 static constexpr size_t Length = _Length;
32 constexpr Array() = default;
34 template <typename... Args>
35 MOZ_IMPLICIT constexpr Array(Args&&... aArgs)
36 : mArr{std::forward<Args>(aArgs)...} {
37 static_assert(sizeof...(aArgs) == Length,
38 "The number of arguments should be equal to the template "
39 "parameter Length");
42 T& operator[](size_t aIndex) {
43 if (MOZ_UNLIKELY(aIndex >= Length)) {
44 detail::InvalidArrayIndex_CRASH(aIndex, Length);
46 return mArr[aIndex];
49 const T& operator[](size_t aIndex) const {
50 if (MOZ_UNLIKELY(aIndex >= Length)) {
51 detail::InvalidArrayIndex_CRASH(aIndex, Length);
53 return mArr[aIndex];
56 bool operator==(const Array<T, Length>& aOther) const {
57 for (size_t i = 0; i < Length; i++) {
58 if (mArr[i] != aOther[i]) {
59 return false;
62 return true;
65 typedef T* iterator;
66 typedef const T* const_iterator;
67 typedef std::reverse_iterator<T*> reverse_iterator;
68 typedef std::reverse_iterator<const T*> const_reverse_iterator;
70 // Methods for range-based for loops.
71 iterator begin() { return mArr; }
72 constexpr const_iterator begin() const { return mArr; }
73 constexpr const_iterator cbegin() const { return begin(); }
74 iterator end() { return mArr + Length; }
75 constexpr const_iterator end() const { return mArr + Length; }
76 constexpr const_iterator cend() const { return end(); }
78 // Methods for reverse iterating.
79 reverse_iterator rbegin() { return reverse_iterator(end()); }
80 const_reverse_iterator rbegin() const {
81 return const_reverse_iterator(end());
83 const_reverse_iterator crbegin() const { return rbegin(); }
84 reverse_iterator rend() { return reverse_iterator(begin()); }
85 const_reverse_iterator rend() const {
86 return const_reverse_iterator(begin());
88 const_reverse_iterator crend() const { return rend(); }
91 template <typename T>
92 class Array<T, 0> {
93 public:
94 T& operator[](size_t aIndex) { MOZ_CRASH("indexing into zero-length array"); }
96 const T& operator[](size_t aIndex) const {
97 MOZ_CRASH("indexing into zero-length array");
101 // MOZ_DBG support
103 template <typename T, size_t Length>
104 std::ostream& operator<<(std::ostream& aOut, const Array<T, Length>& aArray) {
105 return aOut << Span(aArray);
108 } /* namespace mozilla */
110 #endif /* mozilla_Array_h */