Add convenience function that combines WTF::visit(...) with WTF::makeVisitor(...)
authorweinig@apple.com <weinig@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 20 Oct 2016 18:27:28 +0000 (18:27 +0000)
committerweinig@apple.com <weinig@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 20 Oct 2016 18:27:28 +0000 (18:27 +0000)
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()

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@207627 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WTF/ChangeLog
Source/WTF/wtf/Variant.h
Source/WebCore/ChangeLog
Source/WebCore/dom/MessageEvent.cpp
Source/WebCore/dom/Node.cpp
Source/WebCore/html/HTMLSelectElement.cpp
Source/WebCore/html/track/TrackEvent.cpp
Source/WebCore/testing/TypeConversions.h
Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WTF/Variant.cpp

index 494e9f2..30ec9d9 100644 (file)
@@ -1,3 +1,17 @@
+2016-10-19  Sam Weinig  <sam@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  <achristensen@webkit.org>
 
         Revert r207151
index b105236..6d4a739 100644 (file)
@@ -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 @@ struct __hash_visitor{
     }
 };
 
+// -- 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 {
index 1d59dc2..7a30c51 100644 (file)
@@ -1,3 +1,24 @@
+2016-10-20  Sam Weinig  <sam@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  <hyatt@apple.com>
 
         [CSS Parser] Fix font family parsing and add CSS region property parsing
index 803fa75..4e34ea6 100644 (file)
@@ -172,12 +172,10 @@ EventTarget* MessageEvent::source() const
     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)
index f327394..c39d940 100644 (file)
@@ -438,14 +438,12 @@ bool Node::appendChild(Node& newChild, ExceptionCode& ec)
 static HashSet<RefPtr<Node>> nodeSetPreTransformedFromNodeOrStringVector(const Vector<NodeOrString>& vector)
 {
     HashSet<RefPtr<Node>> nodeSet;
-
-    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);
+    for (const auto& variant : vector) {
+        WTF::switchOn(variant,
+            [&](const RefPtr<Node>& node) { nodeSet.add(const_cast<Node*>(node.get())); },
+            [](const String&) { }
+        );
+    }
 
     return nodeSet;
 }
@@ -475,14 +473,12 @@ RefPtr<Node> Node::convertNodesOrStringsIntoNode(Vector<NodeOrString>&& nodeOrSt
 
     Vector<Ref<Node>> nodes;
     nodes.reserveInitialCapacity(nodeOrStringVector.size());
-
-    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);
+    for (auto& variant : nodeOrStringVector) {
+        WTF::switchOn(variant,
+            [&](RefPtr<Node>& node) { nodes.uncheckedAppend(*node.get()); },
+            [&](String& string) { nodes.uncheckedAppend(Text::create(document(), string)); }
+        );
+    }
 
     if (nodes.size() == 1)
         return WTFMove(nodes.first());
index e80346d..75ed1fd 100644 (file)
@@ -225,16 +225,14 @@ ExceptionOr<void> HTMLSelectElement::add(const OptionOrOptGroupElement& element,
 {
     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;
index 72e4679..6ba40c3 100644 (file)
@@ -43,9 +43,9 @@ TrackEvent::TrackEvent(const AtomicString& type, const Init& initializer, IsTrus
     : 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(); }
+        );
     }
 }
 
index 14bb151..3d4e0ef 100644 (file)
@@ -111,11 +111,11 @@ public:
     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:
index a27565c..da2433a 100644 (file)
@@ -1,3 +1,13 @@
+2016-10-20  Sam Weinig  <sam@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.Fujii@sony.com>
 
         [CMake] CMake does not support the dep files for implicit dependency
index bfd95a2..b681561 100644 (file)
@@ -141,6 +141,44 @@ TEST(WTF_Variant, VisitorUsingMakeVisitor)
     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;