no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / dom / media / ByteWriter.h
blobcc7f5ecf3f99b9bd9fc7a3c4ace084a4d79d3b69
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 BYTE_WRITER_H_
6 #define BYTE_WRITER_H_
8 #include "mozilla/EndianUtils.h"
9 #include "nsTArray.h"
11 namespace mozilla {
13 template <typename Endianess>
14 class ByteWriter {
15 public:
16 explicit ByteWriter(nsTArray<uint8_t>& aData) : mPtr(aData) {}
17 ~ByteWriter() = default;
19 [[nodiscard]] bool WriteU8(uint8_t aByte) { return Write(&aByte, 1); }
21 [[nodiscard]] bool WriteU16(uint16_t aShort) {
22 uint8_t c[2];
23 Endianess::writeUint16(&c[0], aShort);
24 return Write(&c[0], 2);
27 [[nodiscard]] bool WriteU32(uint32_t aLong) {
28 uint8_t c[4];
29 Endianess::writeUint32(&c[0], aLong);
30 return Write(&c[0], 4);
33 [[nodiscard]] bool Write32(int32_t aLong) {
34 uint8_t c[4];
35 Endianess::writeInt32(&c[0], aLong);
36 return Write(&c[0], 4);
39 [[nodiscard]] bool WriteU64(uint64_t aLongLong) {
40 uint8_t c[8];
41 Endianess::writeUint64(&c[0], aLongLong);
42 return Write(&c[0], 8);
45 [[nodiscard]] bool Write64(int64_t aLongLong) {
46 uint8_t c[8];
47 Endianess::writeInt64(&c[0], aLongLong);
48 return Write(&c[0], 8);
51 [[nodiscard]] bool Write(const uint8_t* aSrc, size_t aCount) {
52 return mPtr.AppendElements(aSrc, aCount, mozilla::fallible);
55 private:
56 nsTArray<uint8_t>& mPtr;
59 } // namespace mozilla
61 #endif