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