Changeset 102813 in webkit


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

bind should handle member functions
https://bugs.webkit.org/show_bug.cgi?id=74529

Reviewed by Sam Weinig.

Source/JavaScriptCore:

Add FunctionWrapper partial specializations for member function pointers.

  • wtf/Functional.h:

(WTF::C::):

Tools:

Add tests.

  • TestWebKitAPI/Tests/WTF/Functional.cpp:

(TestWebKitAPI::A::A):
(TestWebKitAPI::A::f):
(TestWebKitAPI::A::addF):
(TestWebKitAPI::TEST):

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/ChangeLog

    r102811 r102813  
     12011-12-14  Anders Carlsson  <andersca@apple.com>
     2
     3        bind should handle member functions
     4        https://bugs.webkit.org/show_bug.cgi?id=74529
     5
     6        Reviewed by Sam Weinig.
     7
     8        Add FunctionWrapper partial specializations for member function pointers.
     9
     10        * wtf/Functional.h:
     11        (WTF::C::):
     12
    1132011-12-14  Gavin Barraclough  <barraclough@apple.com>
    214
  • trunk/Source/JavaScriptCore/wtf/Functional.h

    r102808 r102813  
    9797};
    9898
     99template<typename R, typename C> class FunctionWrapper<R (C::*)()> {
     100public:
     101    typedef R ResultType;
     102
     103    explicit FunctionWrapper(R (C::*function)())
     104        : m_function(function)
     105    {
     106    }
     107
     108    R operator()(C* c)
     109    {
     110        return (c->*m_function)();
     111    }
     112
     113private:
     114    R (C::*m_function)();
     115};
     116
     117template<typename R, typename C, typename P0> class FunctionWrapper<R (C::*)(P0)> {
     118public:
     119    typedef R ResultType;
     120
     121    explicit FunctionWrapper(R (C::*function)(P0))
     122        : m_function(function)
     123    {
     124    }
     125
     126    R operator()(C* c, P0 p0)
     127    {
     128        return (c->*m_function)(p0);
     129    }
     130
     131private:
     132    R (C::*m_function)(P0);
     133};
     134
    99135class FunctionImplBase : public ThreadSafeRefCounted<FunctionImplBase> {
    100136public:
  • trunk/Tools/ChangeLog

    r102808 r102813  
     12011-12-14  Anders Carlsson  <andersca@apple.com>
     2
     3        bind should handle member functions
     4        https://bugs.webkit.org/show_bug.cgi?id=74529
     5
     6        Reviewed by Sam Weinig.
     7
     8        Add tests.
     9
     10        * TestWebKitAPI/Tests/WTF/Functional.cpp:
     11        (TestWebKitAPI::A::A):
     12        (TestWebKitAPI::A::f):
     13        (TestWebKitAPI::A::addF):
     14        (TestWebKitAPI::TEST):
     15
    1162011-12-14  Anders Carlsson  <andersca@apple.com>
    217
  • trunk/Tools/TestWebKitAPI/Tests/WTF/Functional.cpp

    r102808 r102813  
    8282}
    8383
     84class A {
     85public:
     86    explicit A(int i)
     87        : m_i(i)
     88    {
     89    }
     90
     91    int f() { return m_i; }
     92    int addF(int j) { return m_i + j; }
     93
     94private:
     95    int m_i;
     96};
     97
     98TEST(FunctionalTest, MemberFunctionBind)
     99{
     100    A a(10);
     101    Function<int ()> function1 = bind(&A::f, &a);
     102    ASSERT_EQ(10, function1());
     103
     104    Function<int ()> function2 = bind(&A::addF, &a, 15);
     105    ASSERT_EQ(25, function2());
     106}
     107
    84108} // namespace TestWebKitAPI
Note: See TracChangeset for help on using the changeset viewer.