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/Assertions.h"
12 #include "mozilla/Maybe.h"
13 #include "mozilla/Span.h"
14 #include "mozilla/UniquePtr.h"
15 #include "mozilla/UniquePtrExtensions.h"
20 * A move-only type that wraps a mozilla::UniquePtr<T[]> and the length of
23 * Unlike mozilla::Array, the length is a run-time property.
24 * Unlike mozilla::Vector and nsTArray, does not have capacity and
25 * assocatiated growth functionality.
26 * Unlike mozilla::Span, mozilla::Buffer owns the allocation it points to.
31 mozilla::UniquePtr
<T
[]> mData
;
35 Buffer(const Buffer
<T
>& aOther
) = delete;
36 Buffer
<T
>& operator=(const Buffer
<T
>& aOther
) = delete;
39 * Construct zero-lenth Buffer (without actually pointing to a heap
42 Buffer() : mData(nullptr), mLength(0){};
45 * Construct from raw parts.
47 * aLength must not be greater than the actual length of the buffer pointed
50 Buffer(mozilla::UniquePtr
<T
[]>&& aData
, size_t aLength
)
51 : mData(std::move(aData
)), mLength(aLength
) {}
54 * Move constructor. Sets the moved-from Buffer to zero-length
57 Buffer(Buffer
<T
>&& aOther
)
58 : mData(std::move(aOther
.mData
)), mLength(aOther
.mLength
) {
63 * Move assignment. Sets the moved-from Buffer to zero-length
66 Buffer
<T
>& operator=(Buffer
<T
>&& aOther
) {
67 mData
= std::move(aOther
.mData
);
68 mLength
= aOther
.mLength
;
74 * Construct by copying the elements of a Span.
76 * Allocates the internal buffer infallibly. Use CopyFrom for fallible
79 explicit Buffer(mozilla::Span
<const T
> aSpan
)
80 : mData(mozilla::MakeUniqueForOverwrite
<T
[]>(aSpan
.Length())),
81 mLength(aSpan
.Length()) {
82 std::copy(aSpan
.cbegin(), aSpan
.cend(), mData
.get());
86 * Create a new Buffer by copying the elements of a Span.
88 * Allocates the internal buffer fallibly.
90 static mozilla::Maybe
<Buffer
<T
>> CopyFrom(mozilla::Span
<const T
> aSpan
) {
91 if (aSpan
.IsEmpty()) {
92 return Some(Buffer());
95 auto data
= mozilla::MakeUniqueForOverwriteFallible
<T
[]>(aSpan
.Length());
97 return mozilla::Nothing();
99 std::copy(aSpan
.cbegin(), aSpan
.cend(), data
.get());
100 return mozilla::Some(Buffer(std::move(data
), aSpan
.Length()));
104 * Construct a buffer of requested length.
106 * The contents will be initialized or uninitialized according
107 * to the behavior of mozilla::MakeUnique<T[]>(aLength) for T.
109 * Allocates the internal buffer infallibly. Use Alloc for fallible
112 explicit Buffer(size_t aLength
)
113 : mData(mozilla::MakeUnique
<T
[]>(aLength
)), mLength(aLength
) {}
116 * Create a new Buffer with an internal buffer of requested length.
118 * The contents will be initialized or uninitialized according to the
119 * behavior of mozilla::MakeUnique<T[]>(aLength) for T.
121 * Allocates the internal buffer fallibly.
123 static mozilla::Maybe
<Buffer
<T
>> Alloc(size_t aLength
) {
124 auto data
= mozilla::MakeUniqueFallible
<T
[]>(aLength
);
126 return mozilla::Nothing();
128 return mozilla::Some(Buffer(std::move(data
), aLength
));
132 * Create a new Buffer with an internal buffer of requested length.
134 * This uses MakeUniqueFallibleForOverwrite so the contents will be
135 * default-initialized.
137 * Allocates the internal buffer fallibly.
139 static Maybe
<Buffer
<T
>> AllocForOverwrite(size_t aLength
) {
140 auto data
= MakeUniqueForOverwriteFallible
<T
[]>(aLength
);
144 return Some(Buffer(std::move(data
), aLength
));
147 auto AsSpan() const { return mozilla::Span
<const T
>{mData
.get(), mLength
}; }
148 auto AsWritableSpan() { return mozilla::Span
<T
>{mData
.get(), mLength
}; }
149 operator mozilla::Span
<const T
>() const { return AsSpan(); }
150 operator mozilla::Span
<T
>() { return AsWritableSpan(); }
153 * Guarantees a non-null and aligned pointer
154 * even for the zero-length case.
156 T
* Elements() { return AsWritableSpan().Elements(); }
157 size_t Length() const { return mLength
; }
159 T
& operator[](size_t aIndex
) {
160 MOZ_ASSERT(aIndex
< mLength
);
161 return mData
.get()[aIndex
];
164 const T
& operator[](size_t aIndex
) const {
165 MOZ_ASSERT(aIndex
< mLength
);
166 return mData
.get()[aIndex
];
170 typedef const T
* const_iterator
;
171 typedef std::reverse_iterator
<T
*> reverse_iterator
;
172 typedef std::reverse_iterator
<const T
*> const_reverse_iterator
;
174 // Methods for range-based for loops.
175 iterator
begin() { return mData
.get(); }
176 const_iterator
begin() const { return mData
.get(); }
177 const_iterator
cbegin() const { return begin(); }
178 iterator
end() { return mData
.get() + mLength
; }
179 const_iterator
end() const { return mData
.get() + mLength
; }
180 const_iterator
cend() const { return end(); }
182 // Methods for reverse iterating.
183 reverse_iterator
rbegin() { return reverse_iterator(end()); }
184 const_reverse_iterator
rbegin() const {
185 return const_reverse_iterator(end());
187 const_reverse_iterator
crbegin() const { return rbegin(); }
188 reverse_iterator
rend() { return reverse_iterator(begin()); }
189 const_reverse_iterator
rend() const {
190 return const_reverse_iterator(begin());
192 const_reverse_iterator
crend() const { return rend(); }
195 } /* namespace mozilla */
197 #endif /* mozilla_Buffer_h */