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/callback.h" | |
6 #include "base/bind.h" | |
7 | |
8 namespace base { | |
9 | |
10 // Keep all the test declarations outside of an anonymous namespace to avoid | |
Ami GONE FROM CHROMIUM
2011/09/09 17:17:12
Umm, what?
awong
2011/09/09 18:56:31
Clarified.
| |
11 // unused definition warnings. | |
12 const int kParentValue = 1; | |
Ami GONE FROM CHROMIUM
2011/09/09 17:17:12
static is implied so I enjoy putting it in explici
awong
2011/09/09 18:56:31
Done.
| |
13 const int kChildValue = 2; | |
14 | |
15 class NoRef { | |
16 public: | |
17 void VoidMethod0() {} | |
18 void VoidConstMethod0() const {} | |
19 int IntMethod0() { return 1; } | |
20 }; | |
21 | |
22 class HasRef : public NoRef { | |
23 public: | |
24 void AddRef(void) const {} | |
25 bool Release(void) const { return true; } | |
26 }; | |
27 | |
28 class Parent { | |
29 public: | |
30 void AddRef(void) const {} | |
31 void Release(void) const {} | |
32 virtual void VirtualSet() { value = kParentValue; } | |
33 void NonVirtualSet() { value = kParentValue; } | |
34 int value; | |
35 }; | |
36 | |
37 class Child : public Parent { | |
38 public: | |
39 virtual void VirtualSet() { value = kChildValue; } | |
40 void NonVirtualSet() { value = kChildValue; } | |
41 }; | |
42 | |
43 class NoRefParent { | |
44 public: | |
45 virtual void VirtualSet() { value = kParentValue; } | |
46 void NonVirtualSet() { value = kParentValue; } | |
47 int value; | |
48 }; | |
49 | |
50 class NoRefChild : public NoRefParent { | |
51 virtual void VirtualSet() { value = kChildValue; } | |
52 void NonVirtualSet() { value = kChildValue; } | |
53 }; | |
54 | |
55 template <typename T> | |
56 T PolymorphicIdentity(T t) { | |
57 return t; | |
58 } | |
59 | |
60 int UnwrapParentRef(Parent& p) { | |
61 return p.value; | |
62 } | |
63 | |
64 template <typename T> | |
65 void VoidPolymorphic1(T t) { | |
66 } | |
67 | |
68 #if defined(NCTEST_METHOD_ON_CONST_OBJECT) // [r"invalid conversion from 'const base::NoRef\*' to 'base::NoRef\*'"] | |
69 | |
70 // Method bound to const-object. | |
71 // | |
72 // Only const methods should be allowed to work with const objects. | |
73 void WontCompile() { | |
74 HasRef has_ref; | |
75 const HasRef* const_has_ref_ptr_ = &has_ref; | |
76 Callback<void(void)> method_to_const_cb = | |
77 Bind(&HasRef::VoidMethod0, const_has_ref_ptr_); | |
78 method_to_const_cb.Run(); | |
79 } | |
80 | |
81 #elif defined(NCTEST_CONSTRUCTION_FROM_SUBTYPE) // [r"conversion from 'base::Ca llback<int\(\)>' to non-scalar type 'base::Callback<void\(\)>'"] | |
82 | |
83 // Construction of Callback<A> from Callback<B> if A is supertype of B. | |
84 // | |
85 // While this is technically safe, most people aren't used to it when coding | |
86 // C++ so if this is happening, it is almost certainly an error. | |
87 void WontCompile() { | |
88 Callback<int(void)> cb_a = Bind(&PolymorphicIdentity<int>, 1); | |
89 Callback<void(void)> cb_b = cb_a; | |
90 } | |
91 | |
92 #elif defined(NCTEST_ASSIGNMENT_FROM_SUBTYPE) // [r"no match for 'operator=' in 'cb_a = cb_b'"] | |
93 | |
94 // Assignment of Callback<A> from Callback<B> if A is supertype of B. | |
95 // See explanation for NCTEST_CONSTRUCTION_FROM_SUBTYPE | |
96 void WontCompile() { | |
97 Callback<int(void)> cb_a = Bind(&PolymorphicIdentity<int>, 1); | |
98 Callback<void(void)> cb_b; | |
99 cb_a = cb_b; | |
100 } | |
101 | |
102 #elif defined(NCTEST_METHOD_BIND_NEEDS_REFCOUNTED_OBJECT) // [r"has no member n amed 'AddRef'"] | |
103 | |
104 // Method bound to non-refcounted object. | |
105 // | |
106 // We require refcounts unless you have Unretained(). | |
107 void WontCompile() { | |
108 NoRef no_ref; | |
109 Callback<void(void)> no_ref_cb = | |
110 Bind(&NoRef::VoidMethod0, &no_ref); | |
111 no_ref_cb.Run(); | |
112 } | |
113 | |
114 #elif defined(NCTEST_CONST_METHOD_NEEDS_REFCOUNTED_OBJECT) // [r"has no member named 'AddRef'"] | |
115 | |
116 // Const Method bound to non-refcounted object. | |
117 // | |
118 // We require refcounts unless you have Unretained(). | |
119 void WontCompile() { | |
120 NoRef no_ref; | |
121 Callback<void(void)> no_ref_const_cb = | |
122 Bind(&NoRef::VoidConstMethod0, &no_ref); | |
123 no_ref_const_cb.Run(); | |
124 } | |
125 | |
126 #elif defined(NCTEST_CONST_POINTER) // [r"invalid conversion from 'const base:: NoRef\*' to 'base::NoRef\*'"] | |
127 | |
128 // Const argument used with non-const pointer parameter of same type. | |
129 // | |
130 // This is just a const-correctness check. | |
131 void WontCompile() { | |
132 const NoRef* const_no_ref_ptr; | |
133 Callback<NoRef*(void)> pointer_same_cb = | |
134 Bind(&PolymorphicIdentity<NoRef*>, const_no_ref_ptr); | |
135 pointer_same_cb.Run(); | |
136 } | |
137 | |
138 #elif defined(NCTEST_CONST_POINTER_SUBTYPE) // [r"'const base::NoRefParent\*' t o 'base::NoRefParent\*'"] | |
139 | |
140 // Const argument used with non-const pointer parameter of super type. | |
141 // | |
142 // This is just a const-correctness check. | |
143 void WontCompile() { | |
144 const NoRefChild* const_child_ptr; | |
145 Callback<NoRefParent*(void)> pointer_super_cb = | |
146 Bind(&PolymorphicIdentity<NoRefParent*>, const_child_ptr); | |
147 pointer_super_cb.Run(); | |
148 } | |
149 | |
150 #elif defined(DISABLED_NCTEST_DISALLOW_NON_CONST_REF_PARAM) // [r"badstring"] | |
151 | |
152 // Functions with reference parameters, unsupported. | |
153 // | |
154 // First, non-const reference parameters are disallowed by the Google | |
155 // style guide. Second, since we are doing argument forwarding it becomes | |
156 // very tricky to avoid copies, maintain const correctness, and not | |
157 // accidentally have the function be modifying a temporary, or a copy. | |
158 void WontCompile() { | |
159 Parent p; | |
160 Callback<int(Parent&)> ref_arg_cb = Bind(&UnwrapParentRef); | |
161 ref_arg_cb.Run(p); | |
162 } | |
163 | |
164 #elif defined(NCTEST_DISALLOW_BIND_TO_NON_CONST_REF_PARAM) // [r"creating array with negative size"] | |
165 | |
166 // Binding functions with reference parameters, unsupported. | |
167 // | |
168 // See comment in NCTEST_DISALLOW_NON_CONST_REF_PARAM | |
169 void WontCompile() { | |
170 Parent p; | |
171 Callback<int(void)> ref_cb = Bind(&UnwrapParentRef, p); | |
172 ref_cb.Run(); | |
173 } | |
174 | |
175 #elif defined(NCTEST_NO_IMPLICIT_ARRAY_PTR_CONVERSION) // [r"creating array wit h negative size"] | |
176 | |
177 // A method should not be bindable with an array of objects. | |
178 // | |
179 // This is likely not wanted behavior. We specifically check for it though | |
180 // because it is possible, depending on how you implement prebinding, to | |
181 // implicitly convert an array type to a pointer type. | |
182 void WontCompile() { | |
183 HasRef p[10]; | |
184 Callback<void(void)> method_bound_to_array_cb = | |
185 Bind(&HasRef::VoidMethod0, p); | |
186 method_bound_to_array_cb.Run(); | |
187 } | |
188 | |
189 #elif defined(NCTEST_NO_RAW_PTR_FOR_REFCOUNTED_TYPES) // [r"creating array with negative size"] | |
190 | |
191 // Refcounted types should not be bound as a raw pointer. | |
192 void WontCompile() { | |
193 HasRef for_raw_ptr; | |
194 Callback<void(void)> ref_count_as_raw_ptr = | |
195 Bind(&VoidPolymorphic1<HasRef*>, &for_raw_ptr); | |
196 } | |
197 | |
198 #elif defined(NCTEST_WEAKPTR_BIND_MUST_RETURN_VOID) // [r"creating array with n egative size"] | |
199 | |
200 // WeakPtrs cannot be bound to methods with return types. | |
201 void WontCompile() { | |
202 NoRef no_ref; | |
203 WeakPtrFactory<NoRef> weak_factory(&no_ref); | |
204 Callback<int(void)> weak_ptr_with_non_void_return_type = | |
205 Bind(&NoRef::IntMethod0, weak_factory.GetWeakPtr()); | |
206 weak_ptr_with_non_void_return_type.Run(); | |
207 } | |
208 | |
209 #elif defined(NCTEST_DISALLOW_ASSIGN_DIFFERINT_TYPES) // [r"creating array with negative size"] | |
210 | |
211 // Bind result cannot be assigned to Callbacks with a mismatching type. | |
212 void WontCompile() { | |
213 Closure callback_mismatches_bind_type = Bind(&VoidPolymorphic1<int>); | |
214 } | |
215 | |
216 #endif | |
217 | |
218 } // namespace base | |
OLD | NEW |