Changeset 102808 in webkit


Ignore:
Timestamp:
Dec 14, 2011 11:55:59 AM (12 years ago)
Author:
andersca@apple.com
Message:

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

Reviewed by Sam Weinig.

Source/JavaScriptCore:

  • 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):

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/ChangeLog

    r102797 r102808  
     12011-12-14  Anders Carlsson  <andersca@apple.com>
     2
     3        Add unary and binary bind overloads
     4        https://bugs.webkit.org/show_bug.cgi?id=74524
     5
     6        Reviewed by Sam Weinig.
     7
     8        * wtf/Functional.h:
     9        (WTF::R):
     10        (WTF::FunctionWrapper::ResultType):
     11        (WTF::bind):
     12
    1132011-12-14  Anders Carlsson  <andersca@apple.com>
    214
  • trunk/Source/JavaScriptCore/wtf/Functional.h

    r102730 r102808  
    6161};
    6262
     63template<typename R, typename P0> class FunctionWrapper<R (*)(P0)> {
     64public:
     65    typedef R ResultType;
     66
     67    explicit FunctionWrapper(R (*function)(P0))
     68        : m_function(function)
     69    {
     70    }
     71
     72    R operator()(P0 p0)
     73    {
     74        return m_function(p0);
     75    }
     76
     77private:
     78    R (*m_function)(P0);
     79};
     80
     81template<typename R, typename P0, typename P1> class FunctionWrapper<R (*)(P0, P1)> {
     82public:
     83    typedef R ResultType;
     84
     85    explicit FunctionWrapper(R (*function)(P0, P1))
     86        : m_function(function)
     87    {
     88    }
     89
     90    R operator()(P0 p0, P1 p1)
     91    {
     92        return m_function(p0, p1);
     93    }
     94
     95private:
     96    R (*m_function)(P0, P1);
     97};
     98
    6399class FunctionImplBase : public ThreadSafeRefCounted<FunctionImplBase> {
    64100public:
     
    92128private:
    93129    FunctionWrapper m_functionWrapper;
     130};
     131
     132template<typename FunctionWrapper, typename P0> class BoundFunctionImpl<FunctionWrapper, typename FunctionWrapper::ResultType (P0)> : public FunctionImpl<typename FunctionWrapper::ResultType ()> {
     133
     134public:
     135    BoundFunctionImpl(FunctionWrapper functionWrapper, const P0& p0)
     136        : m_functionWrapper(functionWrapper)
     137        , m_p0(p0)
     138    {
     139    }
     140
     141    virtual typename FunctionWrapper::ResultType operator()()
     142    {
     143        return m_functionWrapper(m_p0);
     144    }
     145
     146private:
     147    FunctionWrapper m_functionWrapper;
     148    P0 m_p0;
     149};
     150
     151template<typename FunctionWrapper, typename P0, typename P1> class BoundFunctionImpl<FunctionWrapper, typename FunctionWrapper::ResultType (P0, P1)> : public FunctionImpl<typename FunctionWrapper::ResultType ()> {
     152public:
     153    BoundFunctionImpl(FunctionWrapper functionWrapper, const P0& p0, const P1& p1)
     154        : m_functionWrapper(functionWrapper)
     155        , m_p0(p0)
     156        , m_p1(p1)
     157    {
     158    }
     159
     160    virtual typename FunctionWrapper::ResultType operator()()
     161    {
     162        return m_functionWrapper(m_p0, m_p1);
     163    }
     164
     165private:
     166    FunctionWrapper m_functionWrapper;
     167    P0 m_p0;
     168    P1 m_p1;
    94169};
    95170
     
    148223}
    149224
     225template<typename FunctionType, typename A1>
     226Function<typename FunctionWrapper<FunctionType>::ResultType ()> bind(FunctionType function, const A1& a1)
     227{
     228    return Function<typename FunctionWrapper<FunctionType>::ResultType ()>(adoptRef(new BoundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A1)>(FunctionWrapper<FunctionType>(function), a1)));
     229}
     230
     231template<typename FunctionType, typename A1, typename A2>
     232Function<typename FunctionWrapper<FunctionType>::ResultType ()> bind(FunctionType function, const A1& a1, const A2& a2)
     233{
     234    return Function<typename FunctionWrapper<FunctionType>::ResultType ()>(adoptRef(new BoundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A1, A2)>(FunctionWrapper<FunctionType>(function), a1, a2)));
     235}
     236
    150237}
    151238
  • trunk/Tools/ChangeLog

    r102806 r102808  
     12011-12-14  Anders Carlsson  <andersca@apple.com>
     2
     3        Add unary and binary bind overloads
     4        https://bugs.webkit.org/show_bug.cgi?id=74524
     5
     6        Reviewed by Sam Weinig.
     7
     8        Add tests.
     9
     10        * TestWebKitAPI/Tests/WTF/Functional.cpp:
     11        (TestWebKitAPI::TEST):
     12        (TestWebKitAPI::multiplyByTwo):
     13        (TestWebKitAPI::multiplyByOneAndAHalf):
     14        (TestWebKitAPI::multiply):
     15        (TestWebKitAPI::subtract):
     16
    1172011-12-14  Holger Hans Peter Freyther  <holger@moiji-mobile.com>
    218
  • trunk/Tools/TestWebKitAPI/Tests/WTF/Functional.cpp

    r102730 r102808  
    3636TEST(FunctionalTest, Basic)
    3737{
    38     Function<int()> emptyFunction;
     38    Function<int ()> emptyFunction;
    3939    ASSERT_TRUE(emptyFunction.isNull());
    4040
    41     Function<int()> returnFortyTwoFunction = bind(returnFortyTwo);
     41    Function<int ()> returnFortyTwoFunction = bind(returnFortyTwo);
    4242    ASSERT_FALSE(returnFortyTwoFunction.isNull());
    4343    ASSERT_EQ(42, returnFortyTwoFunction());
    4444}
    4545
     46static int multiplyByTwo(int n)
     47{
     48    return n * 2;
     49}
     50
     51static double multiplyByOneAndAHalf(double d)
     52{
     53    return d * 1.5;
     54}
     55
     56TEST(FunctionalTest, UnaryBind)
     57{
     58    Function<int ()> multiplyFourByTwoFunction = bind(multiplyByTwo, 4);
     59    ASSERT_EQ(8, multiplyFourByTwoFunction());
     60
     61    Function<double ()> multiplyByOneAndAHalfFunction = bind(multiplyByOneAndAHalf, 3);
     62    ASSERT_EQ(4.5, multiplyByOneAndAHalfFunction());
     63}
     64
     65static int multiply(int x, int y)
     66{
     67    return x * y;
     68}
     69
     70static int subtract(int x, int y)
     71{
     72    return x - y;
     73}
     74
     75TEST(FunctionalTest, BinaryBind)
     76{
     77    Function<int ()> multiplyFourByTwoFunction = bind(multiply, 4, 2);
     78    ASSERT_EQ(8, multiplyFourByTwoFunction());
     79
     80    Function<int ()> subtractTwoFromFourFunction = bind(subtract, 4, 2);
     81    ASSERT_EQ(2, subtractTwoFromFourFunction());
     82}
     83
    4684} // namespace TestWebKitAPI
Note: See TracChangeset for help on using the changeset viewer.