Title: [207627] trunk
Revision
207627
Author
wei...@apple.com
Date
2016-10-20 11:27:28 -0700 (Thu, 20 Oct 2016)

Log Message

Add convenience function that combines WTF::visit(...) with WTF::makeVisitor(...)
https://bugs.webkit.org/show_bug.cgi?id=163713

Reviewed by Dan Bernstein.

Source/WebCore:

Switch uses of WTF::visit to use WTF::switchOn.

* dom/MessageEvent.cpp:
(WebCore::MessageEvent::source):
* dom/Node.cpp:
(WebCore::nodeSetPreTransformedFromNodeOrStringVector):
(WebCore::Node::convertNodesOrStringsIntoNode):
* html/HTMLSelectElement.cpp:
(WebCore::HTMLSelectElement::add):
* html/track/TrackEvent.cpp:
(WebCore::TrackEvent::TrackEvent):
* testing/TypeConversions.h:
(WebCore::TypeConversions::typeConversionsDictionaryUnionType):

Source/WTF:

- Add WTF::switchOn which merges WTF::visit with WTF::makeVisitor in the following
  way:
        WTF::visit(WTF::makeVisitor(...), variant)

* wtf/Variant.h:
(WTF::switchOn):

Tools:

* TestWebKitAPI/Tests/WTF/Variant.cpp:
Add test for WTF::switchOn()

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (207626 => 207627)


--- trunk/Source/WTF/ChangeLog	2016-10-20 18:06:29 UTC (rev 207626)
+++ trunk/Source/WTF/ChangeLog	2016-10-20 18:27:28 UTC (rev 207627)
@@ -1,3 +1,17 @@
+2016-10-19  Sam Weinig  <s...@webkit.org>
+
+        Add convenience function that combines WTF::visit(...) with WTF::makeVisitor(...)
+        https://bugs.webkit.org/show_bug.cgi?id=163713
+
+        Reviewed by Dan Bernstein.
+
+        - Add WTF::switchOn which merges WTF::visit with WTF::makeVisitor in the following
+          way:
+                WTF::visit(WTF::makeVisitor(...), variant)
+
+        * wtf/Variant.h:
+        (WTF::switchOn):
+
 2016-10-19  Alex Christensen  <achristen...@webkit.org>
 
         Revert r207151

Modified: trunk/Source/WTF/wtf/Variant.h (207626 => 207627)


--- trunk/Source/WTF/wtf/Variant.h	2016-10-20 18:06:29 UTC (rev 207626)
+++ trunk/Source/WTF/wtf/Variant.h	2016-10-20 18:27:28 UTC (rev 207627)
@@ -48,6 +48,7 @@
 #include <type_traits>
 #include <utility>
 #include <wtf/Compiler.h>
+#include <wtf/StdLibExtras.h>
 
 #if COMPILER(MSVC)
 #pragma warning(push)
@@ -2040,6 +2041,14 @@
     }
 };
 
+// -- WebKit Additions --
+
+template<class V, class... F>
+auto switchOn(V&& v, F&&... f) -> decltype(visit(makeVisitor(std::forward<F>(f)...), std::forward<V>(v)))
+{
+    return visit(makeVisitor(std::forward<F>(f)...), std::forward<V>(v));
+}
+
 } // namespace WTF
 
 namespace std {

Modified: trunk/Source/WebCore/ChangeLog (207626 => 207627)


--- trunk/Source/WebCore/ChangeLog	2016-10-20 18:06:29 UTC (rev 207626)
+++ trunk/Source/WebCore/ChangeLog	2016-10-20 18:27:28 UTC (rev 207627)
@@ -1,3 +1,24 @@
+2016-10-20  Sam Weinig  <s...@webkit.org>
+
+        Add convenience function that combines WTF::visit(...) with WTF::makeVisitor(...)
+        https://bugs.webkit.org/show_bug.cgi?id=163713
+
+        Reviewed by Dan Bernstein.
+
+        Switch uses of WTF::visit to use WTF::switchOn.
+
+        * dom/MessageEvent.cpp:
+        (WebCore::MessageEvent::source):
+        * dom/Node.cpp:
+        (WebCore::nodeSetPreTransformedFromNodeOrStringVector):
+        (WebCore::Node::convertNodesOrStringsIntoNode):
+        * html/HTMLSelectElement.cpp:
+        (WebCore::HTMLSelectElement::add):
+        * html/track/TrackEvent.cpp:
+        (WebCore::TrackEvent::TrackEvent):
+        * testing/TypeConversions.h:
+        (WebCore::TypeConversions::typeConversionsDictionaryUnionType):
+
 2016-10-20  Dave Hyatt  <hy...@apple.com>
 
         [CSS Parser] Fix font family parsing and add CSS region property parsing

Modified: trunk/Source/WebCore/dom/MessageEvent.cpp (207626 => 207627)


--- trunk/Source/WebCore/dom/MessageEvent.cpp	2016-10-20 18:06:29 UTC (rev 207626)
+++ trunk/Source/WebCore/dom/MessageEvent.cpp	2016-10-20 18:27:28 UTC (rev 207627)
@@ -172,12 +172,10 @@
     if (!m_source)
         return nullptr;
 
-    auto visitor = WTF::makeVisitor(
+    return WTF::switchOn(m_source.value(),
         [](const RefPtr<DOMWindow>& window) -> EventTarget* { return const_cast<EventTarget*>(static_cast<const EventTarget*>(window.get())); },
         [](const RefPtr<MessagePort>& messagePort) -> EventTarget* { return const_cast<EventTarget*>(static_cast<const EventTarget*>(messagePort.get())); }
     );
-
-    return WTF::visit(visitor, m_source.value());
 }
 
 RefPtr<SerializedScriptValue> MessageEvent::trySerializeData(ExecState* exec)

Modified: trunk/Source/WebCore/dom/Node.cpp (207626 => 207627)


--- trunk/Source/WebCore/dom/Node.cpp	2016-10-20 18:06:29 UTC (rev 207626)
+++ trunk/Source/WebCore/dom/Node.cpp	2016-10-20 18:27:28 UTC (rev 207627)
@@ -438,15 +438,13 @@
 static HashSet<RefPtr<Node>> nodeSetPreTransformedFromNodeOrStringVector(const Vector<NodeOrString>& vector)
 {
     HashSet<RefPtr<Node>> nodeSet;
+    for (const auto& variant : vector) {
+        WTF::switchOn(variant,
+            [&](const RefPtr<Node>& node) { nodeSet.add(const_cast<Node*>(node.get())); },
+            [](const String&) { }
+        );
+    }
 
-    auto visitor = WTF::makeVisitor(
-        [&](const RefPtr<Node>& node) { nodeSet.add(const_cast<Node*>(node.get())); },
-        [](const String&) { }
-    );
-
-    for (const auto& variant : vector)
-        WTF::visit(visitor, variant);
-
     return nodeSet;
 }
 
@@ -475,15 +473,13 @@
 
     Vector<Ref<Node>> nodes;
     nodes.reserveInitialCapacity(nodeOrStringVector.size());
+    for (auto& variant : nodeOrStringVector) {
+        WTF::switchOn(variant,
+            [&](RefPtr<Node>& node) { nodes.uncheckedAppend(*node.get()); },
+            [&](String& string) { nodes.uncheckedAppend(Text::create(document(), string)); }
+        );
+    }
 
-    auto visitor = WTF::makeVisitor(
-        [&](RefPtr<Node>& node) { nodes.uncheckedAppend(*node.get()); },
-        [&](String& string) { nodes.uncheckedAppend(Text::create(document(), string)); }
-    );
-
-    for (auto& variant : nodeOrStringVector)
-        WTF::visit(visitor, variant);
-
     if (nodes.size() == 1)
         return WTFMove(nodes.first());
 

Modified: trunk/Source/WebCore/html/HTMLSelectElement.cpp (207626 => 207627)


--- trunk/Source/WebCore/html/HTMLSelectElement.cpp	2016-10-20 18:06:29 UTC (rev 207626)
+++ trunk/Source/WebCore/html/HTMLSelectElement.cpp	2016-10-20 18:27:28 UTC (rev 207627)
@@ -225,16 +225,14 @@
 {
     HTMLElement* beforeElement = nullptr;
     if (before) {
-        auto visitor = WTF::makeVisitor(
+        beforeElement = WTF::switchOn(before.value(),
             [](const RefPtr<HTMLElement>& element) -> HTMLElement* { return element.get(); },
             [this](int index) -> HTMLElement* { return item(index); }
         );
-
-        beforeElement = WTF::visit(visitor, before.value());
     }
-    HTMLElement& toInsert = WTF::visit([](const auto& htmlElement) -> HTMLElement& {
-        return *htmlElement;
-    }, element);
+    HTMLElement& toInsert = WTF::switchOn(element,
+        [](const auto& htmlElement) -> HTMLElement& { return *htmlElement; }
+    );
 
 
     ExceptionCode ec = 0;

Modified: trunk/Source/WebCore/html/track/TrackEvent.cpp (207626 => 207627)


--- trunk/Source/WebCore/html/track/TrackEvent.cpp	2016-10-20 18:06:29 UTC (rev 207626)
+++ trunk/Source/WebCore/html/track/TrackEvent.cpp	2016-10-20 18:27:28 UTC (rev 207627)
@@ -43,9 +43,9 @@
     : Event(type, initializer, isTrusted)
 {
     if (initializer.track) {
-        m_track = WTF::visit([](const auto& trackbase) -> TrackBase* {
-            return trackbase.get();
-        }, *initializer.track);
+        m_track = WTF::switchOn(*initializer.track,
+            [](const auto& trackbase) -> TrackBase* { return trackbase.get(); }
+        );
     }
 }
 

Modified: trunk/Source/WebCore/testing/TypeConversions.h (207626 => 207627)


--- trunk/Source/WebCore/testing/TypeConversions.h	2016-10-20 18:06:29 UTC (rev 207626)
+++ trunk/Source/WebCore/testing/TypeConversions.h	2016-10-20 18:27:28 UTC (rev 207627)
@@ -111,11 +111,11 @@
     Vector<String> typeConversionsDictionarySequenceValue() { return m_typeConversionsDictionarySequenceValue; }
     UnionType typeConversionsDictionaryUnionType()
     {
-        return WTF::visit(WTF::makeVisitor(
+        return WTF::switchOn(m_typeConversionsDictionaryUnionValue,
             [](const RefPtr<Node>&) -> UnionType { return UnionType::Node; },
             [](const Vector<String>&) -> UnionType { return UnionType::Sequence; },
             [](const OtherDictionary&) -> UnionType { return UnionType::Dictionary; }
-        ), m_typeConversionsDictionaryUnionValue);
+        );
     }
 
 private:

Modified: trunk/Tools/ChangeLog (207626 => 207627)


--- trunk/Tools/ChangeLog	2016-10-20 18:06:29 UTC (rev 207626)
+++ trunk/Tools/ChangeLog	2016-10-20 18:27:28 UTC (rev 207627)
@@ -1,3 +1,13 @@
+2016-10-20  Sam Weinig  <s...@webkit.org>
+
+        Add convenience function that combines WTF::visit(...) with WTF::makeVisitor(...)
+        https://bugs.webkit.org/show_bug.cgi?id=163713
+
+        Reviewed by Dan Bernstein.
+
+        * TestWebKitAPI/Tests/WTF/Variant.cpp:
+        Add test for WTF::switchOn()
+
 2016-10-20  Fujii Hironori  <hironori.fu...@sony.com>
 
         [CMake] CMake does not support the dep files for implicit dependency

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/Variant.cpp (207626 => 207627)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/Variant.cpp	2016-10-20 18:06:29 UTC (rev 207626)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/Variant.cpp	2016-10-20 18:27:28 UTC (rev 207627)
@@ -141,6 +141,44 @@
     EXPECT_TRUE(Type::String == type);
 }
 
+TEST(WTF_Variant, VisitorUsingSwitchOn)
+{
+    enum class Type {
+        None,
+        Int,
+        Float,
+        String,
+    };
+
+    Type type = Type::None;
+
+    Variant<int, float, String> variant = 8;
+    type = WTF::switchOn(variant,
+        [](int) { return Type::Int; },
+        [](float) { return Type::Float; },
+        [](String) { return Type::String; }
+    );
+    EXPECT_TRUE(Type::Int == type);
+
+
+    variant = 1.0f;
+    type = WTF::switchOn(variant,
+        [](int) { return Type::Int; },
+        [](float) { return Type::Float; },
+        [](String) { return Type::String; }
+    );
+    EXPECT_TRUE(Type::Float == type);
+
+
+    variant = "hello";
+    type = WTF::switchOn(variant,
+        [](int) { return Type::Int; },
+        [](float) { return Type::Float; },
+        [](String) { return Type::String; }
+    );
+    EXPECT_TRUE(Type::String == type);
+}
+
 TEST(WTF_Variant, ConstructorDestructor)
 {
     ConstructorDestructorCounter::TestingScope scope;
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to