Source code

Revision control

Other Tools

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
#ifndef mozilla_Buffer_h
6
#define mozilla_Buffer_h
7
8
#include <algorithm>
9
#include "mozilla/Maybe.h"
10
#include "mozilla/Span.h"
11
#include "mozilla/UniquePtr.h"
12
#include "mozilla/UniquePtrExtensions.h"
13
14
namespace mozilla {
15
16
/**
17
* A move-only type that wraps a mozilla::UniquePtr<T[]> and the length of
18
* the T[].
19
*
20
* Unlike mozilla::Array, the length is a run-time property.
21
* Unlike mozilla::Vector and nsTArray, does not have capacity and
22
* assocatiated growth functionality.
23
* Unlike mozilla::Span, mozilla::Buffer owns the allocation it points to.
24
*/
25
template <typename T>
26
class Buffer final {
27
private:
28
mozilla::UniquePtr<T[]> mData;
29
size_t mLength;
30
31
public:
32
Buffer(const Buffer<T>& aOther) = delete;
33
Buffer<T>& operator=(const Buffer<T>& aOther) = delete;
34
35
/**
36
* Construct zero-lenth Buffer (without actually pointing to a heap
37
* allocation).
38
*/
39
Buffer() : mData(nullptr), mLength(0){};
40
41
/**
42
* Construct from raw parts.
43
*
44
* aLength must not be greater than the actual length of the buffer pointed
45
* to by aData.
46
*/
47
Buffer(mozilla::UniquePtr<T[]>&& aData, size_t aLength)
48
: mData(std::move(aData)), mLength(aLength) {}
49
50
/**
51
* Move constructor. Sets the moved-from Buffer to zero-length
52
* state.
53
*/
54
Buffer(Buffer<T>&& aOther)
55
: mData(std::move(aOther.mData)), mLength(aOther.mLength) {
56
aOther.mLength = 0;
57
}
58
59
/**
60
* Construct by copying the elements of a Span.
61
*
62
* Allocates the internal buffer infallibly. Use CopyFrom for fallible
63
* allocation.
64
*/
65
explicit Buffer(mozilla::Span<const T> aSpan)
66
: mData(mozilla::MakeUnique<T[]>(aSpan.Length())),
67
mLength(aSpan.Length()) {
68
std::copy(aSpan.cbegin(), aSpan.cend(), mData.get());
69
}
70
71
/**
72
* Create a new Buffer by copying the elements of a Span.
73
*
74
* Allocates the internal buffer fallibly.
75
*/
76
static mozilla::Maybe<Buffer<T>> CopyFrom(mozilla::Span<const T> aSpan) {
77
auto data = mozilla::MakeUniqueFallible<T[]>(aSpan.Length());
78
if (!data) {
79
return mozilla::Nothing();
80
}
81
std::copy(aSpan.cbegin(), aSpan.cend(), data.get());
82
return mozilla::Some(Buffer(std::move(data), aSpan.Length()));
83
}
84
85
/**
86
* Construct a buffer of requested length.
87
*
88
* The contents will be initialized or uninitialized according
89
* to the behavior of mozilla::MakeUnique<T[]>(aLength) for T.
90
*
91
* Allocates the internal buffer infallibly. Use Alloc for fallible
92
* allocation.
93
*/
94
explicit Buffer(size_t aLength)
95
: mData(mozilla::MakeUnique<T[]>(aLength)), mLength(aLength) {}
96
97
/**
98
* Create a new Buffer with an internal buffer of requested length.
99
*
100
* The contents will be initialized or uninitialized according to the
101
* behavior of mozilla::MakeUnique<T[]>(aLength) for T.
102
*
103
* Allocates the internal buffer fallibly.
104
*/
105
static mozilla::Maybe<Buffer<T>> Alloc(size_t aLength) {
106
auto data = mozilla::MakeUniqueFallible<T[]>(aLength);
107
if (!data) {
108
return mozilla::Nothing();
109
}
110
return mozilla::Some(Buffer(std::move(data), aLength));
111
}
112
113
mozilla::Span<const T> AsSpan() const {
114
return mozilla::MakeSpan(mData.get(), mLength);
115
}
116
mozilla::Span<T> AsWritableSpan() {
117
return mozilla::MakeSpan(mData.get(), mLength);
118
}
119
operator mozilla::Span<const T>() const { return AsSpan(); }
120
operator mozilla::Span<T>() { return AsWritableSpan(); }
121
122
/**
123
* Guarantees a non-null and aligned pointer
124
* even for the zero-length case.
125
*/
126
T* Elements() { return AsWritableSpan().Elements(); }
127
size_t Length() const { return mLength; }
128
129
T& operator[](size_t aIndex) {
130
MOZ_ASSERT(aIndex < mLength);
131
return mData.get()[aIndex];
132
}
133
134
const T& operator[](size_t aIndex) const {
135
MOZ_ASSERT(aIndex < mLength);
136
return mData.get()[aIndex];
137
}
138
139
typedef T* iterator;
140
typedef const T* const_iterator;
141
typedef ReverseIterator<T*> reverse_iterator;
142
typedef ReverseIterator<const T*> const_reverse_iterator;
143
144
// Methods for range-based for loops.
145
iterator begin() { return mData.get(); }
146
const_iterator begin() const { return mData.get(); }
147
const_iterator cbegin() const { return begin(); }
148
iterator end() { return mData.get() + mLength; }
149
const_iterator end() const { return mData.get() + mLength; }
150
const_iterator cend() const { return end(); }
151
152
// Methods for reverse iterating.
153
reverse_iterator rbegin() { return reverse_iterator(end()); }
154
const_reverse_iterator rbegin() const {
155
return const_reverse_iterator(end());
156
}
157
const_reverse_iterator crbegin() const { return rbegin(); }
158
reverse_iterator rend() { return reverse_iterator(begin()); }
159
const_reverse_iterator rend() const {
160
return const_reverse_iterator(begin());
161
}
162
const_reverse_iterator crend() const { return rend(); }
163
};
164
165
} /* namespace mozilla */
166
167
#endif /* mozilla_Buffer_h */