Bug 1584002 [wpt PR 19310] - Add serialization WPT for CSS comparison functions min...
[gecko.git] / mfbt / Array.h
blob1dd777a099655209d3fa2e054393c88ba62c5a95
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 <ostream>
18 #include <stddef.h>
20 namespace mozilla {
22 template <typename T, size_t Length>
23 class Array {
24 T mArr[Length];
26 public:
27 Array() {}
29 template <typename... Args>
30 MOZ_IMPLICIT constexpr Array(Args&&... aArgs)
31 : mArr{std::forward<Args>(aArgs)...} {
32 static_assert(sizeof...(aArgs) == Length,
33 "The number of arguments should be equal to the template "
34 "parameter Length");
37 T& operator[](size_t aIndex) {
38 MOZ_ASSERT(aIndex < Length);
39 return mArr[aIndex];
42 const T& operator[](size_t aIndex) const {
43 MOZ_ASSERT(aIndex < Length);
44 return mArr[aIndex];
47 bool operator==(const Array<T, Length>& aOther) const {
48 for (size_t i = 0; i < Length; i++) {
49 if (mArr[i] != aOther[i]) {
50 return false;
53 return true;
56 typedef T* iterator;
57 typedef const T* const_iterator;
58 typedef ReverseIterator<T*> reverse_iterator;
59 typedef ReverseIterator<const T*> const_reverse_iterator;
61 // Methods for range-based for loops.
62 iterator begin() { return mArr; }
63 const_iterator begin() const { return mArr; }
64 const_iterator cbegin() const { return begin(); }
65 iterator end() { return mArr + Length; }
66 const_iterator end() const { return mArr + Length; }
67 const_iterator cend() const { return end(); }
69 // Methods for reverse iterating.
70 reverse_iterator rbegin() { return reverse_iterator(end()); }
71 const_reverse_iterator rbegin() const {
72 return const_reverse_iterator(end());
74 const_reverse_iterator crbegin() const { return rbegin(); }
75 reverse_iterator rend() { return reverse_iterator(begin()); }
76 const_reverse_iterator rend() const {
77 return const_reverse_iterator(begin());
79 const_reverse_iterator crend() const { return rend(); }
82 template <typename T>
83 class Array<T, 0> {
84 public:
85 T& operator[](size_t aIndex) { MOZ_CRASH("indexing into zero-length array"); }
87 const T& operator[](size_t aIndex) const {
88 MOZ_CRASH("indexing into zero-length array");
92 // MOZ_DBG support
94 template <typename T, size_t Length>
95 std::ostream& operator<<(std::ostream& aOut, const Array<T, Length>& aArray) {
96 return aOut << MakeSpan(aArray);
99 } /* namespace mozilla */
101 #endif /* mozilla_Array_h */