From 1a86b781800ecc2fcce1ad21db8d71fafbf37c67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20M=C3=B6ller?= Date: Mon, 14 Jan 2019 12:48:53 +0100 Subject: [PATCH] Delete StreamInterface methods GetPosition, SetPosition and Rewind Keep methods on subclasses where they are used: FifoBuffer and MemoryStream. Also FileStream gets to keep SetPosition, because it's used by a downstream subclass. Bug: webrtc:6424 Change-Id: If2a00855aba7c2c9dc0909cda7c8a8ef00e0b9af Reviewed-on: https://webrtc-review.googlesource.com/c/116487 Commit-Queue: Niels Moller Reviewed-by: Karl Wiberg Cr-Commit-Position: refs/heads/master@{#26237} --- rtc_base/logging_unittest.cc | 15 --------------- rtc_base/memory_stream.cc | 4 ++++ rtc_base/memory_stream.h | 6 ++++-- rtc_base/stream.cc | 28 ---------------------------- rtc_base/stream.h | 30 ++++++++++++++---------------- rtc_base/stream_unittest.cc | 31 ------------------------------- 6 files changed, 22 insertions(+), 92 deletions(-) diff --git a/rtc_base/logging_unittest.cc b/rtc_base/logging_unittest.cc index bbeac444ed..4e64209cf2 100644 --- a/rtc_base/logging_unittest.cc +++ b/rtc_base/logging_unittest.cc @@ -40,8 +40,6 @@ class StringStream : public StreamInterface { size_t* written, int* error) override; void Close() override; - bool SetPosition(size_t position) override; - bool GetPosition(size_t* position) const override; private: std::string& str_; @@ -92,19 +90,6 @@ StreamResult StringStream::Write(const void* data, void StringStream::Close() {} -bool StringStream::SetPosition(size_t position) { - if (position > str_.size()) - return false; - read_pos_ = position; - return true; -} - -bool StringStream::GetPosition(size_t* position) const { - if (position) - *position = read_pos_; - return true; -} - } // namespace template diff --git a/rtc_base/memory_stream.cc b/rtc_base/memory_stream.cc index e71c0e65d0..a30aacd139 100644 --- a/rtc_base/memory_stream.cc +++ b/rtc_base/memory_stream.cc @@ -91,6 +91,10 @@ bool MemoryStream::GetPosition(size_t* position) const { return true; } +void MemoryStream::Rewind() { + seek_position_ = 0; +} + bool MemoryStream::GetSize(size_t* size) const { if (size) *size = data_length_; diff --git a/rtc_base/memory_stream.h b/rtc_base/memory_stream.h index 51f81d6756..bdcc40d3fa 100644 --- a/rtc_base/memory_stream.h +++ b/rtc_base/memory_stream.h @@ -36,11 +36,13 @@ class MemoryStream final : public StreamInterface { size_t* bytes_written, int* error) override; void Close() override; - bool SetPosition(size_t position) override; - bool GetPosition(size_t* position) const override; bool GetSize(size_t* size) const; bool ReserveSize(size_t size); + bool SetPosition(size_t position); + bool GetPosition(size_t* position) const; + void Rewind(); + char* GetBuffer() { return buffer_; } const char* GetBuffer() const { return buffer_; } diff --git a/rtc_base/stream.cc b/rtc_base/stream.cc index 083df401fe..01f150486d 100644 --- a/rtc_base/stream.cc +++ b/rtc_base/stream.cc @@ -77,14 +77,6 @@ void StreamInterface::PostEvent(int events, int err) { PostEvent(Thread::Current(), events, err); } -bool StreamInterface::SetPosition(size_t position) { - return false; -} - -bool StreamInterface::GetPosition(size_t* position) const { - return false; -} - bool StreamInterface::Flush() { return false; } @@ -129,14 +121,6 @@ void StreamAdapterInterface::Close() { stream_->Close(); } -bool StreamAdapterInterface::SetPosition(size_t position) { - return stream_->SetPosition(position); -} - -bool StreamAdapterInterface::GetPosition(size_t* position) const { - return stream_->GetPosition(position); -} - bool StreamAdapterInterface::Flush() { return stream_->Flush(); } @@ -288,18 +272,6 @@ bool FileStream::SetPosition(size_t position) { return (fseek(file_, static_cast(position), SEEK_SET) == 0); } -bool FileStream::GetPosition(size_t* position) const { - RTC_DCHECK(nullptr != position); - if (!file_) - return false; - long result = ftell(file_); - if (result < 0) - return false; - if (position) - *position = result; - return true; -} - bool FileStream::Flush() { if (file_) { return (0 == fflush(file_)); diff --git a/rtc_base/stream.h b/rtc_base/stream.h index 718c12e986..06c3d63e38 100644 --- a/rtc_base/stream.h +++ b/rtc_base/stream.h @@ -107,15 +107,6 @@ class StreamInterface : public MessageHandler { // Like the aforementioned method, but posts to the current thread. void PostEvent(int events, int err); - // Seek to a byte offset from the beginning of the stream. Returns false if - // the stream does not support seeking, or cannot seek to the specified - // position. - virtual bool SetPosition(size_t position); - - // Get the byte offset of the current position from the start of the stream. - // Returns false if the position is not known. - virtual bool GetPosition(size_t* position) const; - // Return true if flush is successful. virtual bool Flush(); @@ -125,9 +116,6 @@ class StreamInterface : public MessageHandler { // These methods are implemented in terms of other methods, for convenience. // - // Seek to the start of the stream. - inline bool Rewind() { return SetPosition(0); } - // WriteAll is a helper function which repeatedly calls Write until all the // data is written, or something other than SR_SUCCESS is returned. Note that // unlike Write, the argument 'written' is always set, and may be non-zero @@ -180,8 +168,6 @@ class StreamAdapterInterface : public StreamInterface, int* error) override; void Close() override; - bool SetPosition(size_t position) override; - bool GetPosition(size_t* position) const override; bool Flush() override; void Attach(StreamInterface* stream, bool owned = true); @@ -232,8 +218,7 @@ class FileStream : public StreamInterface { size_t* written, int* error) override; void Close() override; - bool SetPosition(size_t position) override; - bool GetPosition(size_t* position) const override; + virtual bool SetPosition(size_t position); bool Flush() override; @@ -291,6 +276,19 @@ class FifoBuffer final : public StreamInterface { size_t* bytes_written, int* error) override; void Close() override; + + // Seek to a byte offset from the beginning of the stream. Returns false if + // the stream does not support seeking, or cannot seek to the specified + // position. + bool SetPosition(size_t position); + + // Get the byte offset of the current position from the start of the stream. + // Returns false if the position is not known. + bool GetPosition(size_t* position) const; + + // Seek to the start of the stream. + bool Rewind() { return SetPosition(0); } + // GetReadData returns a pointer to a buffer which is owned by the stream. // The buffer contains data_len bytes. null is returned if no data is // available, or if the method fails. If the caller processes the data, it diff --git a/rtc_base/stream_unittest.cc b/rtc_base/stream_unittest.cc index a5a4c8efdb..bd6e84f591 100644 --- a/rtc_base/stream_unittest.cc +++ b/rtc_base/stream_unittest.cc @@ -50,17 +50,6 @@ class TestStream : public StreamInterface { void Close() override {} - bool SetPosition(size_t position) override { - pos_ = position; - return true; - } - - bool GetPosition(size_t* position) const override { - if (position) - *position = pos_; - return true; - } - private: size_t pos_; }; @@ -78,26 +67,6 @@ bool VerifyTestBuffer(unsigned char* buffer, size_t len, unsigned char value) { return passed; } -void SeekTest(StreamInterface* stream, const unsigned char value) { - size_t bytes; - unsigned char buffer[13] = {0}; - const size_t kBufSize = sizeof(buffer); - - EXPECT_EQ(stream->Read(buffer, kBufSize, &bytes, nullptr), SR_SUCCESS); - EXPECT_EQ(bytes, kBufSize); - EXPECT_TRUE(VerifyTestBuffer(buffer, kBufSize, value)); - EXPECT_TRUE(stream->GetPosition(&bytes)); - EXPECT_EQ(13U, bytes); - - EXPECT_TRUE(stream->SetPosition(7)); - - EXPECT_EQ(stream->Read(buffer, kBufSize, &bytes, nullptr), SR_SUCCESS); - EXPECT_EQ(bytes, kBufSize); - EXPECT_TRUE(VerifyTestBuffer(buffer, kBufSize, value + 7)); - EXPECT_TRUE(stream->GetPosition(&bytes)); - EXPECT_EQ(20U, bytes); -} - TEST(FifoBufferTest, TestAll) { const size_t kSize = 16; const char in[kSize * 2 + 1] = "0123456789ABCDEFGHIJKLMNOPQRSTUV";