| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2011 Apple Inc. All rights reserved. | 2 * Copyright (C) 2011 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 bind(sixArgFunc, 3, 3.5); | 210 bind(sixArgFunc, 3, 3.5); |
| 211 (*twoBound)('d', &a, &b, &c); | 211 (*twoBound)('d', &a, &b, &c); |
| 212 assertArgs(a, b, c, 3, 3.5, 'd'); | 212 assertArgs(a, b, c, 3, 3.5, 'd'); |
| 213 | 213 |
| 214 std::unique_ptr<Function<void(int*, double*, char*)>> threeBound = | 214 std::unique_ptr<Function<void(int*, double*, char*)>> threeBound = |
| 215 bind(sixArgFunc, 4, 4.5, 'e'); | 215 bind(sixArgFunc, 4, 4.5, 'e'); |
| 216 (*threeBound)(&a, &b, &c); | 216 (*threeBound)(&a, &b, &c); |
| 217 assertArgs(a, b, c, 4, 4.5, 'e'); | 217 assertArgs(a, b, c, 4, 4.5, 'e'); |
| 218 | 218 |
| 219 std::unique_ptr<Function<void(double*, char*)>> fourBound = | 219 std::unique_ptr<Function<void(double*, char*)>> fourBound = |
| 220 bind(sixArgFunc, 5, 5.5, 'f', unretained(&a)); | 220 bind(sixArgFunc, 5, 5.5, 'f', WTF::unretained(&a)); |
| 221 (*fourBound)(&b, &c); | 221 (*fourBound)(&b, &c); |
| 222 assertArgs(a, b, c, 5, 5.5, 'f'); | 222 assertArgs(a, b, c, 5, 5.5, 'f'); |
| 223 | 223 |
| 224 std::unique_ptr<Function<void(char*)>> fiveBound = | 224 std::unique_ptr<Function<void(char*)>> fiveBound = |
| 225 bind(sixArgFunc, 6, 6.5, 'g', unretained(&a), unretained(&b)); | 225 bind(sixArgFunc, 6, 6.5, 'g', WTF::unretained(&a), WTF::unretained(&b)); |
| 226 (*fiveBound)(&c); | 226 (*fiveBound)(&c); |
| 227 assertArgs(a, b, c, 6, 6.5, 'g'); | 227 assertArgs(a, b, c, 6, 6.5, 'g'); |
| 228 | 228 |
| 229 std::unique_ptr<Function<void()>> sixBound = bind( | 229 std::unique_ptr<Function<void()>> sixBound = |
| 230 sixArgFunc, 7, 7.5, 'h', unretained(&a), unretained(&b), unretained(&c)); | 230 bind(sixArgFunc, 7, 7.5, 'h', WTF::unretained(&a), WTF::unretained(&b), |
| 231 WTF::unretained(&c)); |
| 231 (*sixBound)(); | 232 (*sixBound)(); |
| 232 assertArgs(a, b, c, 7, 7.5, 'h'); | 233 assertArgs(a, b, c, 7, 7.5, 'h'); |
| 233 } | 234 } |
| 234 | 235 |
| 235 class A { | 236 class A { |
| 236 public: | 237 public: |
| 237 explicit A(int i) : m_i(i) {} | 238 explicit A(int i) : m_i(i) {} |
| 238 | 239 |
| 239 int f() { return m_i; } | 240 int f() { return m_i; } |
| 240 int addF(int j) { return m_i + j; } | 241 int addF(int j) { return m_i + j; } |
| 241 virtual int overridden() { return 42; } | 242 virtual int overridden() { return 42; } |
| 242 | 243 |
| 243 private: | 244 private: |
| 244 int m_i; | 245 int m_i; |
| 245 }; | 246 }; |
| 246 | 247 |
| 247 class B : public A { | 248 class B : public A { |
| 248 public: | 249 public: |
| 249 explicit B(int i) : A(i) {} | 250 explicit B(int i) : A(i) {} |
| 250 | 251 |
| 251 int f() { return A::f() + 1; } | 252 int f() { return A::f() + 1; } |
| 252 int addF(int j) { return A::addF(j) + 1; } | 253 int addF(int j) { return A::addF(j) + 1; } |
| 253 int overridden() override { return 43; } | 254 int overridden() override { return 43; } |
| 254 }; | 255 }; |
| 255 | 256 |
| 256 TEST(FunctionalTest, MemberFunctionBind) { | 257 TEST(FunctionalTest, MemberFunctionBind) { |
| 257 A a(10); | 258 A a(10); |
| 258 std::unique_ptr<Function<int()>> function1 = bind(&A::f, unretained(&a)); | 259 std::unique_ptr<Function<int()>> function1 = bind(&A::f, WTF::unretained(&a)); |
| 259 EXPECT_EQ(10, (*function1)()); | 260 EXPECT_EQ(10, (*function1)()); |
| 260 | 261 |
| 261 std::unique_ptr<Function<int()>> function2 = | 262 std::unique_ptr<Function<int()>> function2 = |
| 262 bind(&A::addF, unretained(&a), 15); | 263 bind(&A::addF, WTF::unretained(&a), 15); |
| 263 EXPECT_EQ(25, (*function2)()); | 264 EXPECT_EQ(25, (*function2)()); |
| 264 | 265 |
| 265 std::unique_ptr<Function<int()>> function3 = | 266 std::unique_ptr<Function<int()>> function3 = |
| 266 bind(&A::overridden, unretained(&a)); | 267 bind(&A::overridden, WTF::unretained(&a)); |
| 267 EXPECT_EQ(42, (*function3)()); | 268 EXPECT_EQ(42, (*function3)()); |
| 268 } | 269 } |
| 269 | 270 |
| 270 TEST(FunctionalTest, MemberFunctionBindWithSubclassPointer) { | 271 TEST(FunctionalTest, MemberFunctionBindWithSubclassPointer) { |
| 271 B b(10); | 272 B b(10); |
| 272 std::unique_ptr<Function<int()>> function1 = bind(&A::f, unretained(&b)); | 273 std::unique_ptr<Function<int()>> function1 = bind(&A::f, WTF::unretained(&b)); |
| 273 EXPECT_EQ(10, (*function1)()); | 274 EXPECT_EQ(10, (*function1)()); |
| 274 | 275 |
| 275 std::unique_ptr<Function<int()>> function2 = | 276 std::unique_ptr<Function<int()>> function2 = |
| 276 bind(&A::addF, unretained(&b), 15); | 277 bind(&A::addF, WTF::unretained(&b), 15); |
| 277 EXPECT_EQ(25, (*function2)()); | 278 EXPECT_EQ(25, (*function2)()); |
| 278 | 279 |
| 279 std::unique_ptr<Function<int()>> function3 = | 280 std::unique_ptr<Function<int()>> function3 = |
| 280 bind(&A::overridden, unretained(&b)); | 281 bind(&A::overridden, WTF::unretained(&b)); |
| 281 EXPECT_EQ(43, (*function3)()); | 282 EXPECT_EQ(43, (*function3)()); |
| 282 | 283 |
| 283 std::unique_ptr<Function<int()>> function4 = bind(&B::f, unretained(&b)); | 284 std::unique_ptr<Function<int()>> function4 = bind(&B::f, WTF::unretained(&b)); |
| 284 EXPECT_EQ(11, (*function4)()); | 285 EXPECT_EQ(11, (*function4)()); |
| 285 | 286 |
| 286 std::unique_ptr<Function<int()>> function5 = | 287 std::unique_ptr<Function<int()>> function5 = |
| 287 bind(&B::addF, unretained(&b), 15); | 288 bind(&B::addF, WTF::unretained(&b), 15); |
| 288 EXPECT_EQ(26, (*function5)()); | 289 EXPECT_EQ(26, (*function5)()); |
| 289 } | 290 } |
| 290 | 291 |
| 291 TEST(FunctionalTest, MemberFunctionBindWithSubclassPointerWithCast) { | 292 TEST(FunctionalTest, MemberFunctionBindWithSubclassPointerWithCast) { |
| 292 B b(10); | 293 B b(10); |
| 293 std::unique_ptr<Function<int()>> function1 = | 294 std::unique_ptr<Function<int()>> function1 = |
| 294 bind(&A::f, unretained(static_cast<A*>(&b))); | 295 bind(&A::f, WTF::unretained(static_cast<A*>(&b))); |
| 295 EXPECT_EQ(10, (*function1)()); | 296 EXPECT_EQ(10, (*function1)()); |
| 296 | 297 |
| 297 std::unique_ptr<Function<int()>> function2 = | 298 std::unique_ptr<Function<int()>> function2 = |
| 298 bind(&A::addF, unretained(static_cast<A*>(&b)), 15); | 299 bind(&A::addF, WTF::unretained(static_cast<A*>(&b)), 15); |
| 299 EXPECT_EQ(25, (*function2)()); | 300 EXPECT_EQ(25, (*function2)()); |
| 300 | 301 |
| 301 std::unique_ptr<Function<int()>> function3 = | 302 std::unique_ptr<Function<int()>> function3 = |
| 302 bind(&A::overridden, unretained(static_cast<A*>(&b))); | 303 bind(&A::overridden, WTF::unretained(static_cast<A*>(&b))); |
| 303 EXPECT_EQ(43, (*function3)()); | 304 EXPECT_EQ(43, (*function3)()); |
| 304 } | 305 } |
| 305 | 306 |
| 306 TEST(FunctionalTest, MemberFunctionPartBind) { | 307 TEST(FunctionalTest, MemberFunctionPartBind) { |
| 307 A a(10); | 308 A a(10); |
| 308 std::unique_ptr<Function<int(class A*)>> function1 = bind(&A::f); | 309 std::unique_ptr<Function<int(class A*)>> function1 = bind(&A::f); |
| 309 EXPECT_EQ(10, (*function1)(&a)); | 310 EXPECT_EQ(10, (*function1)(&a)); |
| 310 | 311 |
| 311 std::unique_ptr<Function<int(class A*, int)>> unboundFunction2 = | 312 std::unique_ptr<Function<int(class A*, int)>> unboundFunction2 = |
| 312 bind(&A::addF); | 313 bind(&A::addF); |
| 313 EXPECT_EQ(25, (*unboundFunction2)(&a, 15)); | 314 EXPECT_EQ(25, (*unboundFunction2)(&a, 15)); |
| 314 std::unique_ptr<Function<int(int)>> objectBoundFunction2 = | 315 std::unique_ptr<Function<int(int)>> objectBoundFunction2 = |
| 315 bind(&A::addF, unretained(&a)); | 316 bind(&A::addF, WTF::unretained(&a)); |
| 316 EXPECT_EQ(25, (*objectBoundFunction2)(15)); | 317 EXPECT_EQ(25, (*objectBoundFunction2)(15)); |
| 317 } | 318 } |
| 318 | 319 |
| 319 TEST(FunctionalTest, MemberFunctionBindByUniquePtr) { | 320 TEST(FunctionalTest, MemberFunctionBindByUniquePtr) { |
| 320 std::unique_ptr<Function<int()>> function1 = | 321 std::unique_ptr<Function<int()>> function1 = |
| 321 WTF::bind(&A::f, makeUnique<A>(10)); | 322 WTF::bind(&A::f, WTF::makeUnique<A>(10)); |
| 322 EXPECT_EQ(10, (*function1)()); | 323 EXPECT_EQ(10, (*function1)()); |
| 323 } | 324 } |
| 324 | 325 |
| 325 TEST(FunctionalTest, MemberFunctionBindByPassedUniquePtr) { | 326 TEST(FunctionalTest, MemberFunctionBindByPassedUniquePtr) { |
| 326 std::unique_ptr<Function<int()>> function1 = | 327 std::unique_ptr<Function<int()>> function1 = |
| 327 WTF::bind(&A::f, passed(makeUnique<A>(10))); | 328 WTF::bind(&A::f, WTF::passed(WTF::makeUnique<A>(10))); |
| 328 EXPECT_EQ(10, (*function1)()); | 329 EXPECT_EQ(10, (*function1)()); |
| 329 } | 330 } |
| 330 | 331 |
| 331 class Number : public RefCounted<Number> { | 332 class Number : public RefCounted<Number> { |
| 332 public: | 333 public: |
| 333 static PassRefPtr<Number> create(int value) { | 334 static PassRefPtr<Number> create(int value) { |
| 334 return adoptRef(new Number(value)); | 335 return adoptRef(new Number(value)); |
| 335 } | 336 } |
| 336 | 337 |
| 337 ~Number() { m_value = 0; } | 338 ~Number() { m_value = 0; } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 365 std::unique_ptr<Function<int()>> multiplySixByTwoFunction = | 366 std::unique_ptr<Function<int()>> multiplySixByTwoFunction = |
| 366 bind(multiplyNumberByTwo, six.release()); | 367 bind(multiplyNumberByTwo, six.release()); |
| 367 EXPECT_FALSE(six); | 368 EXPECT_FALSE(six); |
| 368 EXPECT_EQ(12, (*multiplySixByTwoFunction)()); | 369 EXPECT_EQ(12, (*multiplySixByTwoFunction)()); |
| 369 } | 370 } |
| 370 | 371 |
| 371 TEST(FunctionalTest, UnretainedWithRefCounted) { | 372 TEST(FunctionalTest, UnretainedWithRefCounted) { |
| 372 RefPtr<Number> five = Number::create(5); | 373 RefPtr<Number> five = Number::create(5); |
| 373 EXPECT_EQ(1, five->refCount()); | 374 EXPECT_EQ(1, five->refCount()); |
| 374 std::unique_ptr<Function<int()>> multiplyFiveByTwoFunction = | 375 std::unique_ptr<Function<int()>> multiplyFiveByTwoFunction = |
| 375 bind(multiplyNumberByTwo, unretained(five.get())); | 376 bind(multiplyNumberByTwo, WTF::unretained(five.get())); |
| 376 EXPECT_EQ(1, five->refCount()); | 377 EXPECT_EQ(1, five->refCount()); |
| 377 EXPECT_EQ(10, (*multiplyFiveByTwoFunction)()); | 378 EXPECT_EQ(10, (*multiplyFiveByTwoFunction)()); |
| 378 EXPECT_EQ(1, five->refCount()); | 379 EXPECT_EQ(1, five->refCount()); |
| 379 } | 380 } |
| 380 | 381 |
| 381 int processUnwrappedClass(const UnwrappedClass& u, int v) { | 382 int processUnwrappedClass(const UnwrappedClass& u, int v) { |
| 382 return u.value() * v; | 383 return u.value() * v; |
| 383 } | 384 } |
| 384 | 385 |
| 385 // Tests that: | 386 // Tests that: |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 468 return moveOnly.value(); | 469 return moveOnly.value(); |
| 469 } | 470 } |
| 470 | 471 |
| 471 int tripleMoveOnlysByValues(MoveOnly first, MoveOnly second, MoveOnly third) { | 472 int tripleMoveOnlysByValues(MoveOnly first, MoveOnly second, MoveOnly third) { |
| 472 return first.value() + second.value() + third.value(); | 473 return first.value() + second.value() + third.value(); |
| 473 } | 474 } |
| 474 | 475 |
| 475 TEST(FunctionalTest, BindMoveOnlyObjects) { | 476 TEST(FunctionalTest, BindMoveOnlyObjects) { |
| 476 MoveOnly one(1); | 477 MoveOnly one(1); |
| 477 std::unique_ptr<Function<int()>> bound = | 478 std::unique_ptr<Function<int()>> bound = |
| 478 bind(singleMoveOnlyByRvalueReference, passed(std::move(one))); | 479 bind(singleMoveOnlyByRvalueReference, WTF::passed(std::move(one))); |
| 479 EXPECT_EQ(0, one.value()); // Should be moved away. | 480 EXPECT_EQ(0, one.value()); // Should be moved away. |
| 480 EXPECT_EQ(1, (*bound)()); | 481 EXPECT_EQ(1, (*bound)()); |
| 481 // The stored value must be cleared in the first function call. | 482 // The stored value must be cleared in the first function call. |
| 482 EXPECT_EQ(0, (*bound)()); | 483 EXPECT_EQ(0, (*bound)()); |
| 483 | 484 |
| 484 bound = bind(singleMoveOnlyByValue, passed(MoveOnly(1))); | 485 bound = bind(singleMoveOnlyByValue, WTF::passed(MoveOnly(1))); |
| 485 EXPECT_EQ(1, (*bound)()); | 486 EXPECT_EQ(1, (*bound)()); |
| 486 EXPECT_EQ(0, (*bound)()); | 487 EXPECT_EQ(0, (*bound)()); |
| 487 | 488 |
| 488 bound = bind(tripleMoveOnlysByRvalueReferences, passed(MoveOnly(1)), | 489 bound = bind(tripleMoveOnlysByRvalueReferences, WTF::passed(MoveOnly(1)), |
| 489 passed(MoveOnly(2)), passed(MoveOnly(3))); | 490 WTF::passed(MoveOnly(2)), WTF::passed(MoveOnly(3))); |
| 490 EXPECT_EQ(6, (*bound)()); | 491 EXPECT_EQ(6, (*bound)()); |
| 491 EXPECT_EQ(0, (*bound)()); | 492 EXPECT_EQ(0, (*bound)()); |
| 492 | 493 |
| 493 bound = bind(tripleMoveOnlysByValues, passed(MoveOnly(1)), | 494 bound = bind(tripleMoveOnlysByValues, WTF::passed(MoveOnly(1)), |
| 494 passed(MoveOnly(2)), passed(MoveOnly(3))); | 495 WTF::passed(MoveOnly(2)), WTF::passed(MoveOnly(3))); |
| 495 EXPECT_EQ(6, (*bound)()); | 496 EXPECT_EQ(6, (*bound)()); |
| 496 EXPECT_EQ(0, (*bound)()); | 497 EXPECT_EQ(0, (*bound)()); |
| 497 } | 498 } |
| 498 | 499 |
| 499 class CountCopy { | 500 class CountCopy { |
| 500 public: | 501 public: |
| 501 CountCopy() : m_copies(0) {} | 502 CountCopy() : m_copies(0) {} |
| 502 CountCopy(const CountCopy& other) : m_copies(other.m_copies + 1) {} | 503 CountCopy(const CountCopy& other) : m_copies(other.m_copies + 1) {} |
| 503 | 504 |
| 504 int copies() const { return m_copies; } | 505 int copies() const { return m_copies; } |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 584 | 585 |
| 585 obj.revokeAll(); | 586 obj.revokeAll(); |
| 586 EXPECT_TRUE(bound->isCancelled()); | 587 EXPECT_TRUE(bound->isCancelled()); |
| 587 (*bound)(); | 588 (*bound)(); |
| 588 EXPECT_EQ(1, counter); | 589 EXPECT_EQ(1, counter); |
| 589 } | 590 } |
| 590 | 591 |
| 591 } // anonymous namespace | 592 } // anonymous namespace |
| 592 | 593 |
| 593 } // namespace WTF | 594 } // namespace WTF |
| OLD | NEW |