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