Use WTF::Function instead of std::function in WTF/
authorcdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 18 Jun 2017 19:49:12 +0000 (19:49 +0000)
committercdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 18 Jun 2017 19:49:12 +0000 (19:49 +0000)
https://bugs.webkit.org/show_bug.cgi?id=173519

Reviewed by Sam Weinig.

Source/WebCore:

Replace a few uses of std::function with WTF::Function in WebCore/
as well. It was either this or including <functional> and I decided
it made more sense to port the code.

* platform/graphics/FontSelectionAlgorithm.h:
(WebCore::FontSelectionAlgorithm::iterateActiveCapabilitiesWithReturn):
* platform/mediastream/MediaConstraints.cpp:
(WebCore::StringConstraint::find):
(WebCore::MediaTrackConstraintSetMap::forEach):
(WebCore::MediaTrackConstraintSetMap::filter):
(WebCore::MediaConstraints::isConstraintSet):
* platform/mediastream/MediaConstraints.h:
(WebCore::NumericConstraint::find):
* platform/mediastream/RealtimeMediaSource.cpp:
(WebCore::RealtimeMediaSource::applyConstraint):

Source/WTF:

Use WTF::Function instead of std::function in WTF/ to avoid
copying.

* wtf/Brigand.h:
* wtf/Condition.h:
* wtf/Expected.h:
* wtf/FunctionDispatcher.h:
* wtf/MainThread.h:
* wtf/MemoryPressureHandler.h:
(WTF::MemoryPressureHandler::setMemoryKillCallback):
(WTF::MemoryPressureHandler::setMemoryPressureStatusChangedCallback):
(WTF::MemoryPressureHandler::setDidExceedInactiveLimitWhileActiveCallback):
* wtf/Optional.h:
* wtf/ParkingLot.h:
* wtf/RefCounter.h:
(WTF::RefCounter<T>::RefCounter):
* wtf/WorkQueue.h:
* wtf/linux/MemoryPressureHandlerLinux.cpp:
(WTF::MemoryPressureHandler::EventFDPoller::EventFDPoller):
* wtf/text/WTFString.cpp:
(WTF::String::split):
* wtf/text/WTFString.h:

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

19 files changed:
Source/WTF/ChangeLog
Source/WTF/wtf/Brigand.h
Source/WTF/wtf/Condition.h
Source/WTF/wtf/Expected.h
Source/WTF/wtf/FunctionDispatcher.h
Source/WTF/wtf/MainThread.h
Source/WTF/wtf/MemoryPressureHandler.h
Source/WTF/wtf/Optional.h
Source/WTF/wtf/ParkingLot.h
Source/WTF/wtf/RefCounter.h
Source/WTF/wtf/WorkQueue.h
Source/WTF/wtf/linux/MemoryPressureHandlerLinux.cpp
Source/WTF/wtf/text/WTFString.cpp
Source/WTF/wtf/text/WTFString.h
Source/WebCore/ChangeLog
Source/WebCore/platform/graphics/FontSelectionAlgorithm.h
Source/WebCore/platform/mediastream/MediaConstraints.cpp
Source/WebCore/platform/mediastream/MediaConstraints.h
Source/WebCore/platform/mediastream/RealtimeMediaSource.cpp

index 277a803..1c1f288 100644 (file)
@@ -1,3 +1,33 @@
+2017-06-18  Chris Dumez  <cdumez@apple.com>
+
+        Use WTF::Function instead of std::function in WTF/
+        https://bugs.webkit.org/show_bug.cgi?id=173519
+
+        Reviewed by Sam Weinig.
+
+        Use WTF::Function instead of std::function in WTF/ to avoid
+        copying.
+
+        * wtf/Brigand.h:
+        * wtf/Condition.h:
+        * wtf/Expected.h:
+        * wtf/FunctionDispatcher.h:
+        * wtf/MainThread.h:
+        * wtf/MemoryPressureHandler.h:
+        (WTF::MemoryPressureHandler::setMemoryKillCallback):
+        (WTF::MemoryPressureHandler::setMemoryPressureStatusChangedCallback):
+        (WTF::MemoryPressureHandler::setDidExceedInactiveLimitWhileActiveCallback):
+        * wtf/Optional.h:
+        * wtf/ParkingLot.h:
+        * wtf/RefCounter.h:
+        (WTF::RefCounter<T>::RefCounter):
+        * wtf/WorkQueue.h:
+        * wtf/linux/MemoryPressureHandlerLinux.cpp:
+        (WTF::MemoryPressureHandler::EventFDPoller::EventFDPoller):
+        * wtf/text/WTFString.cpp:
+        (WTF::String::split):
+        * wtf/text/WTFString.h:
+
 2017-06-16  Alex Christensen  <achristensen@webkit.org>
 
         Fix Mac CMake build.
index 2000a82..2c5c6d5 100644 (file)
@@ -51,7 +51,6 @@
 #include <cstddef>
 #include <cstdint>
 #include <cstring>
-#include <functional>
 #include <initializer_list>
 #include <tuple>
 #include <type_traits>
index bb0c9a5..9444f94 100644 (file)
@@ -26,7 +26,6 @@
 #ifndef WTF_Condition_h
 #define WTF_Condition_h
 
-#include <functional>
 #include <wtf/CurrentTime.h>
 #include <wtf/Noncopyable.h>
 #include <wtf/ParkingLot.h>
index 39c7e87..442d3ca 100644 (file)
@@ -29,7 +29,6 @@
 #pragma once
 
 #include <cstdlib>
-#include <functional>
 #include <initializer_list>
 #include <type_traits>
 #include <utility>
index 344a160..f90c7bc 100644 (file)
@@ -26,7 +26,6 @@
 #ifndef FunctionDispatcher_h
 #define FunctionDispatcher_h
 
-#include <functional>
 #include <wtf/Function.h>
 #include <wtf/ThreadSafeRefCounted.h>
 
index 0e78e8e..14b3467 100644 (file)
@@ -30,7 +30,6 @@
 #ifndef MainThread_h
 #define MainThread_h
 
-#include <functional>
 #include <stdint.h>
 #include <wtf/Function.h>
 #include <wtf/Optional.h>
index 0893f4f..cb16ea9 100644 (file)
@@ -28,9 +28,9 @@
 
 #include <atomic>
 #include <ctime>
-#include <functional>
 #include <wtf/FastMalloc.h>
 #include <wtf/Forward.h>
+#include <wtf/Function.h>
 #include <wtf/NeverDestroyed.h>
 #include <wtf/Optional.h>
 #include <wtf/RunLoop.h>
@@ -59,7 +59,7 @@ enum class WebsamProcessState {
 enum class Critical { No, Yes };
 enum class Synchronous { No, Yes };
 
-typedef std::function<void(Critical, Synchronous)> LowMemoryHandler;
+typedef WTF::Function<void(Critical, Synchronous)> LowMemoryHandler;
 
 class MemoryPressureHandler {
     friend class WTF::NeverDestroyed<MemoryPressureHandler>;
@@ -70,9 +70,9 @@ public:
 
     WTF_EXPORT_PRIVATE void setShouldUsePeriodicMemoryMonitor(bool);
 
-    void setMemoryKillCallback(WTF::Function<void()> function) { m_memoryKillCallback = WTFMove(function); }
-    void setMemoryPressureStatusChangedCallback(WTF::Function<void(bool)> function) { m_memoryPressureStatusChangedCallback = WTFMove(function); }
-    void setDidExceedInactiveLimitWhileActiveCallback(WTF::Function<void()> function) { m_didExceedInactiveLimitWhileActiveCallback = WTFMove(function); }
+    void setMemoryKillCallback(WTF::Function<void()>&& function) { m_memoryKillCallback = WTFMove(function); }
+    void setMemoryPressureStatusChangedCallback(WTF::Function<void(bool)>&& function) { m_memoryPressureStatusChangedCallback = WTFMove(function); }
+    void setDidExceedInactiveLimitWhileActiveCallback(WTF::Function<void()>&& function) { m_didExceedInactiveLimitWhileActiveCallback = WTFMove(function); }
 
     void setLowMemoryHandler(LowMemoryHandler&& handler)
     {
@@ -172,14 +172,14 @@ private:
     class EventFDPoller {
         WTF_MAKE_NONCOPYABLE(EventFDPoller); WTF_MAKE_FAST_ALLOCATED;
     public:
-        EventFDPoller(int fd, std::function<void ()>&& notifyHandler);
+        EventFDPoller(int fd, WTF::Function<void ()>&& notifyHandler);
         ~EventFDPoller();
 
     private:
         void readAndNotify() const;
 
         std::optional<int> m_fd;
-        std::function<void ()> m_notifyHandler;
+        WTF::Function<void ()> m_notifyHandler;
 #if USE(GLIB)
         GRefPtr<GSource> m_source;
 #else
index 2b3c7c1..4189cb1 100644 (file)
@@ -41,7 +41,6 @@
 # include <type_traits>
 # include <initializer_list>
 # include <cassert>
-# include <functional>
 # include <string>
 # include <stdexcept>
 # include <wtf/Assertions.h>
index b6d8830..b38ff30 100644 (file)
@@ -26,7 +26,6 @@
 #ifndef WTF_ParkingLot_h
 #define WTF_ParkingLot_h
 
-#include <functional>
 #include <wtf/Atomics.h>
 #include <wtf/ScopedLambda.h>
 #include <wtf/Threading.h>
index b1aad56..05f28f4 100644 (file)
@@ -26,7 +26,7 @@
 #ifndef RefCounter_h
 #define RefCounter_h
 
-#include <functional>
+#include <wtf/Function.h>
 #include <wtf/Noncopyable.h>
 #include <wtf/RefPtr.h>
 
@@ -59,9 +59,9 @@ class RefCounter {
 
 public:
     using Token = RefPtr<Count>;
-    using ValueChangeFunction = std::function<void (RefCounterEvent)>;
+    using ValueChangeFunction = WTF::Function<void (RefCounterEvent)>;
 
-    RefCounter(ValueChangeFunction = nullptr);
+    RefCounter(ValueChangeFunction&& = nullptr);
     ~RefCounter();
 
     Token count() const
@@ -105,8 +105,8 @@ inline void RefCounter<T>::Count::deref()
 }
 
 template<typename T>
-inline RefCounter<T>::RefCounter(ValueChangeFunction valueDidChange)
-    : m_valueDidChange(valueDidChange)
+inline RefCounter<T>::RefCounter(ValueChangeFunction&& valueDidChange)
+    : m_valueDidChange(WTFMove(valueDidChange))
     , m_count(new Count(*this))
 {
 }
index 23198d4..2432710 100644 (file)
@@ -27,7 +27,6 @@
 #ifndef WorkQueue_h
 #define WorkQueue_h
 
-#include <functional>
 #include <wtf/Forward.h>
 #include <wtf/FunctionDispatcher.h>
 #include <wtf/RefCounted.h>
index e3aac59..15a5b12 100644 (file)
@@ -99,7 +99,7 @@ static GSourceFuncs eventFDSourceFunctions = {
 };
 #endif
 
-MemoryPressureHandler::EventFDPoller::EventFDPoller(int fd, std::function<void ()>&& notifyHandler)
+MemoryPressureHandler::EventFDPoller::EventFDPoller(int fd, WTF::Function<void ()>&& notifyHandler)
     : m_fd(fd)
     , m_notifyHandler(WTFMove(notifyHandler))
 {
index 042d432..dbe6eea 100644 (file)
@@ -743,7 +743,7 @@ void String::split(const String& separator, bool allowEmptyEntries, Vector<Strin
         result.append(substring(startPos));
 }
 
-void String::split(UChar separator, bool allowEmptyEntries, SplitFunctor&& functor) const
+void String::split(UChar separator, bool allowEmptyEntries, const SplitFunctor& functor) const
 {
     StringView view(*this);
 
index ba8e611..70c074a 100644 (file)
@@ -25,8 +25,7 @@
 // This file would be called String.h, but that conflicts with <string.h>
 // on systems without case-sensitive file systems.
 
-#include <functional>
-
+#include <wtf/Function.h>
 #include <wtf/text/ASCIIFastPath.h>
 #include <wtf/text/IntegerToStringConversion.h>
 #include <wtf/text/StringImpl.h>
@@ -367,8 +366,8 @@ public:
         split(separator, false, result);
     }
 
-    using SplitFunctor = std::function<void(const StringView&)>;
-    WTF_EXPORT_STRING_API void split(UChar separator, bool allowEmptyEntries, SplitFunctor&&) const;
+    using SplitFunctor = WTF::Function<void(const StringView&)>;
+    WTF_EXPORT_STRING_API void split(UChar separator, bool allowEmptyEntries, const SplitFunctor&) const;
     WTF_EXPORT_STRING_API void split(UChar separator, bool allowEmptyEntries, Vector<String>& result) const;
     void split(UChar separator, Vector<String>& result) const
     {
index 01bdc51..300cbaf 100644 (file)
@@ -1,3 +1,26 @@
+2017-06-18  Chris Dumez  <cdumez@apple.com>
+
+        Use WTF::Function instead of std::function in WTF/
+        https://bugs.webkit.org/show_bug.cgi?id=173519
+
+        Reviewed by Sam Weinig.
+
+        Replace a few uses of std::function with WTF::Function in WebCore/
+        as well. It was either this or including <functional> and I decided
+        it made more sense to port the code.
+
+        * platform/graphics/FontSelectionAlgorithm.h:
+        (WebCore::FontSelectionAlgorithm::iterateActiveCapabilitiesWithReturn):
+        * platform/mediastream/MediaConstraints.cpp:
+        (WebCore::StringConstraint::find):
+        (WebCore::MediaTrackConstraintSetMap::forEach):
+        (WebCore::MediaTrackConstraintSetMap::filter):
+        (WebCore::MediaConstraints::isConstraintSet):
+        * platform/mediastream/MediaConstraints.h:
+        (WebCore::NumericConstraint::find):
+        * platform/mediastream/RealtimeMediaSource.cpp:
+        (WebCore::RealtimeMediaSource::applyConstraint):
+
 2017-06-18  Jer Noble  <jer.noble@apple.com>
 
         [MSE] Seeking or entering fullscreen can cause extreme CPU usage
index b2e147d..a263a4c 100644 (file)
@@ -26,6 +26,7 @@
 #pragma once
 
 #include "TextFlags.h"
+#include <wtf/Function.h>
 #include <wtf/GetPtr.h>
 #include <wtf/Hasher.h>
 #include <wtf/NeverDestroyed.h>
@@ -513,10 +514,10 @@ public:
 
 private:
     template <typename T>
-    using IterateActiveCapabilitiesWithReturnCallback = std::function<std::optional<T>(FontSelectionCapabilities, size_t)>;
+    using IterateActiveCapabilitiesWithReturnCallback = WTF::Function<std::optional<T>(FontSelectionCapabilities, size_t)>;
 
     template <typename T>
-    inline std::optional<T> iterateActiveCapabilitiesWithReturn(IterateActiveCapabilitiesWithReturnCallback<T> callback)
+    inline std::optional<T> iterateActiveCapabilitiesWithReturn(const IterateActiveCapabilitiesWithReturnCallback<T>& callback)
     {
         for (size_t i = 0; i < m_capabilities.size(); ++i) {
             if (!m_filter[i])
index ebe6035..01d291e 100644 (file)
@@ -38,7 +38,7 @@
 
 namespace WebCore {
 
-const String& StringConstraint::find(std::function<bool(const String&)> filter) const
+const String& StringConstraint::find(const WTF::Function<bool(const String&)>& filter) const
 {
     for (auto& constraint : m_exact) {
         if (filter(constraint))
@@ -182,15 +182,15 @@ const MediaConstraint* FlattenedConstraint::find(MediaConstraintType type) const
     return nullptr;
 }
 
-void MediaTrackConstraintSetMap::forEach(std::function<void(const MediaConstraint&)> callback) const
+void MediaTrackConstraintSetMap::forEach(WTF::Function<void(const MediaConstraint&)>&& callback) const
 {
-    filter([callback] (const MediaConstraint& constraint) mutable {
+    filter([callback = WTFMove(callback)] (const MediaConstraint& constraint) mutable {
         callback(constraint);
         return false;
     });
 }
 
-void MediaTrackConstraintSetMap::filter(std::function<bool(const MediaConstraint&)> callback) const
+void MediaTrackConstraintSetMap::filter(const WTF::Function<bool(const MediaConstraint&)>& callback) const
 {
     if (m_width && !m_width->isEmpty() && callback(*m_width))
         return;
@@ -363,7 +363,7 @@ static inline void addDefaultVideoConstraints(MediaTrackConstraintSetMap& videoC
     }
 }
 
-bool MediaConstraints::isConstraintSet(std::function<bool(const MediaTrackConstraintSetMap&)>&& callback)
+bool MediaConstraints::isConstraintSet(const WTF::Function<bool(const MediaTrackConstraintSetMap&)>& callback)
 {
     if (callback(mandatoryConstraints))
         return true;
index 3c4864d..bffa05e 100644 (file)
@@ -35,6 +35,7 @@
 
 #include "RealtimeMediaSourceSupportedConstraints.h"
 #include <cstdlib>
+#include <wtf/Function.h>
 #include <wtf/Vector.h>
 
 namespace WebCore {
@@ -220,7 +221,7 @@ public:
         return true;
     }
 
-    ValueType find(std::function<bool(ValueType)> function) const
+    ValueType find(const WTF::Function<bool(ValueType)>& function) const
     {
         if (m_min && function(m_min.value()))
             return m_min.value();
@@ -525,7 +526,7 @@ public:
     double fitnessDistance(const String&) const;
     double fitnessDistance(const Vector<String>&) const;
 
-    const String& find(std::function<bool(const String&)>) const;
+    const String& find(const WTF::Function<bool(const String&)>&) const;
 
     bool isEmpty() const { return m_exact.isEmpty() && m_ideal.isEmpty(); }
     bool isMandatory() const { return !m_exact.isEmpty(); }
@@ -572,8 +573,8 @@ private:
 
 class MediaTrackConstraintSetMap {
 public:
-    WEBCORE_EXPORT void forEach(std::function<void(const MediaConstraint&)>) const;
-    void filter(std::function<bool(const MediaConstraint&)>) const;
+    WEBCORE_EXPORT void forEach(WTF::Function<void(const MediaConstraint&)>&&) const;
+    void filter(const WTF::Function<bool(const MediaConstraint&)>&) const;
     bool isEmpty() const;
     WEBCORE_EXPORT size_t size() const;
 
@@ -806,7 +807,7 @@ private:
 
 struct MediaConstraints {
     void setDefaultVideoConstraints();
-    bool isConstraintSet(std::function<bool(const MediaTrackConstraintSetMap&)>&&);
+    bool isConstraintSet(const WTF::Function<bool(const MediaTrackConstraintSetMap&)>&);
 
     MediaTrackConstraintSetMap mandatoryConstraints;
     Vector<MediaTrackConstraintSetMap> advancedConstraints;
index 06cb443..94211bc 100644 (file)
@@ -502,7 +502,7 @@ void RealtimeMediaSource::applyConstraint(const MediaConstraint& constraint)
             return false;
         };
 
-        auto modeString = downcast<StringConstraint>(constraint).find(filter);
+        auto modeString = downcast<StringConstraint>(constraint).find(WTFMove(filter));
         if (!modeString.isEmpty())
             setFacingMode(RealtimeMediaSourceSettings::videoFacingModeEnum(modeString));
         break;