Changeset 102857 in webkit
- Timestamp:
- Dec 14, 2011 5:37:50 PM (12 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/JavaScriptCore/ChangeLog
r102849 r102857 1 2011-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 1 17 2011-12-14 Hajime Morrita <morrita@chromium.org> 2 18 -
trunk/Source/JavaScriptCore/wtf/Functional.h
r102839 r102857 39 39 // package up and invoke function calls inside WebCore. 40 40 41 // Helper class template to determine whether a given type has ref and deref member functions 42 // with the right type signature. 43 template<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 57 public: 58 static const bool value = sizeof(refAndDerefCheck<T>(0)) == sizeof(YesType); 59 }; 60 41 61 // A FunctionWrapper is a class template that can wrap a function pointer or a member function pointer and 42 62 // provide a unified interface for calling that function. … … 46 66 public: 47 67 typedef R ResultType; 68 static const bool shouldRefFirstParameter = false; 48 69 49 70 explicit FunctionWrapper(R (*function)()) … … 64 85 public: 65 86 typedef R ResultType; 87 static const bool shouldRefFirstParameter = false; 66 88 67 89 explicit FunctionWrapper(R (*function)(P0)) … … 82 104 public: 83 105 typedef R ResultType; 106 static const bool shouldRefFirstParameter = false; 84 107 85 108 explicit FunctionWrapper(R (*function)(P0, P1)) … … 100 123 public: 101 124 typedef R ResultType; 125 static const bool shouldRefFirstParameter = HasRefAndDeref<C>::value; 102 126 103 127 explicit FunctionWrapper(R (C::*function)()) … … 118 142 public: 119 143 typedef R ResultType; 144 static const bool shouldRefFirstParameter = HasRefAndDeref<C>::value; 120 145 121 146 explicit FunctionWrapper(R (C::*function)(P0)) … … 131 156 private: 132 157 R (C::*m_function)(P0); 158 }; 159 160 template<typename T, bool shouldRefAndDeref> struct RefAndDeref { 161 static void ref(T) { } 162 static void deref(T) { } 163 }; 164 165 template<typename T> struct RefAndDeref<T*, true> { 166 static void ref(T* t) { t->ref(); } 167 static void deref(T* t) { t->deref(); } 133 168 }; 134 169 … … 173 208 , m_p0(p0) 174 209 { 210 RefAndDeref<P0, FunctionWrapper::shouldRefFirstParameter>::ref(m_p0); 211 } 212 213 ~BoundFunctionImpl() 214 { 215 RefAndDeref<P0, FunctionWrapper::shouldRefFirstParameter>::deref(m_p0); 175 216 } 176 217 … … 192 233 , m_p1(p1) 193 234 { 235 RefAndDeref<P0, FunctionWrapper::shouldRefFirstParameter>::ref(m_p0); 236 } 237 238 ~BoundFunctionImpl() 239 { 240 RefAndDeref<P0, FunctionWrapper::shouldRefFirstParameter>::deref(m_p0); 194 241 } 195 242 -
trunk/Tools/ChangeLog
r102849 r102857 1 2011-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 1 19 2011-12-14 Hajime Morrita <morrita@chromium.org> 2 20 -
trunk/Tools/TestWebKitAPI/Tests/WTF/Functional.cpp
r102839 r102857 106 106 } 107 107 108 class B { 109 public: 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 135 private: 136 int m_numRefCalls; 137 int m_numDerefCalls; 138 }; 139 140 TEST(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 108 151 } // namespace TestWebKitAPI
Note: See TracChangeset
for help on using the changeset viewer.