OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/bind.h" | 5 #include "base/bind.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
(...skipping 768 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
779 copies = 0; | 779 copies = 0; |
780 assigns = 0; | 780 assigns = 0; |
781 DerivedCopyCounter dervied(&copies, &assigns); | 781 DerivedCopyCounter dervied(&copies, &assigns); |
782 Callback<void(CopyCounter)> coerce_cb = | 782 Callback<void(CopyCounter)> coerce_cb = |
783 Bind(&VoidPolymorphic1<CopyCounter>); | 783 Bind(&VoidPolymorphic1<CopyCounter>); |
784 coerce_cb.Run(CopyCounter(dervied)); | 784 coerce_cb.Run(CopyCounter(dervied)); |
785 EXPECT_GE(2, copies); | 785 EXPECT_GE(2, copies); |
786 EXPECT_EQ(0, assigns); | 786 EXPECT_EQ(0, assigns); |
787 } | 787 } |
788 | 788 |
| 789 // Test using lambdas with Bind |
| 790 struct Dummy {}; |
| 791 struct RefcountedClass; |
| 792 void Func1(RefcountedClass*, Dummy*) {} |
| 793 void Func2(Dummy*, RefcountedClass*) {} |
| 794 struct RefcountedClass : public base::RefCounted<RefcountedClass> { |
| 795 void TryStuffWithThis() { |
| 796 scoped_refptr<RefcountedClass> refcounted_object; |
| 797 Dummy d; |
| 798 base::Closure c; |
| 799 c = base::Bind(&Func1, |
| 800 this, |
| 801 base::Unretained(&d)); |
| 802 c = base::Bind(&Func2, |
| 803 base::Unretained(&d), |
| 804 this); |
| 805 } |
| 806 private: |
| 807 friend class base::RefCounted<RefcountedClass>; |
| 808 ~RefcountedClass() {} |
| 809 }; |
| 810 |
789 // Callback construction and assignment tests. | 811 // Callback construction and assignment tests. |
790 // - Construction from an InvokerStorageHolder should not cause ref/deref. | 812 // - Construction from an InvokerStorageHolder should not cause ref/deref. |
791 // - Assignment from other callback should only cause one ref | 813 // - Assignment from other callback should only cause one ref |
792 // | 814 // |
793 // TODO(ajwong): Is there actually a way to test this? | 815 // TODO(ajwong): Is there actually a way to test this? |
794 | 816 |
795 #if defined(OS_WIN) | 817 #if defined(OS_WIN) |
796 int __fastcall FastCallFunc(int n) { | 818 int __fastcall FastCallFunc(int n) { |
797 return n; | 819 return n; |
798 } | 820 } |
(...skipping 21 matching lines...) Expand all Loading... |
820 base::Callback<void(int)> null_cb; | 842 base::Callback<void(int)> null_cb; |
821 ASSERT_TRUE(null_cb.is_null()); | 843 ASSERT_TRUE(null_cb.is_null()); |
822 EXPECT_DEATH(base::Bind(null_cb, 42), ""); | 844 EXPECT_DEATH(base::Bind(null_cb, 42), ""); |
823 } | 845 } |
824 | 846 |
825 #endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && | 847 #endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && |
826 // GTEST_HAS_DEATH_TEST | 848 // GTEST_HAS_DEATH_TEST |
827 | 849 |
828 } // namespace | 850 } // namespace |
829 } // namespace base | 851 } // namespace base |
OLD | NEW |