Changeset 102808 in webkit
- Timestamp:
- Dec 14, 2011 11:55:59 AM (12 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/JavaScriptCore/ChangeLog
r102797 r102808 1 2011-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 1 13 2011-12-14 Anders Carlsson <andersca@apple.com> 2 14 -
trunk/Source/JavaScriptCore/wtf/Functional.h
r102730 r102808 61 61 }; 62 62 63 template<typename R, typename P0> class FunctionWrapper<R (*)(P0)> { 64 public: 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 77 private: 78 R (*m_function)(P0); 79 }; 80 81 template<typename R, typename P0, typename P1> class FunctionWrapper<R (*)(P0, P1)> { 82 public: 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 95 private: 96 R (*m_function)(P0, P1); 97 }; 98 63 99 class FunctionImplBase : public ThreadSafeRefCounted<FunctionImplBase> { 64 100 public: … … 92 128 private: 93 129 FunctionWrapper m_functionWrapper; 130 }; 131 132 template<typename FunctionWrapper, typename P0> class BoundFunctionImpl<FunctionWrapper, typename FunctionWrapper::ResultType (P0)> : public FunctionImpl<typename FunctionWrapper::ResultType ()> { 133 134 public: 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 146 private: 147 FunctionWrapper m_functionWrapper; 148 P0 m_p0; 149 }; 150 151 template<typename FunctionWrapper, typename P0, typename P1> class BoundFunctionImpl<FunctionWrapper, typename FunctionWrapper::ResultType (P0, P1)> : public FunctionImpl<typename FunctionWrapper::ResultType ()> { 152 public: 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 165 private: 166 FunctionWrapper m_functionWrapper; 167 P0 m_p0; 168 P1 m_p1; 94 169 }; 95 170 … … 148 223 } 149 224 225 template<typename FunctionType, typename A1> 226 Function<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 231 template<typename FunctionType, typename A1, typename A2> 232 Function<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 150 237 } 151 238 -
trunk/Tools/ChangeLog
r102806 r102808 1 2011-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 1 17 2011-12-14 Holger Hans Peter Freyther <holger@moiji-mobile.com> 2 18 -
trunk/Tools/TestWebKitAPI/Tests/WTF/Functional.cpp
r102730 r102808 36 36 TEST(FunctionalTest, Basic) 37 37 { 38 Function<int ()> emptyFunction;38 Function<int ()> emptyFunction; 39 39 ASSERT_TRUE(emptyFunction.isNull()); 40 40 41 Function<int ()> returnFortyTwoFunction = bind(returnFortyTwo);41 Function<int ()> returnFortyTwoFunction = bind(returnFortyTwo); 42 42 ASSERT_FALSE(returnFortyTwoFunction.isNull()); 43 43 ASSERT_EQ(42, returnFortyTwoFunction()); 44 44 } 45 45 46 static int multiplyByTwo(int n) 47 { 48 return n * 2; 49 } 50 51 static double multiplyByOneAndAHalf(double d) 52 { 53 return d * 1.5; 54 } 55 56 TEST(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 65 static int multiply(int x, int y) 66 { 67 return x * y; 68 } 69 70 static int subtract(int x, int y) 71 { 72 return x - y; 73 } 74 75 TEST(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 46 84 } // namespace TestWebKitAPI
Note: See TracChangeset
for help on using the changeset viewer.