From af68771dda8e28a27d93933d25f141e4752d35ad Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 10 Sep 2024 11:05:56 +0200 Subject: [PATCH] AK+LibURL: Move CopyOnWrite from LibURL to AK --- AK/CopyOnWrite.h | 41 +++++++++++++++++++++++++++++++++ Userland/Libraries/LibURL/URL.h | 31 ++----------------------- 2 files changed, 43 insertions(+), 29 deletions(-) create mode 100644 AK/CopyOnWrite.h diff --git a/AK/CopyOnWrite.h b/AK/CopyOnWrite.h new file mode 100644 index 00000000000..6961ab86b0a --- /dev/null +++ b/AK/CopyOnWrite.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2024, Andreas Kling + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include + +namespace AK { + +template +class CopyOnWrite { +public: + CopyOnWrite() + : m_value(adopt_ref(*new T)) + { + } + T& mutable_value() + { + if (m_value->ref_count() > 1) + m_value = m_value->clone(); + return *m_value; + } + T const& value() const { return *m_value; } + + operator T const&() const { return value(); } + operator T&() { return mutable_value(); } + + T const* operator->() const { return &value(); } + T* operator->() { return &mutable_value(); } + + T const* ptr() const { return m_value.ptr(); } + T* ptr() { return m_value.ptr(); } + +private: + NonnullRefPtr m_value; +}; + +} diff --git a/Userland/Libraries/LibURL/URL.h b/Userland/Libraries/LibURL/URL.h index 2d31f0ef962..08edf58da71 100644 --- a/Userland/Libraries/LibURL/URL.h +++ b/Userland/Libraries/LibURL/URL.h @@ -9,6 +9,7 @@ #pragma once #include +#include #include #include #include @@ -73,34 +74,6 @@ enum class SpaceAsPlus { String percent_encode(StringView input, PercentEncodeSet set = PercentEncodeSet::Userinfo, SpaceAsPlus = SpaceAsPlus::No); ByteString percent_decode(StringView input); -template -class CopyOnWrite { -public: - CopyOnWrite() - : m_value(adopt_ref(*new T)) - { - } - T& mutable_value() - { - if (m_value->ref_count() > 1) - m_value = m_value->clone(); - return *m_value; - } - T const& value() const { return *m_value; } - - operator T const&() const { return value(); } - operator T&() { return mutable_value(); } - - T const* operator->() const { return &value(); } - T* operator->() { return &mutable_value(); } - - T const* ptr() const { return m_value.ptr(); } - T* ptr() { return m_value.ptr(); } - -private: - NonnullRefPtr m_value; -}; - // https://url.spec.whatwg.org/#url-representation // A URL is a struct that represents a universal identifier. To disambiguate from a valid URL string it can also be referred to as a URL record. class URL { @@ -233,7 +206,7 @@ private: // A URL also has an associated blob URL entry that is either null or a blob URL entry. It is initially null. Optional blob_url_entry; }; - CopyOnWrite m_data; + AK::CopyOnWrite m_data; }; URL create_with_url_or_path(ByteString const&);