OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/callback.h" | 5 #include "base/callback.h" |
6 #include "base/bind.h" | 6 #include "base/bind.h" |
7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
8 | 8 |
9 namespace base { | 9 namespace base { |
10 | 10 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 } | 57 } |
58 | 58 |
59 int UnwrapParentRef(Parent& p) { | 59 int UnwrapParentRef(Parent& p) { |
60 return p.value; | 60 return p.value; |
61 } | 61 } |
62 | 62 |
63 template <typename T> | 63 template <typename T> |
64 void VoidPolymorphic1(T t) { | 64 void VoidPolymorphic1(T t) { |
65 } | 65 } |
66 | 66 |
67 #if defined(NCTEST_METHOD_ON_CONST_OBJECT) // [r"invalid conversion from 'const
base::NoRef\*' to 'base::NoRef\*'"] | 67 #if defined(NCTEST_METHOD_ON_CONST_OBJECT) // [r"fatal error: cannot initialize
a parameter of type 'base::NoRef \*' with an lvalue of type 'typename enable_if
<!IsMoveOnlyType<const HasRef \*const>::value, const HasRef \*const>::type' \(ak
a 'const base::HasRef \*const'\)"] |
68 | 68 |
69 // Method bound to const-object. | 69 // Method bound to const-object. |
70 // | 70 // |
71 // Only const methods should be allowed to work with const objects. | 71 // Only const methods should be allowed to work with const objects. |
72 void WontCompile() { | 72 void WontCompile() { |
73 HasRef has_ref; | 73 HasRef has_ref; |
74 const HasRef* const_has_ref_ptr_ = &has_ref; | 74 const HasRef* const_has_ref_ptr_ = &has_ref; |
75 Callback<void(void)> method_to_const_cb = | 75 Callback<void(void)> method_to_const_cb = |
76 Bind(&HasRef::VoidMethod0, const_has_ref_ptr_); | 76 Bind(&HasRef::VoidMethod0, const_has_ref_ptr_); |
77 method_to_const_cb.Run(); | 77 method_to_const_cb.Run(); |
78 } | 78 } |
79 | 79 |
80 #elif defined(NCTEST_METHOD_BIND_NEEDS_REFCOUNTED_OBJECT) // [r"has no member n
amed 'AddRef'"] | 80 #elif defined(NCTEST_METHOD_BIND_NEEDS_REFCOUNTED_OBJECT) // [r"fatal error: no
member named 'AddRef' in 'base::NoRef'"] |
81 | 81 |
82 // Method bound to non-refcounted object. | 82 // Method bound to non-refcounted object. |
83 // | 83 // |
84 // We require refcounts unless you have Unretained(). | 84 // We require refcounts unless you have Unretained(). |
85 void WontCompile() { | 85 void WontCompile() { |
86 NoRef no_ref; | 86 NoRef no_ref; |
87 Callback<void(void)> no_ref_cb = | 87 Callback<void(void)> no_ref_cb = |
88 Bind(&NoRef::VoidMethod0, &no_ref); | 88 Bind(&NoRef::VoidMethod0, &no_ref); |
89 no_ref_cb.Run(); | 89 no_ref_cb.Run(); |
90 } | 90 } |
91 | 91 |
92 #elif defined(NCTEST_CONST_METHOD_NEEDS_REFCOUNTED_OBJECT) // [r"has no member
named 'AddRef'"] | 92 #elif defined(NCTEST_CONST_METHOD_NEEDS_REFCOUNTED_OBJECT) // [r"fatal error: n
o member named 'AddRef' in 'base::NoRef'"] |
93 | 93 |
94 // Const Method bound to non-refcounted object. | 94 // Const Method bound to non-refcounted object. |
95 // | 95 // |
96 // We require refcounts unless you have Unretained(). | 96 // We require refcounts unless you have Unretained(). |
97 void WontCompile() { | 97 void WontCompile() { |
98 NoRef no_ref; | 98 NoRef no_ref; |
99 Callback<void(void)> no_ref_const_cb = | 99 Callback<void(void)> no_ref_const_cb = |
100 Bind(&NoRef::VoidConstMethod0, &no_ref); | 100 Bind(&NoRef::VoidConstMethod0, &no_ref); |
101 no_ref_const_cb.Run(); | 101 no_ref_const_cb.Run(); |
102 } | 102 } |
103 | 103 |
104 #elif defined(NCTEST_CONST_POINTER) // [r"invalid conversion from 'const base::
NoRef\*' to 'base::NoRef\*'"] | 104 #elif defined(NCTEST_CONST_POINTER) // [r"fatal error: reference to type 'base:
:NoRef \*const' could not bind to an lvalue of type 'typename enable_if<!IsMoveO
nlyType<const NoRef \*const>::value, const NoRef \*const>::type' \(aka 'const ba
se::NoRef \*const'\)"] |
105 | 105 |
106 // Const argument used with non-const pointer parameter of same type. | 106 // Const argument used with non-const pointer parameter of same type. |
107 // | 107 // |
108 // This is just a const-correctness check. | 108 // This is just a const-correctness check. |
109 void WontCompile() { | 109 void WontCompile() { |
110 const NoRef* const_no_ref_ptr; | 110 const NoRef* const_no_ref_ptr; |
111 Callback<NoRef*(void)> pointer_same_cb = | 111 Callback<NoRef*(void)> pointer_same_cb = |
112 Bind(&PolymorphicIdentity<NoRef*>, const_no_ref_ptr); | 112 Bind(&PolymorphicIdentity<NoRef*>, const_no_ref_ptr); |
113 pointer_same_cb.Run(); | 113 pointer_same_cb.Run(); |
114 } | 114 } |
115 | 115 |
116 #elif defined(NCTEST_CONST_POINTER_SUBTYPE) // [r"'const base::NoRefParent\*' t
o 'base::NoRefParent\*'"] | 116 #elif defined(NCTEST_CONST_POINTER_SUBTYPE) // [r"fatal error: reference to typ
e 'base::NoRefParent \*const' could not bind to an lvalue of type 'typename enab
le_if<!IsMoveOnlyType<const NoRefChild \*const>::value, const NoRefChild \*const
>::type' \(aka 'const base::NoRefChild \*const'\)"] |
117 | 117 |
118 // Const argument used with non-const pointer parameter of super type. | 118 // Const argument used with non-const pointer parameter of super type. |
119 // | 119 // |
120 // This is just a const-correctness check. | 120 // This is just a const-correctness check. |
121 void WontCompile() { | 121 void WontCompile() { |
122 const NoRefChild* const_child_ptr; | 122 const NoRefChild* const_child_ptr; |
123 Callback<NoRefParent*(void)> pointer_super_cb = | 123 Callback<NoRefParent*(void)> pointer_super_cb = |
124 Bind(&PolymorphicIdentity<NoRefParent*>, const_child_ptr); | 124 Bind(&PolymorphicIdentity<NoRefParent*>, const_child_ptr); |
125 pointer_super_cb.Run(); | 125 pointer_super_cb.Run(); |
126 } | 126 } |
127 | 127 |
128 #elif defined(DISABLED_NCTEST_DISALLOW_NON_CONST_REF_PARAM) // [r"badstring"] | 128 #elif defined(DISABLED_NCTEST_DISALLOW_NON_CONST_REF_PARAM) // [r"fatal error:
no member named 'AddRef' in 'base::NoRef'"] |
129 // I think there's a type safety promotion issue here where we can pass a const | 129 // TODO(dcheng): I think there's a type safety promotion issue here where we can |
130 // ref to a non const-ref function, or vice versa accidentally. Or we make a | 130 // pass a const ref to a non const-ref function, or vice versa accidentally. Or |
131 // copy accidentally. Check. | 131 // we make a copy accidentally. Check. |
132 | 132 |
133 // Functions with reference parameters, unsupported. | 133 // Functions with reference parameters, unsupported. |
134 // | 134 // |
135 // First, non-const reference parameters are disallowed by the Google | 135 // First, non-const reference parameters are disallowed by the Google |
136 // style guide. Second, since we are doing argument forwarding it becomes | 136 // style guide. Second, since we are doing argument forwarding it becomes |
137 // very tricky to avoid copies, maintain const correctness, and not | 137 // very tricky to avoid copies, maintain const correctness, and not |
138 // accidentally have the function be modifying a temporary, or a copy. | 138 // accidentally have the function be modifying a temporary, or a copy. |
139 void WontCompile() { | 139 void WontCompile() { |
140 Parent p; | 140 Parent p; |
141 Callback<int(Parent&)> ref_arg_cb = Bind(&UnwrapParentRef); | 141 Callback<int(Parent&)> ref_arg_cb = Bind(&UnwrapParentRef); |
142 ref_arg_cb.Run(p); | 142 ref_arg_cb.Run(p); |
143 } | 143 } |
144 | 144 |
145 #elif defined(NCTEST_DISALLOW_BIND_TO_NON_CONST_REF_PARAM) // [r"size of array
is negative"] | 145 #elif defined(NCTEST_DISALLOW_BIND_TO_NON_CONST_REF_PARAM) // [r"fatal error: s
tatic_assert failed \"do_not_bind_functions_with_nonconst_ref\""] |
146 | 146 |
147 // Binding functions with reference parameters, unsupported. | 147 // Binding functions with reference parameters, unsupported. |
148 // | 148 // |
149 // See comment in NCTEST_DISALLOW_NON_CONST_REF_PARAM | 149 // See comment in NCTEST_DISALLOW_NON_CONST_REF_PARAM |
150 void WontCompile() { | 150 void WontCompile() { |
151 Parent p; | 151 Parent p; |
152 Callback<int(void)> ref_cb = Bind(&UnwrapParentRef, p); | 152 Callback<int(void)> ref_cb = Bind(&UnwrapParentRef, p); |
153 ref_cb.Run(); | 153 ref_cb.Run(); |
154 } | 154 } |
155 | 155 |
156 #elif defined(NCTEST_NO_IMPLICIT_ARRAY_PTR_CONVERSION) // [r"size of array is n
egative"] | 156 #elif defined(NCTEST_NO_IMPLICIT_ARRAY_PTR_CONVERSION) // [r"fatal error: stati
c_assert failed \"first_bound_argument_to_method_cannot_be_array\""] |
157 | 157 |
158 // A method should not be bindable with an array of objects. | 158 // A method should not be bindable with an array of objects. |
159 // | 159 // |
160 // This is likely not wanted behavior. We specifically check for it though | 160 // This is likely not wanted behavior. We specifically check for it though |
161 // because it is possible, depending on how you implement prebinding, to | 161 // because it is possible, depending on how you implement prebinding, to |
162 // implicitly convert an array type to a pointer type. | 162 // implicitly convert an array type to a pointer type. |
163 void WontCompile() { | 163 void WontCompile() { |
164 HasRef p[10]; | 164 HasRef p[10]; |
165 Callback<void(void)> method_bound_to_array_cb = | 165 Callback<void(void)> method_bound_to_array_cb = |
166 Bind(&HasRef::VoidMethod0, p); | 166 Bind(&HasRef::VoidMethod0, p); |
167 method_bound_to_array_cb.Run(); | 167 method_bound_to_array_cb.Run(); |
168 } | 168 } |
169 | 169 |
170 #elif defined(NCTEST_NO_RAW_PTR_FOR_REFCOUNTED_TYPES) // [r"size of array is ne
gative"] | 170 #elif defined(NCTEST_NO_RAW_PTR_FOR_REFCOUNTED_TYPES) // [r"fatal error: static
_assert failed \"p1_is_refcounted_type_and_needs_scoped_refptr\""] |
171 | 171 |
172 // Refcounted types should not be bound as a raw pointer. | 172 // Refcounted types should not be bound as a raw pointer. |
173 void WontCompile() { | 173 void WontCompile() { |
174 HasRef for_raw_ptr; | 174 HasRef for_raw_ptr; |
175 int a; | 175 int a; |
176 Callback<void(void)> ref_count_as_raw_ptr_a = | 176 Callback<void(void)> ref_count_as_raw_ptr_a = |
177 Bind(&VoidPolymorphic1<int*>, &a); | 177 Bind(&VoidPolymorphic1<int*>, &a); |
178 Callback<void(void)> ref_count_as_raw_ptr = | 178 Callback<void(void)> ref_count_as_raw_ptr = |
179 Bind(&VoidPolymorphic1<HasRef*>, &for_raw_ptr); | 179 Bind(&VoidPolymorphic1<HasRef*>, &for_raw_ptr); |
180 } | 180 } |
181 | 181 |
182 #elif defined(NCTEST_WEAKPTR_BIND_MUST_RETURN_VOID) // [r"size of array is nega
tive"] | 182 #elif defined(NCTEST_WEAKPTR_BIND_MUST_RETURN_VOID) // [r"fatal error: static_a
ssert failed \"weak_ptrs_can_only_bind_to_methods_without_return_values\""] |
183 | 183 |
184 // WeakPtrs cannot be bound to methods with return types. | 184 // WeakPtrs cannot be bound to methods with return types. |
185 void WontCompile() { | 185 void WontCompile() { |
186 NoRef no_ref; | 186 NoRef no_ref; |
187 WeakPtrFactory<NoRef> weak_factory(&no_ref); | 187 WeakPtrFactory<NoRef> weak_factory(&no_ref); |
188 Callback<int(void)> weak_ptr_with_non_void_return_type = | 188 Callback<int(void)> weak_ptr_with_non_void_return_type = |
189 Bind(&NoRef::IntMethod0, weak_factory.GetWeakPtr()); | 189 Bind(&NoRef::IntMethod0, weak_factory.GetWeakPtr()); |
190 weak_ptr_with_non_void_return_type.Run(); | 190 weak_ptr_with_non_void_return_type.Run(); |
191 } | 191 } |
192 | 192 |
193 #elif defined(NCTEST_DISALLOW_ASSIGN_DIFFERENT_TYPES) // [r"conversion from 'ba
se::Callback<void\(int\)>' to non-scalar type"] | 193 #elif defined(NCTEST_DISALLOW_ASSIGN_DIFFERENT_TYPES) // [r"fatal error: no via
ble conversion from 'Callback<typename internal::BindState<typename internal::Fu
nctorTraits<void \(\*\)\(int\)>::RunnableType, typename internal::FunctorTraits<
void \(\*\)\(int\)>::RunType, void \(\)>::UnboundRunType>' to 'Callback<void \(\
)>'"] |
194 | 194 |
195 // Bind result cannot be assigned to Callbacks with a mismatching type. | 195 // Bind result cannot be assigned to Callbacks with a mismatching type. |
196 void WontCompile() { | 196 void WontCompile() { |
197 Closure callback_mismatches_bind_type = Bind(&VoidPolymorphic1<int>); | 197 Closure callback_mismatches_bind_type = Bind(&VoidPolymorphic1<int>); |
198 } | 198 } |
199 | 199 |
200 #endif | 200 #endif |
201 | 201 |
202 } // namespace base | 202 } // namespace base |
OLD | NEW |