Changeset 102857 in webkit


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

binding a member function should ref/deref the object pointer if needed
https://bugs.webkit.org/show_bug.cgi?id=74552

Reviewed by Sam Weinig.

Source/JavaScriptCore:

Add a HasRefAndDeref helper class template which checks if a given class type has ref and deref
member functions which the right type. Use this to determine if we should ref/deref the first parameter.

  • wtf/Functional.h:

(WTF::R):
(WTF::C::):
(WTF::RefAndDeref::ref):
(WTF::RefAndDeref::deref):

Tools:

Add new tests.

  • TestWebKitAPI/Tests/WTF/Functional.cpp:

(TestWebKitAPI::B::B):
(TestWebKitAPI::B::~B):
(TestWebKitAPI::B::ref):
(TestWebKitAPI::B::deref):
(TestWebKitAPI::B::f):
(TestWebKitAPI::B::g):
(TestWebKitAPI::TEST):

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/ChangeLog

    r102849 r102857  
     12011-12-14  Anders Carlsson  <andersca@apple.com>
     2
     3        binding a member function should ref/deref the object pointer if needed
     4        https://bugs.webkit.org/show_bug.cgi?id=74552
     5
     6        Reviewed by Sam Weinig.
     7
     8        Add a HasRefAndDeref helper class template which checks if a given class type has ref and deref
     9        member functions which the right type. Use this to determine if we should ref/deref the first parameter.
     10
     11        * wtf/Functional.h:
     12        (WTF::R):
     13        (WTF::C::):
     14        (WTF::RefAndDeref::ref):
     15        (WTF::RefAndDeref::deref):
     16
    1172011-12-14  Hajime Morrita  <morrita@chromium.org>
    218
  • trunk/Source/JavaScriptCore/wtf/Functional.h

    r102839 r102857  
    3939// package up and invoke function calls inside WebCore.
    4040
     41// Helper class template to determine whether a given type has ref and deref member functions
     42// with the right type signature.
     43template<typename T> class HasRefAndDeref {
     44    typedef char YesType;
     45    struct NoType {
     46        char padding[8];
     47    };
     48
     49    template<typename U, U, U> struct TypeChecker { };
     50
     51    template<typename U>
     52    static YesType refAndDerefCheck(TypeChecker<void (U::*)(), &U::ref, &U::deref>*);
     53
     54    template<typename U>
     55    static NoType refAndDerefCheck(...);
     56
     57public:
     58    static const bool value = sizeof(refAndDerefCheck<T>(0)) == sizeof(YesType);
     59};
     60
    4161// A FunctionWrapper is a class template that can wrap a function pointer or a member function pointer and
    4262// provide a unified interface for calling that function.
     
    4666public:
    4767    typedef R ResultType;
     68    static const bool shouldRefFirstParameter = false;
    4869
    4970    explicit FunctionWrapper(R (*function)())
     
    6485public:
    6586    typedef R ResultType;
     87    static const bool shouldRefFirstParameter = false;
    6688
    6789    explicit FunctionWrapper(R (*function)(P0))
     
    82104public:
    83105    typedef R ResultType;
     106    static const bool shouldRefFirstParameter = false;
    84107
    85108    explicit FunctionWrapper(R (*function)(P0, P1))
     
    100123public:
    101124    typedef R ResultType;
     125    static const bool shouldRefFirstParameter = HasRefAndDeref<C>::value;
    102126
    103127    explicit FunctionWrapper(R (C::*function)())
     
    118142public:
    119143    typedef R ResultType;
     144    static const bool shouldRefFirstParameter = HasRefAndDeref<C>::value;
    120145
    121146    explicit FunctionWrapper(R (C::*function)(P0))
     
    131156private:
    132157    R (C::*m_function)(P0);
     158};
     159
     160template<typename T, bool shouldRefAndDeref> struct RefAndDeref {
     161    static void ref(T) { }
     162    static void deref(T) { }
     163};
     164
     165template<typename T> struct RefAndDeref<T*, true> {
     166    static void ref(T* t) { t->ref(); }
     167    static void deref(T* t) { t->deref(); }
    133168};
    134169
     
    173208        , m_p0(p0)
    174209    {
     210        RefAndDeref<P0, FunctionWrapper::shouldRefFirstParameter>::ref(m_p0);
     211    }
     212
     213    ~BoundFunctionImpl()
     214    {
     215        RefAndDeref<P0, FunctionWrapper::shouldRefFirstParameter>::deref(m_p0);
    175216    }
    176217
     
    192233        , m_p1(p1)
    193234    {
     235        RefAndDeref<P0, FunctionWrapper::shouldRefFirstParameter>::ref(m_p0);
     236    }
     237   
     238    ~BoundFunctionImpl()
     239    {
     240        RefAndDeref<P0, FunctionWrapper::shouldRefFirstParameter>::deref(m_p0);
    194241    }
    195242
  • trunk/Tools/ChangeLog

    r102849 r102857  
     12011-12-14  Anders Carlsson  <andersca@apple.com>
     2
     3        binding a member function should ref/deref the object pointer if needed
     4        https://bugs.webkit.org/show_bug.cgi?id=74552
     5
     6        Reviewed by Sam Weinig.
     7
     8        Add new tests.
     9
     10        * TestWebKitAPI/Tests/WTF/Functional.cpp:
     11        (TestWebKitAPI::B::B):
     12        (TestWebKitAPI::B::~B):
     13        (TestWebKitAPI::B::ref):
     14        (TestWebKitAPI::B::deref):
     15        (TestWebKitAPI::B::f):
     16        (TestWebKitAPI::B::g):
     17        (TestWebKitAPI::TEST):
     18
    1192011-12-14  Hajime Morrita  <morrita@chromium.org>
    220
  • trunk/Tools/TestWebKitAPI/Tests/WTF/Functional.cpp

    r102839 r102857  
    106106}
    107107
     108class B {
     109public:
     110    B()
     111        : m_numRefCalls(0)
     112        , m_numDerefCalls(0)
     113    {
     114    }
     115
     116    ~B()
     117    {
     118        ASSERT_TRUE(m_numRefCalls == m_numDerefCalls);
     119        ASSERT_GT(m_numRefCalls, 0);
     120    }
     121
     122    void ref()
     123    {
     124        m_numRefCalls++;
     125    }
     126
     127    void deref()
     128    {
     129        m_numDerefCalls++;
     130    }
     131
     132    void f() { ASSERT_GT(m_numRefCalls, 0); }
     133    void g(int) { ASSERT_GT(m_numRefCalls, 0); }
     134
     135private:
     136    int m_numRefCalls;
     137    int m_numDerefCalls;
     138};
     139
     140TEST(FunctionalTest, MemberFunctionBindRefDeref)
     141{
     142    B b;
     143
     144    Function<void ()> function1 = bind(&B::f, &b);
     145    function1();
     146
     147    Function<void ()> function2 = bind(&B::g, &b, 10);
     148    function2();
     149}
     150
    108151} // namespace TestWebKitAPI
Note: See TracChangeset for help on using the changeset viewer.