OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/bind.h" |
| 6 |
| 7 #if defined(BASE_CALLBACK_H_) |
| 8 // We explicitly do not want to include callback.h so people are not tempted |
| 9 // to use bind.h in a headerfile for getting the Callback types. |
| 10 #error "base/bind.h should avoid pulling in callack.h by default." |
| 11 #endif |
| 12 |
| 13 #include "base/callback.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 using ::testing::Mock; |
| 18 using ::testing::Return; |
| 19 using ::testing::StrictMock; |
| 20 |
| 21 namespace base { |
| 22 namespace { |
| 23 |
| 24 class NoRef { |
| 25 public: |
| 26 NoRef() {} |
| 27 |
| 28 MOCK_METHOD0(VoidMethod0, void(void)); |
| 29 MOCK_CONST_METHOD0(VoidConstMethod0, void(void)); |
| 30 |
| 31 MOCK_METHOD0(IntMethod0, int(void)); |
| 32 MOCK_CONST_METHOD0(IntConstMethod0, int(void)); |
| 33 |
| 34 private: |
| 35 // Particularly important in this test to ensure no copies are made. |
| 36 DISALLOW_COPY_AND_ASSIGN(NoRef); |
| 37 }; |
| 38 |
| 39 class HasRef : public NoRef { |
| 40 public: |
| 41 HasRef() {} |
| 42 |
| 43 MOCK_CONST_METHOD0(AddRef, void(void)); |
| 44 MOCK_CONST_METHOD0(Release, bool(void)); |
| 45 |
| 46 private: |
| 47 // Particularly important in this test to ensure no copies are made. |
| 48 DISALLOW_COPY_AND_ASSIGN(HasRef); |
| 49 }; |
| 50 |
| 51 static const int kParentValue = 1; |
| 52 static const int kChildValue = 2; |
| 53 |
| 54 class Parent { |
| 55 public: |
| 56 void AddRef(void) const {} |
| 57 void Release(void) const {} |
| 58 virtual void VirtualSet() { value = kParentValue; } |
| 59 void NonVirtualSet() { value = kParentValue; } |
| 60 int value; |
| 61 }; |
| 62 |
| 63 class Child : public Parent { |
| 64 public: |
| 65 virtual void VirtualSet() { value = kChildValue; } |
| 66 void NonVirtualSet() { value = kChildValue; } |
| 67 }; |
| 68 |
| 69 class NoRefParent { |
| 70 public: |
| 71 virtual void VirtualSet() { value = kParentValue; } |
| 72 void NonVirtualSet() { value = kParentValue; } |
| 73 int value; |
| 74 }; |
| 75 |
| 76 class NoRefChild : public NoRefParent { |
| 77 virtual void VirtualSet() { value = kChildValue; } |
| 78 void NonVirtualSet() { value = kChildValue; } |
| 79 }; |
| 80 |
| 81 // Used for probing the number of copies that occur if a type must be coerced |
| 82 // during argument forwarding in the Run() methods. |
| 83 struct DerivedCopyCounter { |
| 84 DerivedCopyCounter(int* copies, int* assigns) |
| 85 : copies_(copies), assigns_(assigns) { |
| 86 } |
| 87 int* copies_; |
| 88 int* assigns_; |
| 89 }; |
| 90 |
| 91 // Used for probing the number of copies in an argument. |
| 92 class CopyCounter { |
| 93 public: |
| 94 CopyCounter(int* copies, int* assigns) |
| 95 : copies_(copies), assigns_(assigns) { |
| 96 } |
| 97 |
| 98 CopyCounter(const CopyCounter& other) |
| 99 : copies_(other.copies_), |
| 100 assigns_(other.assigns_) { |
| 101 (*copies_)++; |
| 102 } |
| 103 |
| 104 // Probing for copies from coerscion. |
| 105 CopyCounter(const DerivedCopyCounter& other) |
| 106 : copies_(other.copies_), |
| 107 assigns_(other.assigns_) { |
| 108 (*copies_)++; |
| 109 } |
| 110 |
| 111 const CopyCounter& operator=(const CopyCounter& rhs) { |
| 112 copies_ = rhs.copies_; |
| 113 assigns_ = rhs.assigns_; |
| 114 |
| 115 if (assigns_) { |
| 116 (*assigns_)++; |
| 117 } |
| 118 |
| 119 return *this; |
| 120 } |
| 121 |
| 122 int copies() const { |
| 123 return *copies_; |
| 124 } |
| 125 |
| 126 int assigns() const { |
| 127 return *assigns_; |
| 128 } |
| 129 |
| 130 private: |
| 131 int* copies_; |
| 132 int* assigns_; |
| 133 }; |
| 134 |
| 135 // Some test functions that we can Bind to. |
| 136 template <typename T> |
| 137 T PolymorphicIdentity(T t) { |
| 138 return t; |
| 139 } |
| 140 |
| 141 template <typename T> |
| 142 void VoidPolymorphic1(T t) { |
| 143 } |
| 144 |
| 145 int Identity(int n) { |
| 146 return n; |
| 147 } |
| 148 |
| 149 int ArrayGet(const int array[], int n) { |
| 150 return array[n]; |
| 151 } |
| 152 |
| 153 int Sum(int a, int b, int c, int d, int e, int f) { |
| 154 return a + b + c + d + e + f; |
| 155 } |
| 156 |
| 157 const char* CStringIdentity(const char* s) { |
| 158 return s; |
| 159 } |
| 160 |
| 161 int GetCopies(const CopyCounter& counter) { |
| 162 return counter.copies(); |
| 163 } |
| 164 |
| 165 int UnwrapNoRefParent(NoRefParent p) { |
| 166 return p.value; |
| 167 } |
| 168 |
| 169 int UnwrapNoRefParentPtr(NoRefParent* p) { |
| 170 return p->value; |
| 171 } |
| 172 |
| 173 int UnwrapNoRefParentConstRef(const NoRefParent& p) { |
| 174 return p.value; |
| 175 } |
| 176 |
| 177 // Only useful in no-compile tests. |
| 178 int UnwrapNoRefParentRef(Parent& p) { |
| 179 return p.value; |
| 180 } |
| 181 |
| 182 class BindTest : public ::testing::Test { |
| 183 public: |
| 184 BindTest() { |
| 185 const_has_ref_ptr_ = &has_ref_; |
| 186 const_no_ref_ptr_ = &no_ref_; |
| 187 static_func_mock_ptr = &static_func_mock_; |
| 188 } |
| 189 |
| 190 virtual ~BindTest() { |
| 191 } |
| 192 |
| 193 static void VoidFunc0(void) { |
| 194 static_func_mock_ptr->VoidMethod0(); |
| 195 } |
| 196 |
| 197 static int IntFunc0(void) { return static_func_mock_ptr->IntMethod0(); } |
| 198 |
| 199 protected: |
| 200 StrictMock<NoRef> no_ref_; |
| 201 StrictMock<HasRef> has_ref_; |
| 202 const HasRef* const_has_ref_ptr_; |
| 203 const NoRef* const_no_ref_ptr_; |
| 204 StrictMock<NoRef> static_func_mock_; |
| 205 |
| 206 // Used by the static functions to perform expectations. |
| 207 static StrictMock<NoRef>* static_func_mock_ptr; |
| 208 |
| 209 private: |
| 210 DISALLOW_COPY_AND_ASSIGN(BindTest); |
| 211 }; |
| 212 |
| 213 StrictMock<NoRef>* BindTest::static_func_mock_ptr; |
| 214 |
| 215 // Ensure we can create unbound callbacks. We need this to be able to store |
| 216 // them in class members that can be initialized later. |
| 217 TEST_F(BindTest, DefaultConstruction) { |
| 218 Callback<void(void)> c0; |
| 219 Callback<void(int)> c1; |
| 220 Callback<void(int,int)> c2; |
| 221 Callback<void(int,int,int)> c3; |
| 222 Callback<void(int,int,int,int)> c4; |
| 223 Callback<void(int,int,int,int,int)> c5; |
| 224 Callback<void(int,int,int,int,int,int)> c6; |
| 225 } |
| 226 |
| 227 // Sanity check that we can instantiate a callback for each arity. |
| 228 TEST_F(BindTest, ArityTest) { |
| 229 Callback<int(void)> c0 = Bind(&Sum, 32, 16, 8, 4, 2, 1); |
| 230 EXPECT_EQ(63, c0.Run()); |
| 231 |
| 232 Callback<int(int)> c1= Bind(&Sum, 32, 16, 8, 4, 2); |
| 233 EXPECT_EQ(75, c1.Run(13)); |
| 234 |
| 235 Callback<int(int,int)> c2= Bind(&Sum, 32, 16, 8, 4); |
| 236 EXPECT_EQ(85, c2.Run(13, 12)); |
| 237 |
| 238 Callback<int(int,int,int)> c3= Bind(&Sum, 32, 16, 8); |
| 239 EXPECT_EQ(92, c3.Run(13, 12, 11)); |
| 240 |
| 241 Callback<int(int,int,int,int)> c4= Bind(&Sum, 32, 16); |
| 242 EXPECT_EQ(94, c4.Run(13, 12, 11, 10)); |
| 243 |
| 244 Callback<int(int,int,int,int,int)> c5= Bind(&Sum, 32); |
| 245 EXPECT_EQ(87, c5.Run(13, 12, 11, 10, 9)); |
| 246 |
| 247 Callback<int(int,int,int,int,int,int)> c6= Bind(&Sum); |
| 248 EXPECT_EQ(69, c6.Run(13, 12, 11, 10, 9, 14)); |
| 249 } |
| 250 |
| 251 // Function type support. |
| 252 // - Normal function. |
| 253 // - Method bound to non-const object. |
| 254 // - Const method bound to non-const object. |
| 255 // - Const method bound to const object. |
| 256 // - Derived classes can be used with pointers to non-virtual base functions. |
| 257 // - Derived classes can be used with pointers to virtual base functions (and |
| 258 // preserve virtual dispatch). |
| 259 TEST_F(BindTest, FunctionTypeSupport) { |
| 260 EXPECT_CALL(static_func_mock_, VoidMethod0()); |
| 261 EXPECT_CALL(has_ref_, AddRef()).Times(3); |
| 262 EXPECT_CALL(has_ref_, Release()).Times(3); |
| 263 EXPECT_CALL(has_ref_, VoidMethod0()); |
| 264 EXPECT_CALL(has_ref_, VoidConstMethod0()).Times(2); |
| 265 |
| 266 Closure normal_cb = Bind(&VoidFunc0); |
| 267 Closure method_cb = Bind(&HasRef::VoidMethod0, &has_ref_); |
| 268 Closure const_method_nonconst_obj_cb = Bind(&HasRef::VoidConstMethod0, |
| 269 &has_ref_); |
| 270 Closure const_method_const_obj_cb = Bind(&HasRef::VoidConstMethod0, |
| 271 const_has_ref_ptr_); |
| 272 normal_cb.Run(); |
| 273 method_cb.Run(); |
| 274 const_method_nonconst_obj_cb.Run(); |
| 275 const_method_const_obj_cb.Run(); |
| 276 |
| 277 Child child; |
| 278 child.value = 0; |
| 279 Closure virtual_set_cb = Bind(&Parent::VirtualSet, &child); |
| 280 virtual_set_cb.Run(); |
| 281 EXPECT_EQ(kChildValue, child.value); |
| 282 |
| 283 child.value = 0; |
| 284 Closure non_virtual_set_cb = Bind(&Parent::NonVirtualSet, &child); |
| 285 non_virtual_set_cb.Run(); |
| 286 EXPECT_EQ(kParentValue, child.value); |
| 287 } |
| 288 |
| 289 // Return value support. |
| 290 // - Function with return value. |
| 291 // - Method with return value. |
| 292 // - Const method with return value. |
| 293 TEST_F(BindTest, ReturnValues) { |
| 294 EXPECT_CALL(static_func_mock_, IntMethod0()).WillOnce(Return(1337)); |
| 295 EXPECT_CALL(has_ref_, AddRef()).Times(3); |
| 296 EXPECT_CALL(has_ref_, Release()).Times(3); |
| 297 EXPECT_CALL(has_ref_, IntMethod0()).WillOnce(Return(31337)); |
| 298 EXPECT_CALL(has_ref_, IntConstMethod0()) |
| 299 .WillOnce(Return(41337)) |
| 300 .WillOnce(Return(51337)); |
| 301 |
| 302 Callback<int(void)> normal_cb = Bind(&IntFunc0); |
| 303 Callback<int(void)> method_cb = Bind(&HasRef::IntMethod0, &has_ref_); |
| 304 Callback<int(void)> const_method_nonconst_obj_cb = |
| 305 Bind(&HasRef::IntConstMethod0, &has_ref_); |
| 306 Callback<int(void)> const_method_const_obj_cb = |
| 307 Bind(&HasRef::IntConstMethod0, const_has_ref_ptr_); |
| 308 EXPECT_EQ(1337, normal_cb.Run()); |
| 309 EXPECT_EQ(31337, method_cb.Run()); |
| 310 EXPECT_EQ(41337, const_method_nonconst_obj_cb.Run()); |
| 311 EXPECT_EQ(51337, const_method_const_obj_cb.Run()); |
| 312 } |
| 313 |
| 314 // Argument binding tests. |
| 315 // - Argument binding to primitive. |
| 316 // - Argument binding to a literal integer. |
| 317 // - Argument binding to a literal string. |
| 318 // - Argument binding with template function. |
| 319 // - Argument binding to an object. |
| 320 // - Argument gets type converted. |
| 321 // - Pointer argument gets converted. |
| 322 // - Const Reference forces conversion. |
| 323 TEST_F(BindTest, ArgumentBinding) { |
| 324 int n = 2; |
| 325 |
| 326 Callback<int(void)> bind_primitive_cb = Bind(&Identity, n); |
| 327 EXPECT_EQ(n, bind_primitive_cb.Run()); |
| 328 |
| 329 Callback<int(void)> bind_int_literal_cb = Bind(&Identity, 3); |
| 330 EXPECT_EQ(3, bind_int_literal_cb.Run()); |
| 331 |
| 332 Callback<const char*(void)> bind_string_literal_cb = |
| 333 Bind(&CStringIdentity, "hi"); |
| 334 EXPECT_STREQ("hi", bind_string_literal_cb.Run()); |
| 335 |
| 336 Callback<int(void)> bind_template_function_cb = |
| 337 Bind(&PolymorphicIdentity<int>, 4); |
| 338 EXPECT_EQ(4, bind_template_function_cb.Run()); |
| 339 |
| 340 NoRefParent p; |
| 341 p.value = 5; |
| 342 Callback<int(void)> bind_object_cb = Bind(&UnwrapNoRefParent, p); |
| 343 EXPECT_EQ(5, bind_object_cb.Run()); |
| 344 |
| 345 NoRefChild c; |
| 346 c.value = 6; |
| 347 Callback<int(void)> bind_promotes_cb = Bind(&UnwrapNoRefParent, c); |
| 348 EXPECT_EQ(6, bind_promotes_cb.Run()); |
| 349 |
| 350 c.value = 7; |
| 351 Callback<int(void)> bind_pointer_promotes_cb = |
| 352 Bind(&UnwrapNoRefParentPtr, &c); |
| 353 EXPECT_EQ(7, bind_pointer_promotes_cb.Run()); |
| 354 |
| 355 c.value = 8; |
| 356 Callback<int(void)> bind_const_reference_promotes_cb = |
| 357 Bind(&UnwrapNoRefParentConstRef, c); |
| 358 EXPECT_EQ(8, bind_const_reference_promotes_cb.Run()); |
| 359 } |
| 360 |
| 361 // Functions that take reference parameters. |
| 362 // - Forced reference parameter type still stores a copy. |
| 363 // - Forced const reference parameter type still stores a copy. |
| 364 TEST_F(BindTest, ReferenceArgumentBinding) { |
| 365 int n = 1; |
| 366 int& ref_n = n; |
| 367 const int& const_ref_n = n; |
| 368 |
| 369 Callback<int(void)> ref_copies_cb = Bind(&Identity, ref_n); |
| 370 EXPECT_EQ(n, ref_copies_cb.Run()); |
| 371 n++; |
| 372 EXPECT_EQ(n - 1, ref_copies_cb.Run()); |
| 373 |
| 374 Callback<int(void)> const_ref_copies_cb = Bind(&Identity, const_ref_n); |
| 375 EXPECT_EQ(n, const_ref_copies_cb.Run()); |
| 376 n++; |
| 377 EXPECT_EQ(n - 1, const_ref_copies_cb.Run()); |
| 378 } |
| 379 |
| 380 // Check that we can pass in arrays and have them be stored as a pointer. |
| 381 // - Array of values stores a pointer. |
| 382 // - Array of const values stores a pointer. |
| 383 TEST_F(BindTest, ArrayArgumentBinding) { |
| 384 int array[4] = {1, 1, 1, 1}; |
| 385 const int (*const_array_ptr)[4] = &array; |
| 386 |
| 387 Callback<int(void)> array_cb = Bind(&ArrayGet, array, 1); |
| 388 EXPECT_EQ(1, array_cb.Run()); |
| 389 |
| 390 Callback<int(void)> const_array_cb = Bind(&ArrayGet, *const_array_ptr, 1); |
| 391 EXPECT_EQ(1, const_array_cb.Run()); |
| 392 |
| 393 array[1] = 3; |
| 394 EXPECT_EQ(3, array_cb.Run()); |
| 395 EXPECT_EQ(3, const_array_cb.Run()); |
| 396 } |
| 397 |
| 398 // Verify SupportsAddRefAndRelease correctly introspects the class type for |
| 399 // AddRef() and Release(). |
| 400 TEST_F(BindTest, SupportsAddRefAndRelease) { |
| 401 EXPECT_TRUE(internal::SupportsAddRefAndRelease<HasRef>::value); |
| 402 EXPECT_FALSE(internal::SupportsAddRefAndRelease<NoRef>::value); |
| 403 |
| 404 // StrictMock<T> is a derived class of T. So, we use StrictMock<HasRef> and |
| 405 // StrictMock<NoRef> to test that SupportsAddRefAndRelease works over |
| 406 // inheritance. |
| 407 EXPECT_TRUE(internal::SupportsAddRefAndRelease<StrictMock<HasRef> >::value); |
| 408 EXPECT_FALSE(internal::SupportsAddRefAndRelease<StrictMock<NoRef> >::value); |
| 409 } |
| 410 |
| 411 // Unretained() wrapper support. |
| 412 // - Method bound to Unretained() non-object. |
| 413 // - Const method bound to Unretained() non-const object. |
| 414 // - Const method bound to Unretained() const object. |
| 415 TEST_F(BindTest, Unretained) { |
| 416 EXPECT_CALL(no_ref_, VoidMethod0()); |
| 417 EXPECT_CALL(no_ref_, VoidConstMethod0()).Times(2); |
| 418 |
| 419 Callback<void(void)> method_cb = |
| 420 Bind(&NoRef::VoidMethod0, Unretained(&no_ref_)); |
| 421 method_cb.Run(); |
| 422 |
| 423 Callback<void(void)> const_method_cb = |
| 424 Bind(&NoRef::VoidConstMethod0, Unretained(&no_ref_)); |
| 425 const_method_cb.Run(); |
| 426 |
| 427 Callback<void(void)> const_method_const_ptr_cb = |
| 428 Bind(&NoRef::VoidConstMethod0, Unretained(const_no_ref_ptr_)); |
| 429 const_method_const_ptr_cb.Run(); |
| 430 } |
| 431 |
| 432 // ConstRef() wrapper support. |
| 433 // - Binding w/o ConstRef takes a copy. |
| 434 // - Binding a ConstRef takes a reference. |
| 435 // - Binding ConstRef to a function ConstRef does not copy on invoke. |
| 436 TEST_F(BindTest, ConstRef) { |
| 437 int n = 1; |
| 438 |
| 439 Callback<int(void)> copy_cb = Bind(&Identity, n); |
| 440 Callback<int(void)> const_ref_cb = Bind(&Identity, ConstRef(n)); |
| 441 EXPECT_EQ(n, copy_cb.Run()); |
| 442 EXPECT_EQ(n, const_ref_cb.Run()); |
| 443 n++; |
| 444 EXPECT_EQ(n - 1, copy_cb.Run()); |
| 445 EXPECT_EQ(n, const_ref_cb.Run()); |
| 446 |
| 447 int copies = 0; |
| 448 int assigns = 0; |
| 449 CopyCounter counter(&copies, &assigns); |
| 450 Callback<int(void)> all_const_ref_cb = |
| 451 Bind(&GetCopies, ConstRef(counter)); |
| 452 EXPECT_EQ(0, all_const_ref_cb.Run()); |
| 453 EXPECT_EQ(0, copies); |
| 454 EXPECT_EQ(0, assigns); |
| 455 } |
| 456 |
| 457 // Argument Copy-constructor usage for non-reference parameters. |
| 458 // - Bound arguments are only copied once. |
| 459 // - Forwarded arguments are only copied once. |
| 460 // - Forwarded arguments with coerscions are only copied twice (once for the |
| 461 // coerscion, and one for the final dispatch). |
| 462 TEST_F(BindTest, ArgumentCopies) { |
| 463 int copies = 0; |
| 464 int assigns = 0; |
| 465 |
| 466 CopyCounter counter(&copies, &assigns); |
| 467 |
| 468 Callback<void(void)> copy_cb = |
| 469 Bind(&VoidPolymorphic1<CopyCounter>, counter); |
| 470 EXPECT_GE(1, copies); |
| 471 EXPECT_EQ(0, assigns); |
| 472 |
| 473 copies = 0; |
| 474 assigns = 0; |
| 475 Callback<void(CopyCounter)> forward_cb = |
| 476 Bind(&VoidPolymorphic1<CopyCounter>); |
| 477 forward_cb.Run(counter); |
| 478 EXPECT_GE(1, copies); |
| 479 EXPECT_EQ(0, assigns); |
| 480 |
| 481 copies = 0; |
| 482 assigns = 0; |
| 483 DerivedCopyCounter dervied(&copies, &assigns); |
| 484 Callback<void(CopyCounter)> coerce_cb = |
| 485 Bind(&VoidPolymorphic1<CopyCounter>); |
| 486 coerce_cb.Run(dervied); |
| 487 EXPECT_GE(2, copies); |
| 488 EXPECT_EQ(0, assigns); |
| 489 } |
| 490 |
| 491 // Callback construction and assignment tests. |
| 492 // - Construction from an InvokerStorageHolder should not cause ref/deref. |
| 493 // - Assignment from other callback should only cause one ref |
| 494 // |
| 495 // TODO(ajwong): Is there actually a way to test this? |
| 496 |
| 497 // No-compile tests. These should not compile. If they do, we are allowing |
| 498 // error-prone, or incorrect behavior in the callback system. Uncomment the |
| 499 // tests to check. |
| 500 TEST_F(BindTest, NoCompile) { |
| 501 // - Method bound to const-object. |
| 502 // |
| 503 // Only const methods should be allowed to work with const objects. |
| 504 // |
| 505 // Callback<void(void)> method_to_const_cb = |
| 506 // Bind(&HasRef::VoidMethod0, const_has_ref_ptr_); |
| 507 // method_to_const_cb.Run(); |
| 508 |
| 509 // - Method bound to non-refcounted object. |
| 510 // - Const Method bound to non-refcounted object. |
| 511 // |
| 512 // We require refcounts unless you have Unretained(). |
| 513 // |
| 514 // Callback<void(void)> no_ref_cb = |
| 515 // Bind(&NoRef::VoidMethod0, &no_ref_); |
| 516 // no_ref_cb.Run(); |
| 517 // Callback<void(void)> no_ref_const_cb = |
| 518 // Bind(&NoRef::VoidConstMethod0, &no_ref_); |
| 519 // no_ref_const_cb.Run(); |
| 520 |
| 521 // - Unretained() used with a refcounted object. |
| 522 // |
| 523 // If the object supports refcounts, unretaining it in the callback is a |
| 524 // memory management contract break. |
| 525 // Callback<void(void)> unretained_cb = |
| 526 // Bind(&HasRef::VoidConstMethod0, Unretained(&has_ref_)); |
| 527 // unretained_cb.Run(); |
| 528 |
| 529 // - Const argument used with non-const pointer parameter of same type. |
| 530 // - Const argument used with non-const pointer parameter of super type. |
| 531 // |
| 532 // This is just a const-correctness check. |
| 533 // |
| 534 // const Parent* const_parent_ptr; |
| 535 // const Child* const_child_ptr; |
| 536 // Callback<Parent*(void)> pointer_same_cb = |
| 537 // Bind(&PolymorphicIdentity<Parent*>, const_parent_ptr); |
| 538 // pointer_same_cb.Run(); |
| 539 // Callback<Parent*(void)> pointer_super_cb = |
| 540 // Bind(&PolymorphicIdentity<Parent*>, const_child_ptr); |
| 541 // pointer_super_cb.Run(); |
| 542 |
| 543 // - Construction of Callback<A> from Callback<B> if A is supertype of B. |
| 544 // Specific example: Callback<void(void)> a; Callback<int(void)> b; a = b; |
| 545 // |
| 546 // While this is technically safe, most people aren't used to it when coding |
| 547 // C++ so if this is happening, it is almost certainly an error. |
| 548 // |
| 549 // Callback<int(void)> cb_a0 = Bind(&Identity, 1); |
| 550 // Callback<void(void)> cb_b0 = cb_a0; |
| 551 |
| 552 // - Assignment of Callback<A> from Callback<B> if A is supertype of B. |
| 553 // See explanation above. |
| 554 // |
| 555 // Callback<int(void)> cb_a1 = Bind(&Identity, 1); |
| 556 // Callback<void(void)> cb_b1; |
| 557 // cb_a1 = cb_b1; |
| 558 |
| 559 // - Functions with reference parameters, unsupported. |
| 560 // |
| 561 // First, non-const reference parameters are disallowed by the Google |
| 562 // style guide. Seconds, since we are doing argument forwarding it becomes |
| 563 // very tricky to avoid copies, maintain const correctness, and not |
| 564 // accidentally have the function be modifying a temporary, or a copy. |
| 565 // |
| 566 // NoRefParent p; |
| 567 // Callback<int(Parent&)> ref_arg_cb = Bind(&UnwrapNoRefParentRef); |
| 568 // ref_arg_cb.Run(p); |
| 569 // Callback<int(void)> ref_cb = Bind(&UnwrapNoRefParentRef, p); |
| 570 // ref_cb.Run(); |
| 571 |
| 572 // - A method should not be bindable with an array of objects. |
| 573 // |
| 574 // This is likely not wanted behavior. We specifically check for it though |
| 575 // because it is possible, depending on how you implement prebinding, to |
| 576 // implicitly convert an array type to a pointer type. |
| 577 // |
| 578 // HasRef p[10]; |
| 579 // Callback<void(void)> method_bound_to_array_cb = |
| 580 // Bind(&HasRef::VoidConstMethod0, p); |
| 581 // method_bound_to_array_cb.Run(); |
| 582 |
| 583 // - Refcounted types should not be bound as a raw pointer. |
| 584 // HasRef for_raw_ptr; |
| 585 // Callback<void(void)> ref_count_as_raw_ptr = |
| 586 // Bind(&VoidPolymorphic1<HasRef*>, &for_raw_ptr); |
| 587 // ASSERT_EQ(&for_raw_ptr, ref_count_as_raw_ptr.Run()); |
| 588 |
| 589 } |
| 590 |
| 591 } // namespace |
| 592 } // namespace base |
OLD | NEW |