Title: [102808] trunk
Revision
102808
Author
ander...@apple.com
Date
2011-12-14 11:55:59 -0800 (Wed, 14 Dec 2011)

Log Message

Add unary and binary bind overloads
https://bugs.webkit.org/show_bug.cgi?id=74524

Reviewed by Sam Weinig.

Source/_javascript_Core:

* wtf/Functional.h:
(WTF::R):
(WTF::FunctionWrapper::ResultType):
(WTF::bind):

Tools:

Add tests.

* TestWebKitAPI/Tests/WTF/Functional.cpp:
(TestWebKitAPI::TEST):
(TestWebKitAPI::multiplyByTwo):
(TestWebKitAPI::multiplyByOneAndAHalf):
(TestWebKitAPI::multiply):
(TestWebKitAPI::subtract):

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (102807 => 102808)


--- trunk/Source/_javascript_Core/ChangeLog	2011-12-14 19:49:47 UTC (rev 102807)
+++ trunk/Source/_javascript_Core/ChangeLog	2011-12-14 19:55:59 UTC (rev 102808)
@@ -1,5 +1,17 @@
 2011-12-14  Anders Carlsson  <ander...@apple.com>
 
+        Add unary and binary bind overloads
+        https://bugs.webkit.org/show_bug.cgi?id=74524
+
+        Reviewed by Sam Weinig.
+
+        * wtf/Functional.h:
+        (WTF::R):
+        (WTF::FunctionWrapper::ResultType):
+        (WTF::bind):
+
+2011-12-14  Anders Carlsson  <ander...@apple.com>
+
         Add back the callOnMainThread overload that takes a WTF::Function
         https://bugs.webkit.org/show_bug.cgi?id=74512
 

Modified: trunk/Source/_javascript_Core/wtf/Functional.h (102807 => 102808)


--- trunk/Source/_javascript_Core/wtf/Functional.h	2011-12-14 19:49:47 UTC (rev 102807)
+++ trunk/Source/_javascript_Core/wtf/Functional.h	2011-12-14 19:55:59 UTC (rev 102808)
@@ -60,6 +60,42 @@
     R (*m_function)();
 };
 
+template<typename R, typename P0> class FunctionWrapper<R (*)(P0)> {
+public:
+    typedef R ResultType;
+
+    explicit FunctionWrapper(R (*function)(P0))
+        : m_function(function)
+    {
+    }
+
+    R operator()(P0 p0)
+    {
+        return m_function(p0);
+    }
+
+private:
+    R (*m_function)(P0);
+};
+
+template<typename R, typename P0, typename P1> class FunctionWrapper<R (*)(P0, P1)> {
+public:
+    typedef R ResultType;
+
+    explicit FunctionWrapper(R (*function)(P0, P1))
+        : m_function(function)
+    {
+    }
+
+    R operator()(P0 p0, P1 p1)
+    {
+        return m_function(p0, p1);
+    }
+
+private:
+    R (*m_function)(P0, P1);
+};
+
 class FunctionImplBase : public ThreadSafeRefCounted<FunctionImplBase> {
 public:
     virtual ~FunctionImplBase() { }
@@ -93,6 +129,45 @@
     FunctionWrapper m_functionWrapper;
 };
 
+template<typename FunctionWrapper, typename P0> class BoundFunctionImpl<FunctionWrapper, typename FunctionWrapper::ResultType (P0)> : public FunctionImpl<typename FunctionWrapper::ResultType ()> {
+
+public:
+    BoundFunctionImpl(FunctionWrapper functionWrapper, const P0& p0)
+        : m_functionWrapper(functionWrapper)
+        , m_p0(p0)
+    {
+    }
+
+    virtual typename FunctionWrapper::ResultType operator()()
+    {
+        return m_functionWrapper(m_p0);
+    }
+
+private:
+    FunctionWrapper m_functionWrapper;
+    P0 m_p0;
+};
+
+template<typename FunctionWrapper, typename P0, typename P1> class BoundFunctionImpl<FunctionWrapper, typename FunctionWrapper::ResultType (P0, P1)> : public FunctionImpl<typename FunctionWrapper::ResultType ()> {
+public:
+    BoundFunctionImpl(FunctionWrapper functionWrapper, const P0& p0, const P1& p1)
+        : m_functionWrapper(functionWrapper)
+        , m_p0(p0)
+        , m_p1(p1)
+    {
+    }
+
+    virtual typename FunctionWrapper::ResultType operator()()
+    {
+        return m_functionWrapper(m_p0, m_p1);
+    }
+
+private:
+    FunctionWrapper m_functionWrapper;
+    P0 m_p0;
+    P1 m_p1;
+};
+
 class FunctionBase {
 public:
     bool isNull() const
@@ -147,8 +222,20 @@
     return Function<typename FunctionWrapper<FunctionType>::ResultType ()>(adoptRef(new BoundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType ()>(FunctionWrapper<FunctionType>(function))));
 }
 
+template<typename FunctionType, typename A1>
+Function<typename FunctionWrapper<FunctionType>::ResultType ()> bind(FunctionType function, const A1& a1)
+{
+    return Function<typename FunctionWrapper<FunctionType>::ResultType ()>(adoptRef(new BoundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A1)>(FunctionWrapper<FunctionType>(function), a1)));
 }
 
+template<typename FunctionType, typename A1, typename A2>
+Function<typename FunctionWrapper<FunctionType>::ResultType ()> bind(FunctionType function, const A1& a1, const A2& a2)
+{
+    return Function<typename FunctionWrapper<FunctionType>::ResultType ()>(adoptRef(new BoundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A1, A2)>(FunctionWrapper<FunctionType>(function), a1, a2)));
+}
+
+}
+
 using WTF::Function;
 using WTF::bind;
 

Modified: trunk/Tools/ChangeLog (102807 => 102808)


--- trunk/Tools/ChangeLog	2011-12-14 19:49:47 UTC (rev 102807)
+++ trunk/Tools/ChangeLog	2011-12-14 19:55:59 UTC (rev 102808)
@@ -1,3 +1,19 @@
+2011-12-14  Anders Carlsson  <ander...@apple.com>
+
+        Add unary and binary bind overloads
+        https://bugs.webkit.org/show_bug.cgi?id=74524
+
+        Reviewed by Sam Weinig.
+
+        Add tests.
+
+        * TestWebKitAPI/Tests/WTF/Functional.cpp:
+        (TestWebKitAPI::TEST):
+        (TestWebKitAPI::multiplyByTwo):
+        (TestWebKitAPI::multiplyByOneAndAHalf):
+        (TestWebKitAPI::multiply):
+        (TestWebKitAPI::subtract):
+
 2011-12-14  Holger Hans Peter Freyther  <hol...@moiji-mobile.com>
 
         [Qt] Test fonts are not used with Qt5

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/Functional.cpp (102807 => 102808)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/Functional.cpp	2011-12-14 19:49:47 UTC (rev 102807)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/Functional.cpp	2011-12-14 19:55:59 UTC (rev 102808)
@@ -35,12 +35,50 @@
 
 TEST(FunctionalTest, Basic)
 {
-    Function<int()> emptyFunction;
+    Function<int ()> emptyFunction;
     ASSERT_TRUE(emptyFunction.isNull());
 
-    Function<int()> returnFortyTwoFunction = bind(returnFortyTwo);
+    Function<int ()> returnFortyTwoFunction = bind(returnFortyTwo);
     ASSERT_FALSE(returnFortyTwoFunction.isNull());
     ASSERT_EQ(42, returnFortyTwoFunction());
 }
 
+static int multiplyByTwo(int n)
+{
+    return n * 2;
+}
+
+static double multiplyByOneAndAHalf(double d)
+{
+    return d * 1.5;
+}
+
+TEST(FunctionalTest, UnaryBind)
+{
+    Function<int ()> multiplyFourByTwoFunction = bind(multiplyByTwo, 4);
+    ASSERT_EQ(8, multiplyFourByTwoFunction());
+
+    Function<double ()> multiplyByOneAndAHalfFunction = bind(multiplyByOneAndAHalf, 3);
+    ASSERT_EQ(4.5, multiplyByOneAndAHalfFunction());
+}
+
+static int multiply(int x, int y)
+{
+    return x * y;
+}
+
+static int subtract(int x, int y)
+{
+    return x - y;
+}
+
+TEST(FunctionalTest, BinaryBind)
+{
+    Function<int ()> multiplyFourByTwoFunction = bind(multiply, 4, 2);
+    ASSERT_EQ(8, multiplyFourByTwoFunction());
+
+    Function<int ()> subtractTwoFromFourFunction = bind(subtract, 4, 2);
+    ASSERT_EQ(2, subtractTwoFromFourFunction());
+}
+
 } // namespace TestWebKitAPI
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to