OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 } | 200 } |
201 | 201 |
202 int UnwrapNoRefParentConstRef(const NoRefParent& p) { | 202 int UnwrapNoRefParentConstRef(const NoRefParent& p) { |
203 return p.value; | 203 return p.value; |
204 } | 204 } |
205 | 205 |
206 void RefArgSet(int &n) { | 206 void RefArgSet(int &n) { |
207 n = 2; | 207 n = 2; |
208 } | 208 } |
209 | 209 |
| 210 void PtrArgSet(int *n) { |
| 211 *n = 2; |
| 212 } |
| 213 |
210 int FunctionWithWeakFirstParam(WeakPtr<NoRef> o, int n) { | 214 int FunctionWithWeakFirstParam(WeakPtr<NoRef> o, int n) { |
211 return n; | 215 return n; |
212 } | 216 } |
213 | 217 |
| 218 void TakesACallback(const Closure& callback) { |
| 219 callback.Run(); |
| 220 } |
| 221 |
214 class BindTest : public ::testing::Test { | 222 class BindTest : public ::testing::Test { |
215 public: | 223 public: |
216 BindTest() { | 224 BindTest() { |
217 const_has_ref_ptr_ = &has_ref_; | 225 const_has_ref_ptr_ = &has_ref_; |
218 const_no_ref_ptr_ = &no_ref_; | 226 const_no_ref_ptr_ = &no_ref_; |
219 static_func_mock_ptr = &static_func_mock_; | 227 static_func_mock_ptr = &static_func_mock_; |
220 } | 228 } |
221 | 229 |
222 virtual ~BindTest() { | 230 virtual ~BindTest() { |
223 } | 231 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 Callback<int(int,int)> c2 = Bind(c3, 4); | 293 Callback<int(int,int)> c2 = Bind(c3, 4); |
286 EXPECT_EQ(85, c2.Run(13, 12)); | 294 EXPECT_EQ(85, c2.Run(13, 12)); |
287 | 295 |
288 Callback<int(int)> c1 = Bind(c2, 2); | 296 Callback<int(int)> c1 = Bind(c2, 2); |
289 EXPECT_EQ(75, c1.Run(13)); | 297 EXPECT_EQ(75, c1.Run(13)); |
290 | 298 |
291 Callback<int(void)> c0 = Bind(c1, 1); | 299 Callback<int(void)> c0 = Bind(c1, 1); |
292 EXPECT_EQ(63, c0.Run()); | 300 EXPECT_EQ(63, c0.Run()); |
293 } | 301 } |
294 | 302 |
| 303 // Test that currying the rvalue result of another Bind() works correctly. |
| 304 // - rvalue should be usable as argument to Bind(). |
| 305 // - multiple runs of resulting Callback remain valid. |
| 306 TEST_F(BindTest, CurryingRvalueResultOfBind) { |
| 307 int n = 0; |
| 308 Closure cb = base::Bind(&TakesACallback, base::Bind(&PtrArgSet, &n)); |
| 309 |
| 310 // If we implement Bind() such that the return value has auto_ptr-like |
| 311 // semantics, the second call here will fail because ownership of |
| 312 // the internal BindState<> would have been transfered to a *temporary* |
| 313 // constructon of a Callback object on the first call. |
| 314 cb.Run(); |
| 315 EXPECT_EQ(2, n); |
| 316 |
| 317 n = 0; |
| 318 cb.Run(); |
| 319 EXPECT_EQ(2, n); |
| 320 } |
| 321 |
295 // Function type support. | 322 // Function type support. |
296 // - Normal function. | 323 // - Normal function. |
297 // - Normal function bound with non-refcounted first argument. | 324 // - Normal function bound with non-refcounted first argument. |
298 // - Method bound to non-const object. | 325 // - Method bound to non-const object. |
299 // - Method bound to scoped_refptr. | 326 // - Method bound to scoped_refptr. |
300 // - Const method bound to non-const object. | 327 // - Const method bound to non-const object. |
301 // - Const method bound to const object. | 328 // - Const method bound to const object. |
302 // - Derived classes can be used with pointers to non-virtual base functions. | 329 // - Derived classes can be used with pointers to non-virtual base functions. |
303 // - Derived classes can be used with pointers to virtual base functions (and | 330 // - Derived classes can be used with pointers to virtual base functions (and |
304 // preserve virtual dispatch). | 331 // preserve virtual dispatch). |
(...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
770 Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1); | 797 Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1); |
771 EXPECT_EQ(1, fastcall_cb.Run()); | 798 EXPECT_EQ(1, fastcall_cb.Run()); |
772 | 799 |
773 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2); | 800 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2); |
774 EXPECT_EQ(2, stdcall_cb.Run()); | 801 EXPECT_EQ(2, stdcall_cb.Run()); |
775 } | 802 } |
776 #endif | 803 #endif |
777 | 804 |
778 } // namespace | 805 } // namespace |
779 } // namespace base | 806 } // namespace base |
OLD | NEW |