Bug 1468696 [wpt PR 11497] - Update wpt metadata, a=testonly
[gecko.git] / mfbt / Buffer.h
blob2f2f3bc2632e14d91406fcf6b6f195a0e15040bd
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
8 #include <algorithm>
9 #include "mozilla/Maybe.h"
10 #include "mozilla/Span.h"
11 #include "mozilla/UniquePtr.h"
12 #include "mozilla/UniquePtrExtensions.h"
14 namespace mozilla {
16 /**
17 * A move-only type that wraps a mozilla::UniquePtr<T[]> and the length of
18 * the T[].
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.
25 template <typename T>
26 class Buffer final {
27 private:
28 mozilla::UniquePtr<T[]> mData;
29 size_t mLength;
31 public:
32 Buffer(const Buffer<T>& aOther) = delete;
33 Buffer<T>& operator=(const Buffer<T>& aOther) = delete;
35 /**
36 * Construct zero-lenth Buffer (without actually pointing to a heap
37 * allocation).
39 Buffer() : mData(nullptr), mLength(0){};
41 /**
42 * Construct from raw parts.
44 * aLength must not be greater than the actual length of the buffer pointed
45 * to by aData.
47 Buffer(mozilla::UniquePtr<T[]>&& aData, size_t aLength)
48 : mData(std::move(aData)), mLength(aLength) {}
50 /**
51 * Move constructor. Sets the moved-from Buffer to zero-length
52 * state.
54 Buffer(Buffer<T>&& aOther)
55 : mData(std::move(aOther.mData)), mLength(aOther.mLength) {
56 aOther.mLength = 0;
59 /**
60 * Construct by copying the elements of a Span.
62 * Allocates the internal buffer infallibly. Use CopyFrom for fallible
63 * allocation.
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());
71 /**
72 * Create a new Buffer by copying the elements of a Span.
74 * Allocates the internal buffer fallibly.
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();
81 std::copy(aSpan.cbegin(), aSpan.cend(), data.get());
82 return mozilla::Some(Buffer(std::move(data), aSpan.Length()));
85 /**
86 * Construct a buffer of requested length.
88 * The contents will be initialized or uninitialized according
89 * to the behavior of mozilla::MakeUnique<T[]>(aLength) for T.
91 * Allocates the internal buffer infallibly. Use Alloc for fallible
92 * allocation.
94 explicit Buffer(size_t aLength)
95 : mData(mozilla::MakeUnique<T[]>(aLength)), mLength(aLength) {}
97 /**
98 * Create a new Buffer with an internal buffer of requested length.
100 * The contents will be initialized or uninitialized according to the
101 * behavior of mozilla::MakeUnique<T[]>(aLength) for T.
103 * Allocates the internal buffer fallibly.
105 static mozilla::Maybe<Buffer<T>> Alloc(size_t aLength) {
106 auto data = mozilla::MakeUniqueFallible<T[]>(aLength);
107 if (!data) {
108 return mozilla::Nothing();
110 return mozilla::Some(Buffer(std::move(data), aLength));
113 mozilla::Span<const T> AsSpan() const {
114 return mozilla::MakeSpan(mData.get(), mLength);
116 mozilla::Span<T> AsWritableSpan() {
117 return mozilla::MakeSpan(mData.get(), mLength);
119 operator mozilla::Span<const T>() const { return AsSpan(); }
120 operator mozilla::Span<T>() { return AsWritableSpan(); }
123 * Guarantees a non-null and aligned pointer
124 * even for the zero-length case.
126 T* Elements() { return AsWritableSpan().Elements(); }
127 size_t Length() const { return mLength; }
129 T& operator[](size_t aIndex) {
130 MOZ_ASSERT(aIndex < mLength);
131 return mData.get()[aIndex];
134 const T& operator[](size_t aIndex) const {
135 MOZ_ASSERT(aIndex < mLength);
136 return mData.get()[aIndex];
139 typedef T* iterator;
140 typedef const T* const_iterator;
141 typedef ReverseIterator<T*> reverse_iterator;
142 typedef ReverseIterator<const T*> const_reverse_iterator;
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(); }
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());
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());
162 const_reverse_iterator crend() const { return rend(); }
165 } /* namespace mozilla */
167 #endif /* mozilla_Buffer_h */