Selaa lähdekoodia

AK+LibURL: Move CopyOnWrite<T> from LibURL to AK

Andreas Kling 9 kuukautta sitten
vanhempi
commit
af68771dda
2 muutettua tiedostoa jossa 43 lisäystä ja 29 poistoa
  1. 41 0
      AK/CopyOnWrite.h
  2. 2 29
      Userland/Libraries/LibURL/URL.h

+ 41 - 0
AK/CopyOnWrite.h

@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2024, Andreas Kling <andreas@ladybird.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/NonnullRefPtr.h>
+
+namespace AK {
+
+template<typename T>
+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<T> m_value;
+};
+
+}

+ 2 - 29
Userland/Libraries/LibURL/URL.h

@@ -9,6 +9,7 @@
 #pragma once
 #pragma once
 
 
 #include <AK/ByteString.h>
 #include <AK/ByteString.h>
+#include <AK/CopyOnWrite.h>
 #include <AK/String.h>
 #include <AK/String.h>
 #include <AK/StringView.h>
 #include <AK/StringView.h>
 #include <AK/Vector.h>
 #include <AK/Vector.h>
@@ -73,34 +74,6 @@ enum class SpaceAsPlus {
 String percent_encode(StringView input, PercentEncodeSet set = PercentEncodeSet::Userinfo, SpaceAsPlus = SpaceAsPlus::No);
 String percent_encode(StringView input, PercentEncodeSet set = PercentEncodeSet::Userinfo, SpaceAsPlus = SpaceAsPlus::No);
 ByteString percent_decode(StringView input);
 ByteString percent_decode(StringView input);
 
 
-template<typename T>
-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<T> m_value;
-};
-
 // https://url.spec.whatwg.org/#url-representation
 // 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.
 // 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 {
 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.
         // A URL also has an associated blob URL entry that is either null or a blob URL entry. It is initially null.
         Optional<BlobURLEntry> blob_url_entry;
         Optional<BlobURLEntry> blob_url_entry;
     };
     };
-    CopyOnWrite<Data> m_data;
+    AK::CopyOnWrite<Data> m_data;
 };
 };
 
 
 URL create_with_url_or_path(ByteString const&);
 URL create_with_url_or_path(ByteString const&);