From be02328f0ea02f76268eb16cda61fa4ca16111aa Mon Sep 17 00:00:00 2001 From: Harald Alvestrand Date: Mon, 4 Dec 2023 09:32:14 +0000 Subject: [PATCH] Redefine ByteBufferWriterT template to really use argument Previously, ByteBufferWriterT could only be instantiated for "char", due to using "char" internally. The rewrite extracts the inner type from the BufferT parameter and uses that. This is preparatory to changing its type to "uint8_t". Bug: webrtc:15665 Change-Id: Ib771d37e3abb8261049c16122c6b43dcb561e9a0 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/329680 Reviewed-by: Tomas Gunnarsson Commit-Queue: Harald Alvestrand Cr-Commit-Position: refs/heads/main@{#41306} --- rtc_base/byte_buffer.h | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/rtc_base/byte_buffer.h b/rtc_base/byte_buffer.h index 4a1e5f23a8..77c80680c1 100644 --- a/rtc_base/byte_buffer.h +++ b/rtc_base/byte_buffer.h @@ -27,39 +27,43 @@ namespace rtc { template class ByteBufferWriterT { + using value_type = typename BufferClassT::value_type; + public: ByteBufferWriterT() { Construct(nullptr, kDefaultCapacity); } - ByteBufferWriterT(const char* bytes, size_t len) { Construct(bytes, len); } + ByteBufferWriterT(const value_type* bytes, size_t len) { + Construct(bytes, len); + } ByteBufferWriterT(const ByteBufferWriterT&) = delete; ByteBufferWriterT& operator=(const ByteBufferWriterT&) = delete; - const char* Data() const { return buffer_.data(); } + const value_type* Data() const { return buffer_.data(); } size_t Length() const { return buffer_.size(); } size_t Capacity() const { return buffer_.capacity(); } // Write value to the buffer. Resizes the buffer when it is // neccessary. void WriteUInt8(uint8_t val) { - WriteBytes(reinterpret_cast(&val), 1); + WriteBytes(reinterpret_cast(&val), 1); } void WriteUInt16(uint16_t val) { uint16_t v = HostToNetwork16(val); - WriteBytes(reinterpret_cast(&v), 2); + WriteBytes(reinterpret_cast(&v), 2); } void WriteUInt24(uint32_t val) { uint32_t v = HostToNetwork32(val); - char* start = reinterpret_cast(&v); + value_type* start = reinterpret_cast(&v); ++start; WriteBytes(start, 3); } void WriteUInt32(uint32_t val) { uint32_t v = HostToNetwork32(val); - WriteBytes(reinterpret_cast(&v), 4); + WriteBytes(reinterpret_cast(&v), 4); } void WriteUInt64(uint64_t val) { uint64_t v = HostToNetwork64(val); - WriteBytes(reinterpret_cast(&v), 8); + WriteBytes(reinterpret_cast(&v), 8); } // Serializes an unsigned varint in the format described by // https://developers.google.com/protocol-buffers/docs/encoding#varints @@ -68,22 +72,24 @@ class ByteBufferWriterT { while (val >= 0x80) { // Write 7 bits at a time, then set the msb to a continuation byte // (msb=1). - char byte = static_cast(val) | 0x80; + value_type byte = static_cast(val) | 0x80; WriteBytes(&byte, 1); val >>= 7; } - char last_byte = static_cast(val); + value_type last_byte = static_cast(val); WriteBytes(&last_byte, 1); } void WriteString(absl::string_view val) { WriteBytes(val.data(), val.size()); } - void WriteBytes(const char* val, size_t len) { buffer_.AppendData(val, len); } + void WriteBytes(const value_type* val, size_t len) { + buffer_.AppendData(val, len); + } - // Reserves the given number of bytes and returns a char* that can be written - // into. Useful for functions that require a char* buffer and not a - // ByteBufferWriter. - char* ReserveWriteBuffer(size_t len) { + // Reserves the given number of bytes and returns a value_type* that can be + // written into. Useful for functions that require a value_type* buffer and + // not a ByteBufferWriter. + value_type* ReserveWriteBuffer(size_t len) { buffer_.SetSize(buffer_.size() + len); return buffer_.data(); } @@ -97,7 +103,7 @@ class ByteBufferWriterT { private: static constexpr size_t kDefaultCapacity = 4096; - void Construct(const char* bytes, size_t size) { + void Construct(const value_type* bytes, size_t size) { if (bytes) { buffer_.AppendData(bytes, size); } else {