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