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 | 8 |
8 namespace base { | 9 namespace base { |
9 | 10 |
10 // Do not put everything inside an anonymous namespace. If you do, many of the | 11 // 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 // helper function declarations will generate unused definition warnings unless |
12 // unused definition warnings. | 13 // unused definition warnings. |
13 | 14 |
14 static const int kParentValue = 1; | 15 static const int kParentValue = 1; |
15 static const int kChildValue = 2; | 16 static const int kChildValue = 2; |
16 | 17 |
17 class NoRef { | 18 class NoRef { |
18 public: | 19 public: |
19 void VoidMethod0() {} | 20 void VoidMethod0() {} |
20 void VoidConstMethod0() const {} | 21 void VoidConstMethod0() const {} |
21 int IntMethod0() { return 1; } | 22 int IntMethod0() { return 1; } |
22 }; | 23 }; |
23 | 24 |
24 class HasRef : public NoRef { | 25 class HasRef : public NoRef, public base::RefCounted<HasRef> { |
25 public: | |
26 void AddRef(void) const {} | |
27 bool Release(void) const { return true; } | |
28 }; | 26 }; |
29 | 27 |
30 class Parent { | 28 class Parent { |
31 public: | 29 public: |
32 void AddRef(void) const {} | 30 void AddRef(void) const {} |
33 void Release(void) const {} | 31 void Release(void) const {} |
34 virtual void VirtualSet() { value = kParentValue; } | 32 virtual void VirtualSet() { value = kParentValue; } |
35 void NonVirtualSet() { value = kParentValue; } | 33 void NonVirtualSet() { value = kParentValue; } |
36 int value; | 34 int value; |
37 }; | 35 }; |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 Callback<void(void)> method_bound_to_array_cb = | 166 Callback<void(void)> method_bound_to_array_cb = |
169 Bind(&HasRef::VoidMethod0, p); | 167 Bind(&HasRef::VoidMethod0, p); |
170 method_bound_to_array_cb.Run(); | 168 method_bound_to_array_cb.Run(); |
171 } | 169 } |
172 | 170 |
173 #elif defined(NCTEST_NO_RAW_PTR_FOR_REFCOUNTED_TYPES) // [r"creating array with
negative size"] | 171 #elif defined(NCTEST_NO_RAW_PTR_FOR_REFCOUNTED_TYPES) // [r"creating array with
negative size"] |
174 | 172 |
175 // Refcounted types should not be bound as a raw pointer. | 173 // Refcounted types should not be bound as a raw pointer. |
176 void WontCompile() { | 174 void WontCompile() { |
177 HasRef for_raw_ptr; | 175 HasRef for_raw_ptr; |
| 176 int a; |
| 177 Callback<void(void)> ref_count_as_raw_ptr_a = |
| 178 Bind(&VoidPolymorphic1<int*>, &a); |
178 Callback<void(void)> ref_count_as_raw_ptr = | 179 Callback<void(void)> ref_count_as_raw_ptr = |
179 Bind(&VoidPolymorphic1<HasRef*>, &for_raw_ptr); | 180 Bind(&VoidPolymorphic1<HasRef*>, &for_raw_ptr); |
180 } | 181 } |
181 | 182 |
182 #elif defined(NCTEST_WEAKPTR_BIND_MUST_RETURN_VOID) // [r"creating array with n
egative size"] | 183 #elif defined(NCTEST_WEAKPTR_BIND_MUST_RETURN_VOID) // [r"creating array with n
egative size"] |
183 | 184 |
184 // WeakPtrs cannot be bound to methods with return types. | 185 // WeakPtrs cannot be bound to methods with return types. |
185 void WontCompile() { | 186 void WontCompile() { |
186 NoRef no_ref; | 187 NoRef no_ref; |
187 WeakPtrFactory<NoRef> weak_factory(&no_ref); | 188 WeakPtrFactory<NoRef> weak_factory(&no_ref); |
188 Callback<int(void)> weak_ptr_with_non_void_return_type = | 189 Callback<int(void)> weak_ptr_with_non_void_return_type = |
189 Bind(&NoRef::IntMethod0, weak_factory.GetWeakPtr()); | 190 Bind(&NoRef::IntMethod0, weak_factory.GetWeakPtr()); |
190 weak_ptr_with_non_void_return_type.Run(); | 191 weak_ptr_with_non_void_return_type.Run(); |
191 } | 192 } |
192 | 193 |
193 #elif defined(NCTEST_DISALLOW_ASSIGN_DIFFERINT_TYPES) // [r"creating array with
negative size"] | 194 #elif defined(NCTEST_DISALLOW_ASSIGN_DIFFERINT_TYPES) // [r"invalid conversion
from"] |
194 | 195 |
195 // Bind result cannot be assigned to Callbacks with a mismatching type. | 196 // Bind result cannot be assigned to Callbacks with a mismatching type. |
196 void WontCompile() { | 197 void WontCompile() { |
197 Closure callback_mismatches_bind_type = Bind(&VoidPolymorphic1<int>); | 198 Closure callback_mismatches_bind_type = Bind(&VoidPolymorphic1<int>); |
198 } | 199 } |
199 | 200 |
200 #endif | 201 #endif |
201 | 202 |
202 } // namespace base | 203 } // namespace base |
OLD | NEW |