Chromium Code Reviews| 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 <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 155 T PassThru(T scoper) { | 155 T PassThru(T scoper) { |
| 156 return scoper; | 156 return scoper; |
| 157 } | 157 } |
| 158 | 158 |
| 159 // Some test functions that we can Bind to. | 159 // Some test functions that we can Bind to. |
| 160 template <typename T> | 160 template <typename T> |
| 161 T PolymorphicIdentity(T t) { | 161 T PolymorphicIdentity(T t) { |
| 162 return t; | 162 return t; |
| 163 } | 163 } |
| 164 | 164 |
| 165 template <typename T> | 165 template <typename... Ts> |
| 166 void VoidPolymorphic1(T t) { | 166 struct VoidPolymorphic { |
|
danakj
2015/12/16 20:00:41
I'm not opposed, but why wrap the method in a stru
tzik
2015/12/17 01:42:36
This is for gcc compilation error.
Taking the fun
danakj
2015/12/18 00:08:58
Ah, cool. Thanks!
| |
| 167 } | 167 static void Run(Ts... t) {} |
| 168 }; | |
| 168 | 169 |
| 169 int Identity(int n) { | 170 int Identity(int n) { |
| 170 return n; | 171 return n; |
| 171 } | 172 } |
| 172 | 173 |
| 173 int ArrayGet(const int array[], int n) { | 174 int ArrayGet(const int array[], int n) { |
| 174 return array[n]; | 175 return array[n]; |
| 175 } | 176 } |
| 176 | 177 |
| 177 int Sum(int a, int b, int c, int d, int e, int f) { | 178 int Sum(int a, int b, int c, int d, int e, int f) { |
| (...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 492 | 493 |
| 493 // Unbound argument type support tests. | 494 // Unbound argument type support tests. |
| 494 // - Unbound value. | 495 // - Unbound value. |
| 495 // - Unbound pointer. | 496 // - Unbound pointer. |
| 496 // - Unbound reference. | 497 // - Unbound reference. |
| 497 // - Unbound const reference. | 498 // - Unbound const reference. |
| 498 // - Unbound unsized array. | 499 // - Unbound unsized array. |
| 499 // - Unbound sized array. | 500 // - Unbound sized array. |
| 500 // - Unbound array-of-arrays. | 501 // - Unbound array-of-arrays. |
| 501 TEST_F(BindTest, UnboundArgumentTypeSupport) { | 502 TEST_F(BindTest, UnboundArgumentTypeSupport) { |
| 502 Callback<void(int)> unbound_value_cb = Bind(&VoidPolymorphic1<int>); | 503 Callback<void(int)> unbound_value_cb = Bind(&VoidPolymorphic<int>::Run); |
| 503 Callback<void(int*)> unbound_pointer_cb = Bind(&VoidPolymorphic1<int*>); | 504 Callback<void(int*)> unbound_pointer_cb = Bind(&VoidPolymorphic<int*>::Run); |
| 504 Callback<void(int&)> unbound_ref_cb = Bind(&VoidPolymorphic1<int&>); | 505 Callback<void(int&)> unbound_ref_cb = Bind(&VoidPolymorphic<int&>::Run); |
| 505 Callback<void(const int&)> unbound_const_ref_cb = | 506 Callback<void(const int&)> unbound_const_ref_cb = |
| 506 Bind(&VoidPolymorphic1<const int&>); | 507 Bind(&VoidPolymorphic<const int&>::Run); |
| 507 Callback<void(int[])> unbound_unsized_array_cb = | 508 Callback<void(int[])> unbound_unsized_array_cb = |
| 508 Bind(&VoidPolymorphic1<int[]>); | 509 Bind(&VoidPolymorphic<int[]>::Run); |
| 509 Callback<void(int[2])> unbound_sized_array_cb = | 510 Callback<void(int[2])> unbound_sized_array_cb = |
| 510 Bind(&VoidPolymorphic1<int[2]>); | 511 Bind(&VoidPolymorphic<int[2]>::Run); |
| 511 Callback<void(int[][2])> unbound_array_of_arrays_cb = | 512 Callback<void(int[][2])> unbound_array_of_arrays_cb = |
| 512 Bind(&VoidPolymorphic1<int[][2]>); | 513 Bind(&VoidPolymorphic<int[][2]>::Run); |
| 514 | |
| 515 Callback<void(int&)> unbound_ref_with_bound_arg = | |
| 516 Bind(&VoidPolymorphic<int, int&>::Run, 1); | |
| 513 } | 517 } |
| 514 | 518 |
| 515 // Function with unbound reference parameter. | 519 // Function with unbound reference parameter. |
| 516 // - Original parameter is modified by callback. | 520 // - Original parameter is modified by callback. |
| 517 TEST_F(BindTest, UnboundReferenceSupport) { | 521 TEST_F(BindTest, UnboundReferenceSupport) { |
| 518 int n = 0; | 522 int n = 0; |
| 519 Callback<void(int&)> unbound_ref_cb = Bind(&RefArgSet); | 523 Callback<void(int&)> unbound_ref_cb = Bind(&RefArgSet); |
| 520 unbound_ref_cb.Run(n); | 524 unbound_ref_cb.Run(n); |
| 521 EXPECT_EQ(2, n); | 525 EXPECT_EQ(2, n); |
| 522 } | 526 } |
| (...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 803 // - Forwarded arguments are only copied once. | 807 // - Forwarded arguments are only copied once. |
| 804 // - Forwarded arguments with coercions are only copied twice (once for the | 808 // - Forwarded arguments with coercions are only copied twice (once for the |
| 805 // coercion, and one for the final dispatch). | 809 // coercion, and one for the final dispatch). |
| 806 TEST_F(BindTest, ArgumentCopies) { | 810 TEST_F(BindTest, ArgumentCopies) { |
| 807 int copies = 0; | 811 int copies = 0; |
| 808 int assigns = 0; | 812 int assigns = 0; |
| 809 | 813 |
| 810 CopyCounter counter(&copies, &assigns); | 814 CopyCounter counter(&copies, &assigns); |
| 811 | 815 |
| 812 Callback<void(void)> copy_cb = | 816 Callback<void(void)> copy_cb = |
| 813 Bind(&VoidPolymorphic1<CopyCounter>, counter); | 817 Bind(&VoidPolymorphic<CopyCounter>::Run, counter); |
| 814 EXPECT_GE(1, copies); | 818 EXPECT_GE(1, copies); |
| 815 EXPECT_EQ(0, assigns); | 819 EXPECT_EQ(0, assigns); |
| 816 | 820 |
| 817 copies = 0; | 821 copies = 0; |
| 818 assigns = 0; | 822 assigns = 0; |
| 819 Callback<void(CopyCounter)> forward_cb = | 823 Callback<void(CopyCounter)> forward_cb = |
| 820 Bind(&VoidPolymorphic1<CopyCounter>); | 824 Bind(&VoidPolymorphic<CopyCounter>::Run); |
| 821 forward_cb.Run(counter); | 825 forward_cb.Run(counter); |
| 822 EXPECT_GE(1, copies); | 826 EXPECT_GE(1, copies); |
| 823 EXPECT_EQ(0, assigns); | 827 EXPECT_EQ(0, assigns); |
| 824 | 828 |
| 825 copies = 0; | 829 copies = 0; |
| 826 assigns = 0; | 830 assigns = 0; |
| 827 DerivedCopyCounter derived(&copies, &assigns); | 831 DerivedCopyCounter derived(&copies, &assigns); |
| 828 Callback<void(CopyCounter)> coerce_cb = | 832 Callback<void(CopyCounter)> coerce_cb = |
| 829 Bind(&VoidPolymorphic1<CopyCounter>); | 833 Bind(&VoidPolymorphic<CopyCounter>::Run); |
| 830 coerce_cb.Run(CopyCounter(derived)); | 834 coerce_cb.Run(CopyCounter(derived)); |
| 831 EXPECT_GE(2, copies); | 835 EXPECT_GE(2, copies); |
| 832 EXPECT_EQ(0, assigns); | 836 EXPECT_EQ(0, assigns); |
| 833 } | 837 } |
| 834 | 838 |
| 835 // Callback construction and assignment tests. | 839 // Callback construction and assignment tests. |
| 836 // - Construction from an InvokerStorageHolder should not cause ref/deref. | 840 // - Construction from an InvokerStorageHolder should not cause ref/deref. |
| 837 // - Assignment from other callback should only cause one ref | 841 // - Assignment from other callback should only cause one ref |
| 838 // | 842 // |
| 839 // TODO(ajwong): Is there actually a way to test this? | 843 // TODO(ajwong): Is there actually a way to test this? |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 866 base::Callback<void(int)> null_cb; | 870 base::Callback<void(int)> null_cb; |
| 867 ASSERT_TRUE(null_cb.is_null()); | 871 ASSERT_TRUE(null_cb.is_null()); |
| 868 EXPECT_DEATH(base::Bind(null_cb, 42), ""); | 872 EXPECT_DEATH(base::Bind(null_cb, 42), ""); |
| 869 } | 873 } |
| 870 | 874 |
| 871 #endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && | 875 #endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && |
| 872 // GTEST_HAS_DEATH_TEST | 876 // GTEST_HAS_DEATH_TEST |
| 873 | 877 |
| 874 } // namespace | 878 } // namespace |
| 875 } // namespace base | 879 } // namespace base |
| OLD | NEW |