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/. */
5 #ifndef mozilla_Buffer_h
6 #define mozilla_Buffer_h
11 #include "mozilla/Maybe.h"
12 #include "mozilla/Span.h"
13 #include "mozilla/UniquePtr.h"
14 #include "mozilla/UniquePtrExtensions.h"
19 * A move-only type that wraps a mozilla::UniquePtr<T[]> and the length of
22 * Unlike mozilla::Array, the length is a run-time property.
23 * Unlike mozilla::Vector and nsTArray, does not have capacity and
24 * assocatiated growth functionality.
25 * Unlike mozilla::Span, mozilla::Buffer owns the allocation it points to.
30 mozilla::UniquePtr
<T
[]> mData
;
34 Buffer(const Buffer
<T
>& aOther
) = delete;
35 Buffer
<T
>& operator=(const Buffer
<T
>& aOther
) = delete;
38 * Construct zero-lenth Buffer (without actually pointing to a heap
41 Buffer() : mData(nullptr), mLength(0){};
44 * Construct from raw parts.
46 * aLength must not be greater than the actual length of the buffer pointed
49 Buffer(mozilla::UniquePtr
<T
[]>&& aData
, size_t aLength
)
50 : mData(std::move(aData
)), mLength(aLength
) {}
53 * Move constructor. Sets the moved-from Buffer to zero-length
56 Buffer(Buffer
<T
>&& aOther
)
57 : mData(std::move(aOther
.mData
)), mLength(aOther
.mLength
) {
62 * Move assignment. Sets the moved-from Buffer to zero-length
65 Buffer
<T
>& operator=(Buffer
<T
>&& aOther
) {
66 mData
= std::move(aOther
.mData
);
67 mLength
= aOther
.mLength
;
73 * Construct by copying the elements of a Span.
75 * Allocates the internal buffer infallibly. Use CopyFrom for fallible
78 explicit Buffer(mozilla::Span
<const T
> aSpan
)
79 : mData(mozilla::MakeUniqueForOverwrite
<T
[]>(aSpan
.Length())),
80 mLength(aSpan
.Length()) {
81 std::copy(aSpan
.cbegin(), aSpan
.cend(), mData
.get());
85 * Create a new Buffer by copying the elements of a Span.
87 * Allocates the internal buffer fallibly.
89 static mozilla::Maybe
<Buffer
<T
>> CopyFrom(mozilla::Span
<const T
> aSpan
) {
90 if (aSpan
.IsEmpty()) {
91 return Some(Buffer());
94 auto data
= mozilla::MakeUniqueForOverwriteFallible
<T
[]>(aSpan
.Length());
96 return mozilla::Nothing();
98 std::copy(aSpan
.cbegin(), aSpan
.cend(), data
.get());
99 return mozilla::Some(Buffer(std::move(data
), aSpan
.Length()));
103 * Construct a buffer of requested length.
105 * The contents will be initialized or uninitialized according
106 * to the behavior of mozilla::MakeUnique<T[]>(aLength) for T.
108 * Allocates the internal buffer infallibly. Use Alloc for fallible
111 explicit Buffer(size_t aLength
)
112 : mData(mozilla::MakeUnique
<T
[]>(aLength
)), mLength(aLength
) {}
115 * Create a new Buffer with an internal buffer of requested length.
117 * The contents will be initialized or uninitialized according to the
118 * behavior of mozilla::MakeUnique<T[]>(aLength) for T.
120 * Allocates the internal buffer fallibly.
122 static mozilla::Maybe
<Buffer
<T
>> Alloc(size_t aLength
) {
123 auto data
= mozilla::MakeUniqueFallible
<T
[]>(aLength
);
125 return mozilla::Nothing();
127 return mozilla::Some(Buffer(std::move(data
), aLength
));
131 * Create a new Buffer with an internal buffer of requested length.
133 * This uses MakeUniqueFallibleForOverwrite so the contents will be
134 * default-initialized.
136 * Allocates the internal buffer fallibly.
138 static Maybe
<Buffer
<T
>> AllocForOverwrite(size_t aLength
) {
139 auto data
= MakeUniqueForOverwriteFallible
<T
[]>(aLength
);
143 return Some(Buffer(std::move(data
), aLength
));
146 auto AsSpan() const { return mozilla::Span
<const T
>{mData
.get(), mLength
}; }
147 auto AsWritableSpan() { return mozilla::Span
<T
>{mData
.get(), mLength
}; }
148 operator mozilla::Span
<const T
>() const { return AsSpan(); }
149 operator mozilla::Span
<T
>() { return AsWritableSpan(); }
152 * Guarantees a non-null and aligned pointer
153 * even for the zero-length case.
155 T
* Elements() { return AsWritableSpan().Elements(); }
156 size_t Length() const { return mLength
; }
158 T
& operator[](size_t aIndex
) {
159 MOZ_ASSERT(aIndex
< mLength
);
160 return mData
.get()[aIndex
];
163 const T
& operator[](size_t aIndex
) const {
164 MOZ_ASSERT(aIndex
< mLength
);
165 return mData
.get()[aIndex
];
169 typedef const T
* const_iterator
;
170 typedef std::reverse_iterator
<T
*> reverse_iterator
;
171 typedef std::reverse_iterator
<const T
*> const_reverse_iterator
;
173 // Methods for range-based for loops.
174 iterator
begin() { return mData
.get(); }
175 const_iterator
begin() const { return mData
.get(); }
176 const_iterator
cbegin() const { return begin(); }
177 iterator
end() { return mData
.get() + mLength
; }
178 const_iterator
end() const { return mData
.get() + mLength
; }
179 const_iterator
cend() const { return end(); }
181 // Methods for reverse iterating.
182 reverse_iterator
rbegin() { return reverse_iterator(end()); }
183 const_reverse_iterator
rbegin() const {
184 return const_reverse_iterator(end());
186 const_reverse_iterator
crbegin() const { return rbegin(); }
187 reverse_iterator
rend() { return reverse_iterator(begin()); }
188 const_reverse_iterator
rend() const {
189 return const_reverse_iterator(begin());
191 const_reverse_iterator
crend() const { return rend(); }
194 } /* namespace mozilla */
196 #endif /* mozilla_Buffer_h */