blob: eba8747bb26b64ef8abf2f03baaec86228acbf64 [file] [log] [blame]
[email protected]81814bce2011-09-10 03:03:001// 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/callback.h"
6#include "base/bind.h"
7
8namespace base {
9
10// Do not put everything inside an anonymous namespace. If you do, many of the
11// helper function declarations will generate unused definition warnings unless
12// unused definition warnings.
13
14static const int kParentValue = 1;
15static const int kChildValue = 2;
16
17class NoRef {
18 public:
19 void VoidMethod0() {}
20 void VoidConstMethod0() const {}
21 int IntMethod0() { return 1; }
22};
23
24class HasRef : public NoRef {
25 public:
26 void AddRef(void) const {}
27 bool Release(void) const { return true; }
28};
29
30class Parent {
31 public:
32 void AddRef(void) const {}
33 void Release(void) const {}
34 virtual void VirtualSet() { value = kParentValue; }
35 void NonVirtualSet() { value = kParentValue; }
36 int value;
37};
38
39class Child : public Parent {
40 public:
41 virtual void VirtualSet() { value = kChildValue; }
42 void NonVirtualSet() { value = kChildValue; }
43};
44
45class NoRefParent {
46 public:
47 virtual void VirtualSet() { value = kParentValue; }
48 void NonVirtualSet() { value = kParentValue; }
49 int value;
50};
51
52class NoRefChild : public NoRefParent {
53 virtual void VirtualSet() { value = kChildValue; }
54 void NonVirtualSet() { value = kChildValue; }
55};
56
57template <typename T>
58T PolymorphicIdentity(T t) {
59 return t;
60}
61
62int UnwrapParentRef(Parent& p) {
63 return p.value;
64}
65
66template <typename T>
67void VoidPolymorphic1(T t) {
68}
69
70#if defined(NCTEST_METHOD_ON_CONST_OBJECT) // [r"invalid conversion from 'const base::NoRef\*' to 'base::NoRef\*'"]
71
72// Method bound to const-object.
73//
74// Only const methods should be allowed to work with const objects.
75void WontCompile() {
76 HasRef has_ref;
77 const HasRef* const_has_ref_ptr_ = &has_ref;
78 Callback<void(void)> method_to_const_cb =
79 Bind(&HasRef::VoidMethod0, const_has_ref_ptr_);
80 method_to_const_cb.Run();
81}
82
[email protected]81814bce2011-09-10 03:03:0083#elif defined(NCTEST_METHOD_BIND_NEEDS_REFCOUNTED_OBJECT) // [r"has no member named 'AddRef'"]
84
85// Method bound to non-refcounted object.
86//
87// We require refcounts unless you have Unretained().
88void WontCompile() {
89 NoRef no_ref;
90 Callback<void(void)> no_ref_cb =
91 Bind(&NoRef::VoidMethod0, &no_ref);
92 no_ref_cb.Run();
93}
94
95#elif defined(NCTEST_CONST_METHOD_NEEDS_REFCOUNTED_OBJECT) // [r"has no member named 'AddRef'"]
96
97// Const Method bound to non-refcounted object.
98//
99// We require refcounts unless you have Unretained().
100void WontCompile() {
101 NoRef no_ref;
102 Callback<void(void)> no_ref_const_cb =
103 Bind(&NoRef::VoidConstMethod0, &no_ref);
104 no_ref_const_cb.Run();
105}
106
107#elif defined(NCTEST_CONST_POINTER) // [r"invalid conversion from 'const base::NoRef\*' to 'base::NoRef\*'"]
108
109// Const argument used with non-const pointer parameter of same type.
110//
111// This is just a const-correctness check.
112void WontCompile() {
113 const NoRef* const_no_ref_ptr;
114 Callback<NoRef*(void)> pointer_same_cb =
115 Bind(&PolymorphicIdentity<NoRef*>, const_no_ref_ptr);
116 pointer_same_cb.Run();
117}
118
119#elif defined(NCTEST_CONST_POINTER_SUBTYPE) // [r"'const base::NoRefParent\*' to 'base::NoRefParent\*'"]
120
121// Const argument used with non-const pointer parameter of super type.
122//
123// This is just a const-correctness check.
124void WontCompile() {
125 const NoRefChild* const_child_ptr;
126 Callback<NoRefParent*(void)> pointer_super_cb =
127 Bind(&PolymorphicIdentity<NoRefParent*>, const_child_ptr);
128 pointer_super_cb.Run();
129}
130
131#elif defined(DISABLED_NCTEST_DISALLOW_NON_CONST_REF_PARAM) // [r"badstring"]
132// I think there's a type safety promotion issue here where we can pass a const
133// ref to a non const-ref function, or vice versa accidentally. Or we make a
134// copy accidentally. Check.
135
136// Functions with reference parameters, unsupported.
137//
138// First, non-const reference parameters are disallowed by the Google
139// style guide. Second, since we are doing argument forwarding it becomes
140// very tricky to avoid copies, maintain const correctness, and not
141// accidentally have the function be modifying a temporary, or a copy.
142void WontCompile() {
143 Parent p;
144 Callback<int(Parent&)> ref_arg_cb = Bind(&UnwrapParentRef);
145 ref_arg_cb.Run(p);
146}
147
148#elif defined(NCTEST_DISALLOW_BIND_TO_NON_CONST_REF_PARAM) // [r"creating array with negative size"]
149
150// Binding functions with reference parameters, unsupported.
151//
152// See comment in NCTEST_DISALLOW_NON_CONST_REF_PARAM
153void WontCompile() {
154 Parent p;
155 Callback<int(void)> ref_cb = Bind(&UnwrapParentRef, p);
156 ref_cb.Run();
157}
158
159#elif defined(NCTEST_NO_IMPLICIT_ARRAY_PTR_CONVERSION) // [r"creating array with negative size"]
160
161// A method should not be bindable with an array of objects.
162//
163// This is likely not wanted behavior. We specifically check for it though
164// because it is possible, depending on how you implement prebinding, to
165// implicitly convert an array type to a pointer type.
166void WontCompile() {
167 HasRef p[10];
168 Callback<void(void)> method_bound_to_array_cb =
169 Bind(&HasRef::VoidMethod0, p);
170 method_bound_to_array_cb.Run();
171}
172
173#elif defined(NCTEST_NO_RAW_PTR_FOR_REFCOUNTED_TYPES) // [r"creating array with negative size"]
174
175// Refcounted types should not be bound as a raw pointer.
176void WontCompile() {
177 HasRef for_raw_ptr;
178 Callback<void(void)> ref_count_as_raw_ptr =
179 Bind(&VoidPolymorphic1<HasRef*>, &for_raw_ptr);
180}
181
182#elif defined(NCTEST_WEAKPTR_BIND_MUST_RETURN_VOID) // [r"creating array with negative size"]
183
184// WeakPtrs cannot be bound to methods with return types.
185void WontCompile() {
186 NoRef no_ref;
187 WeakPtrFactory<NoRef> weak_factory(&no_ref);
188 Callback<int(void)> weak_ptr_with_non_void_return_type =
189 Bind(&NoRef::IntMethod0, weak_factory.GetWeakPtr());
190 weak_ptr_with_non_void_return_type.Run();
191}
192
193#elif defined(NCTEST_DISALLOW_ASSIGN_DIFFERINT_TYPES) // [r"creating array with negative size"]
194
195// Bind result cannot be assigned to Callbacks with a mismatching type.
196void WontCompile() {
197 Closure callback_mismatches_bind_type = Bind(&VoidPolymorphic1<int>);
198}
199
200#endif
201
202} // namespace base