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/. */
8 #include "mozilla/EndianUtils.h"
13 template <typename Endianess
>
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
) {
23 Endianess::writeUint16(&c
[0], aShort
);
24 return Write(&c
[0], 2);
27 [[nodiscard
]] bool WriteU32(uint32_t aLong
) {
29 Endianess::writeUint32(&c
[0], aLong
);
30 return Write(&c
[0], 4);
33 [[nodiscard
]] bool Write32(int32_t aLong
) {
35 Endianess::writeInt32(&c
[0], aLong
);
36 return Write(&c
[0], 4);
39 [[nodiscard
]] bool WriteU64(uint64_t aLongLong
) {
41 Endianess::writeUint64(&c
[0], aLongLong
);
42 return Write(&c
[0], 8);
45 [[nodiscard
]] bool Write64(int64_t aLongLong
) {
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
);
56 nsTArray
<uint8_t>& mPtr
;
59 } // namespace mozilla