| 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 #include <vector> |
| 9 | 10 |
| 10 #include "base/callback.h" | 11 #include "base/callback.h" |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 15 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 19 |
| (...skipping 758 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 777 | 778 |
| 778 TYPED_TEST(BindMoveOnlyTypeTest, UnboundForwarding) { | 779 TYPED_TEST(BindMoveOnlyTypeTest, UnboundForwarding) { |
| 779 int deletes = 0; | 780 int deletes = 0; |
| 780 TypeParam ptr(new DeleteCounter(&deletes)); | 781 TypeParam ptr(new DeleteCounter(&deletes)); |
| 781 // Test unbound argument forwarding. | 782 // Test unbound argument forwarding. |
| 782 Callback<TypeParam(TypeParam)> cb_unbound = Bind(&PassThru<TypeParam>); | 783 Callback<TypeParam(TypeParam)> cb_unbound = Bind(&PassThru<TypeParam>); |
| 783 cb_unbound.Run(std::move(ptr)); | 784 cb_unbound.Run(std::move(ptr)); |
| 784 EXPECT_EQ(1, deletes); | 785 EXPECT_EQ(1, deletes); |
| 785 } | 786 } |
| 786 | 787 |
| 788 void VerifyVector(const std::vector<scoped_ptr<int>>& v) { |
| 789 ASSERT_EQ(1u, v.size()); |
| 790 EXPECT_EQ(12345, *v[0]); |
| 791 } |
| 792 |
| 793 std::vector<scoped_ptr<int>> AcceptAndReturnMoveOnlyVector( |
| 794 std::vector<scoped_ptr<int>> v) { |
| 795 VerifyVector(v); |
| 796 return v; |
| 797 } |
| 798 |
| 799 // Test that a vector containing move-only types can be used with Callback. |
| 800 TEST_F(BindTest, BindMoveOnlyVector) { |
| 801 using MoveOnlyVector = std::vector<scoped_ptr<int>>; |
| 802 |
| 803 MoveOnlyVector v; |
| 804 v.push_back(make_scoped_ptr(new int(12345))); |
| 805 |
| 806 // Early binding should work: |
| 807 base::Callback<MoveOnlyVector()> bound_cb = |
| 808 base::Bind(&AcceptAndReturnMoveOnlyVector, Passed(&v)); |
| 809 MoveOnlyVector intermediate_result = bound_cb.Run(); |
| 810 VerifyVector(intermediate_result); |
| 811 |
| 812 // As should passing it as an argument to Run(): |
| 813 base::Callback<MoveOnlyVector(MoveOnlyVector)> unbound_cb = |
| 814 base::Bind(&AcceptAndReturnMoveOnlyVector); |
| 815 MoveOnlyVector final_result = unbound_cb.Run(std::move(intermediate_result)); |
| 816 VerifyVector(final_result); |
| 817 } |
| 818 |
| 787 // Argument Copy-constructor usage for non-reference parameters. | 819 // Argument Copy-constructor usage for non-reference parameters. |
| 788 // - Bound arguments are only copied once. | 820 // - Bound arguments are only copied once. |
| 789 // - Forwarded arguments are only copied once. | 821 // - Forwarded arguments are only copied once. |
| 790 // - Forwarded arguments with coercions are only copied twice (once for the | 822 // - Forwarded arguments with coercions are only copied twice (once for the |
| 791 // coercion, and one for the final dispatch). | 823 // coercion, and one for the final dispatch). |
| 792 TEST_F(BindTest, ArgumentCopies) { | 824 TEST_F(BindTest, ArgumentCopies) { |
| 793 int copies = 0; | 825 int copies = 0; |
| 794 int assigns = 0; | 826 int assigns = 0; |
| 795 | 827 |
| 796 CopyCounter counter(&copies, &assigns); | 828 CopyCounter counter(&copies, &assigns); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 852 base::Callback<void(int)> null_cb; | 884 base::Callback<void(int)> null_cb; |
| 853 ASSERT_TRUE(null_cb.is_null()); | 885 ASSERT_TRUE(null_cb.is_null()); |
| 854 EXPECT_DEATH(base::Bind(null_cb, 42), ""); | 886 EXPECT_DEATH(base::Bind(null_cb, 42), ""); |
| 855 } | 887 } |
| 856 | 888 |
| 857 #endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && | 889 #endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && |
| 858 // GTEST_HAS_DEATH_TEST | 890 // GTEST_HAS_DEATH_TEST |
| 859 | 891 |
| 860 } // namespace | 892 } // namespace |
| 861 } // namespace base | 893 } // namespace base |
| OLD | NEW |