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 27 matching lines...) Expand all Loading... |
38 : m_value(value) | 38 : m_value(value) |
39 { | 39 { |
40 } | 40 } |
41 | 41 |
42 int value() const { return m_value; } | 42 int value() const { return m_value; } |
43 | 43 |
44 private: | 44 private: |
45 int m_value; | 45 int m_value; |
46 }; | 46 }; |
47 | 47 |
48 class WrappedClass { | |
49 public: | |
50 explicit WrappedClass(int value) | |
51 : m_value(value) | |
52 { | |
53 } | |
54 | |
55 UnwrappedClass unwrap() const { return UnwrappedClass(m_value); } | |
56 | |
57 private: | |
58 int m_value; | |
59 }; | |
60 | |
61 // This class must be wrapped in bind() and unwrapped in closure execution. | 48 // This class must be wrapped in bind() and unwrapped in closure execution. |
62 class ClassToBeWrapped { | 49 class ClassToBeWrapped { |
63 WTF_MAKE_NONCOPYABLE(ClassToBeWrapped); | 50 WTF_MAKE_NONCOPYABLE(ClassToBeWrapped); |
64 public: | 51 public: |
65 explicit ClassToBeWrapped(int value) | 52 explicit ClassToBeWrapped(int value) |
66 : m_value(value) | 53 : m_value(value) |
67 { | 54 { |
68 } | 55 } |
69 | 56 |
70 WrappedClass wrap() const { return WrappedClass(m_value); } | 57 int value() const { return m_value; } |
71 | 58 |
72 private: | 59 private: |
73 int m_value; | 60 int m_value; |
74 }; | 61 }; |
75 | 62 |
| 63 class WrappedClass { |
| 64 public: |
| 65 WrappedClass(const ClassToBeWrapped& to_be_wrapped) |
| 66 : m_value(to_be_wrapped.value()) |
| 67 { |
| 68 } |
| 69 |
| 70 explicit WrappedClass(int value) |
| 71 : m_value(value) |
| 72 { |
| 73 } |
| 74 |
| 75 UnwrappedClass unwrap() const { return UnwrappedClass(m_value); } |
| 76 |
| 77 private: |
| 78 int m_value; |
| 79 }; |
| 80 |
| 81 UnwrappedClass Unwrap(const WrappedClass& wrapped) |
| 82 { |
| 83 return wrapped.unwrap(); |
| 84 } |
| 85 |
76 template<> struct ParamStorageTraits<ClassToBeWrapped> { | 86 template<> struct ParamStorageTraits<ClassToBeWrapped> { |
77 using StorageType = WrappedClass; | 87 using StorageType = WrappedClass; |
78 static StorageType wrap(const ClassToBeWrapped& value) { return value.wrap()
; } | |
79 static UnwrappedClass unwrap(const StorageType& value) { return value.unwrap
(); } | |
80 }; | 88 }; |
81 | 89 |
82 class HasWeakPtrSupport { | 90 class HasWeakPtrSupport { |
83 public: | 91 public: |
84 HasWeakPtrSupport() : m_weakPtrFactory(this) {} | 92 HasWeakPtrSupport() : m_weakPtrFactory(this) {} |
85 | 93 |
86 WTF::WeakPtr<HasWeakPtrSupport> createWeakPtr() { | 94 WTF::WeakPtr<HasWeakPtrSupport> createWeakPtr() { |
87 return m_weakPtrFactory.createWeakPtr(); | 95 return m_weakPtrFactory.createWeakPtr(); |
88 } | 96 } |
89 | 97 |
(...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
516 | 524 |
517 int takeCountCopyAsValue(CountCopy counter) | 525 int takeCountCopyAsValue(CountCopy counter) |
518 { | 526 { |
519 return counter.copies(); | 527 return counter.copies(); |
520 } | 528 } |
521 | 529 |
522 TEST(FunctionalTest, CountCopiesOfBoundArguments) | 530 TEST(FunctionalTest, CountCopiesOfBoundArguments) |
523 { | 531 { |
524 CountCopy lvalue; | 532 CountCopy lvalue; |
525 std::unique_ptr<Function<int()>> bound = bind(takeCountCopyAsConstReference,
lvalue); | 533 std::unique_ptr<Function<int()>> bound = bind(takeCountCopyAsConstReference,
lvalue); |
526 EXPECT_EQ(2, (*bound)()); // wrapping and unwrapping. | 534 EXPECT_EQ(1, (*bound)()); // unwrapping. |
527 | 535 |
528 bound = bind(takeCountCopyAsConstReference, CountCopy()); // Rvalue. | 536 bound = bind(takeCountCopyAsConstReference, CountCopy()); // Rvalue. |
529 EXPECT_EQ(2, (*bound)()); | 537 EXPECT_EQ(1, (*bound)()); |
530 | 538 |
531 bound = bind(takeCountCopyAsValue, lvalue); | 539 bound = bind(takeCountCopyAsValue, lvalue); |
532 EXPECT_EQ(3, (*bound)()); // wrapping, unwrapping and copying in the final f
unction argument. | 540 EXPECT_EQ(2, (*bound)()); // unwrapping and copying in the final function ar
gument. |
533 | 541 |
534 bound = bind(takeCountCopyAsValue, CountCopy()); | 542 bound = bind(takeCountCopyAsValue, CountCopy()); |
535 EXPECT_EQ(3, (*bound)()); | 543 EXPECT_EQ(2, (*bound)()); |
536 } | 544 } |
537 | 545 |
538 TEST(FunctionalTest, MoveUnboundArgumentsByRvalueReference) | 546 TEST(FunctionalTest, MoveUnboundArgumentsByRvalueReference) |
539 { | 547 { |
540 std::unique_ptr<Function<int(MoveOnly&&)>> boundSingle = bind(singleMoveOnly
ByRvalueReference); | 548 std::unique_ptr<Function<int(MoveOnly&&)>> boundSingle = bind(singleMoveOnly
ByRvalueReference); |
541 EXPECT_EQ(1, (*boundSingle)(MoveOnly(1))); | 549 EXPECT_EQ(1, (*boundSingle)(MoveOnly(1))); |
542 EXPECT_EQ(42, (*boundSingle)(MoveOnly(42))); | 550 EXPECT_EQ(42, (*boundSingle)(MoveOnly(42))); |
543 | 551 |
544 std::unique_ptr<Function<int(MoveOnly&&, MoveOnly&&, MoveOnly&&)>> boundTrip
le = bind(tripleMoveOnlysByRvalueReferences); | 552 std::unique_ptr<Function<int(MoveOnly&&, MoveOnly&&, MoveOnly&&)>> boundTrip
le = bind(tripleMoveOnlysByRvalueReferences); |
545 EXPECT_EQ(6, (*boundTriple)(MoveOnly(1), MoveOnly(2), MoveOnly(3))); | 553 EXPECT_EQ(6, (*boundTriple)(MoveOnly(1), MoveOnly(2), MoveOnly(3))); |
(...skipping 28 matching lines...) Expand all Loading... |
574 EXPECT_EQ(1, counter); | 582 EXPECT_EQ(1, counter); |
575 | 583 |
576 obj.revokeAll(); | 584 obj.revokeAll(); |
577 (*bound)(); | 585 (*bound)(); |
578 EXPECT_EQ(1, counter); | 586 EXPECT_EQ(1, counter); |
579 } | 587 } |
580 | 588 |
581 } // anonymous namespace | 589 } // anonymous namespace |
582 | 590 |
583 } // namespace WTF | 591 } // namespace WTF |
OLD | NEW |