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