From 3485613f4ad2e766378c6a503879d04a8884bf89 Mon Sep 17 00:00:00 2001 From: AnotherTest Date: Fri, 15 May 2020 10:45:09 +0430 Subject: [PATCH] LibCore: Make IODevice::can_read_line() const This also makes LibHTTP's Job::can_read_line() const, as IODevice was keeping that from being const. Fixes #2219 --- Libraries/LibCore/IODevice.cpp | 4 ++-- Libraries/LibCore/IODevice.h | 17 ++++++++--------- Libraries/LibHTTP/HttpJob.cpp | 2 +- Libraries/LibHTTP/HttpJob.h | 2 +- Libraries/LibHTTP/HttpsJob.cpp | 2 +- Libraries/LibHTTP/HttpsJob.h | 2 +- Libraries/LibHTTP/Job.h | 3 +-- 7 files changed, 15 insertions(+), 17 deletions(-) diff --git a/Libraries/LibCore/IODevice.cpp b/Libraries/LibCore/IODevice.cpp index 3ee2ea0317..df69d3b182 100644 --- a/Libraries/LibCore/IODevice.cpp +++ b/Libraries/LibCore/IODevice.cpp @@ -121,7 +121,7 @@ bool IODevice::can_read_from_fd() const return FD_ISSET(m_fd, &rfds); } -bool IODevice::can_read_line() +bool IODevice::can_read_line() const { if (m_eof && !m_buffered_data.is_empty()) return true; @@ -206,7 +206,7 @@ ByteBuffer IODevice::read_line(size_t max_size) return {}; } -bool IODevice::populate_read_buffer() +bool IODevice::populate_read_buffer() const { if (m_fd < 0) return false; diff --git a/Libraries/LibCore/IODevice.h b/Libraries/LibCore/IODevice.h index 081e21f015..8c8ee6beb6 100644 --- a/Libraries/LibCore/IODevice.h +++ b/Libraries/LibCore/IODevice.h @@ -65,8 +65,7 @@ public: bool write(const u8*, int size); bool write(const StringView&); - // FIXME: I would like this to be const but currently it needs to call populate_read_buffer(). - bool can_read_line(); + bool can_read_line() const; bool can_read() const; @@ -88,20 +87,20 @@ protected: void set_fd(int); void set_mode(OpenMode mode) { m_mode = mode; } - void set_error(int error) { m_error = error; } - void set_eof(bool eof) { m_eof = eof; } + void set_error(int error) const { m_error = error; } + void set_eof(bool eof) const { m_eof = eof; } - virtual void did_update_fd(int) {} + virtual void did_update_fd(int) { } private: - bool populate_read_buffer(); + bool populate_read_buffer() const; bool can_read_from_fd() const; int m_fd { -1 }; - int m_error { 0 }; - bool m_eof { false }; OpenMode m_mode { NotOpen }; - Vector m_buffered_data; + mutable int m_error { 0 }; + mutable bool m_eof { false }; + mutable Vector m_buffered_data; }; } diff --git a/Libraries/LibHTTP/HttpJob.cpp b/Libraries/LibHTTP/HttpJob.cpp index b8ca8b413d..1ebc5a8d45 100644 --- a/Libraries/LibHTTP/HttpJob.cpp +++ b/Libraries/LibHTTP/HttpJob.cpp @@ -73,7 +73,7 @@ void HttpJob::register_on_ready_to_write(Function callback) callback(); } -bool HttpJob::can_read_line() +bool HttpJob::can_read_line() const { return m_socket->can_read_line(); } diff --git a/Libraries/LibHTTP/HttpJob.h b/Libraries/LibHTTP/HttpJob.h index 1bd679d6ff..9eff924e34 100644 --- a/Libraries/LibHTTP/HttpJob.h +++ b/Libraries/LibHTTP/HttpJob.h @@ -53,7 +53,7 @@ public: protected: virtual void register_on_ready_to_read(Function) override; virtual void register_on_ready_to_write(Function) override; - virtual bool can_read_line() override; + virtual bool can_read_line() const override; virtual ByteBuffer read_line(size_t) override; virtual bool can_read() const override; virtual ByteBuffer receive(size_t) override; diff --git a/Libraries/LibHTTP/HttpsJob.cpp b/Libraries/LibHTTP/HttpsJob.cpp index 5ec2c357d0..d5495b5de0 100644 --- a/Libraries/LibHTTP/HttpsJob.cpp +++ b/Libraries/LibHTTP/HttpsJob.cpp @@ -104,7 +104,7 @@ void HttpsJob::register_on_ready_to_write(Function callback) }; } -bool HttpsJob::can_read_line() +bool HttpsJob::can_read_line() const { return m_socket->can_read_line(); } diff --git a/Libraries/LibHTTP/HttpsJob.h b/Libraries/LibHTTP/HttpsJob.h index f426c9803e..eb8dac4f63 100644 --- a/Libraries/LibHTTP/HttpsJob.h +++ b/Libraries/LibHTTP/HttpsJob.h @@ -53,7 +53,7 @@ public: protected: virtual void register_on_ready_to_read(Function) override; virtual void register_on_ready_to_write(Function) override; - virtual bool can_read_line() override; + virtual bool can_read_line() const override; virtual ByteBuffer read_line(size_t) override; virtual bool can_read() const override; virtual ByteBuffer receive(size_t) override; diff --git a/Libraries/LibHTTP/Job.h b/Libraries/LibHTTP/Job.h index 9631dde4ae..2cc3f55937 100644 --- a/Libraries/LibHTTP/Job.h +++ b/Libraries/LibHTTP/Job.h @@ -51,8 +51,7 @@ protected: void on_socket_connected(); virtual void register_on_ready_to_read(Function) = 0; virtual void register_on_ready_to_write(Function) = 0; - // FIXME: I want const but Core::IODevice::can_read_line populates a cache with this - virtual bool can_read_line() = 0; + virtual bool can_read_line() const = 0; virtual ByteBuffer read_line(size_t) = 0; virtual bool can_read() const = 0; virtual ByteBuffer receive(size_t) = 0;