Source code

Revision control

Other Tools

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/. */
6
7
/* A compile-time constant-length array with bounds-checking assertions. */
8
9
#ifndef mozilla_Array_h
10
#define mozilla_Array_h
11
12
#include <stddef.h>
13
14
#include <ostream>
15
#include <utility>
16
17
#include "mozilla/Assertions.h"
18
#include "mozilla/Attributes.h"
19
#include "mozilla/ReverseIterator.h"
20
21
namespace mozilla {
22
23
template <typename T, size_t _Length>
24
class Array {
25
T mArr[_Length];
26
27
public:
28
using ElementType = T;
29
static constexpr size_t Length = _Length;
30
31
Array() {}
32
33
template <typename... Args>
34
MOZ_IMPLICIT constexpr Array(Args&&... aArgs)
35
: mArr{std::forward<Args>(aArgs)...} {
36
static_assert(sizeof...(aArgs) == Length,
37
"The number of arguments should be equal to the template "
38
"parameter Length");
39
}
40
41
T& operator[](size_t aIndex) {
42
MOZ_ASSERT(aIndex < Length);
43
return mArr[aIndex];
44
}
45
46
const T& operator[](size_t aIndex) const {
47
MOZ_ASSERT(aIndex < Length);
48
return mArr[aIndex];
49
}
50
51
bool operator==(const Array<T, Length>& aOther) const {
52
for (size_t i = 0; i < Length; i++) {
53
if (mArr[i] != aOther[i]) {
54
return false;
55
}
56
}
57
return true;
58
}
59
60
typedef T* iterator;
61
typedef const T* const_iterator;
62
typedef ReverseIterator<T*> reverse_iterator;
63
typedef ReverseIterator<const T*> const_reverse_iterator;
64
65
// Methods for range-based for loops.
66
iterator begin() { return mArr; }
67
const_iterator begin() const { return mArr; }
68
const_iterator cbegin() const { return begin(); }
69
iterator end() { return mArr + Length; }
70
const_iterator end() const { return mArr + Length; }
71
const_iterator cend() const { return end(); }
72
73
// Methods for reverse iterating.
74
reverse_iterator rbegin() { return reverse_iterator(end()); }
75
const_reverse_iterator rbegin() const {
76
return const_reverse_iterator(end());
77
}
78
const_reverse_iterator crbegin() const { return rbegin(); }
79
reverse_iterator rend() { return reverse_iterator(begin()); }
80
const_reverse_iterator rend() const {
81
return const_reverse_iterator(begin());
82
}
83
const_reverse_iterator crend() const { return rend(); }
84
};
85
86
template <typename T>
87
class Array<T, 0> {
88
public:
89
T& operator[](size_t aIndex) { MOZ_CRASH("indexing into zero-length array"); }
90
91
const T& operator[](size_t aIndex) const {
92
MOZ_CRASH("indexing into zero-length array");
93
}
94
};
95
96
// MOZ_DBG support
97
98
template <typename T, size_t Length>
99
std::ostream& operator<<(std::ostream& aOut, const Array<T, Length>& aArray) {
100
return aOut << MakeSpan(aArray);
101
}
102
103
} /* namespace mozilla */
104
105
#endif /* mozilla_Array_h */