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.
5 #include "base/callback.h"
7 #include "base/memory/ref_counted.h"
11 // Do not put everything inside an anonymous namespace. If you do, many of the
12 // helper function declarations will generate unused definition warnings.
14 static const int kParentValue = 1;
15 static const int kChildValue = 2;
20 void VoidConstMethod0() const {}
21 int IntMethod0() { return 1; }
24 class HasRef : public NoRef, public base::RefCounted<HasRef> {
29 void AddRef(void) const {}
30 void Release(void) const {}
31 virtual void VirtualSet() { value = kParentValue; }
32 void NonVirtualSet() { value = kParentValue; }
36 class Child : public Parent {
38 virtual void VirtualSet() { value = kChildValue; }
39 void NonVirtualSet() { value = kChildValue; }
44 virtual void VirtualSet() { value = kParentValue; }
45 void NonVirtualSet() { value = kParentValue; }
49 class NoRefChild : public NoRefParent {
50 virtual void VirtualSet() { value = kChildValue; }
51 void NonVirtualSet() { value = kChildValue; }
55 T PolymorphicIdentity(T t) {
59 int UnwrapParentRef(Parent& p) {
64 void VoidPolymorphic1(T t) {
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' \(aka 'const base::HasRef \*const'\)"]
69 // Method bound to const-object.
71 // Only const methods should be allowed to work with const objects.
74 const HasRef* const_has_ref_ptr_ = &has_ref;
75 Callback<void(void)> method_to_const_cb =
76 Bind(&HasRef::VoidMethod0, const_has_ref_ptr_);
77 method_to_const_cb.Run();
80 #elif defined(NCTEST_METHOD_BIND_NEEDS_REFCOUNTED_OBJECT) // [r"fatal error: no member named 'AddRef' in 'base::NoRef'"]
82 // Method bound to non-refcounted object.
84 // We require refcounts unless you have Unretained().
87 Callback<void(void)> no_ref_cb =
88 Bind(&NoRef::VoidMethod0, &no_ref);
92 #elif defined(NCTEST_CONST_METHOD_NEEDS_REFCOUNTED_OBJECT) // [r"fatal error: no member named 'AddRef' in 'base::NoRef'"]
94 // Const Method bound to non-refcounted object.
96 // We require refcounts unless you have Unretained().
99 Callback<void(void)> no_ref_const_cb =
100 Bind(&NoRef::VoidConstMethod0, &no_ref);
101 no_ref_const_cb.Run();
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<!IsMoveOnlyType<const NoRef \*const>::value, const NoRef \*const>::type' \(aka 'const base::NoRef \*const'\)"]
106 // Const argument used with non-const pointer parameter of same type.
108 // This is just a const-correctness check.
110 const NoRef* const_no_ref_ptr;
111 Callback<NoRef*(void)> pointer_same_cb =
112 Bind(&PolymorphicIdentity<NoRef*>, const_no_ref_ptr);
113 pointer_same_cb.Run();
116 #elif defined(NCTEST_CONST_POINTER_SUBTYPE) // [r"fatal error: reference to type 'base::NoRefParent \*const' could not bind to an lvalue of type 'typename enable_if<!IsMoveOnlyType<const NoRefChild \*const>::value, const NoRefChild \*const>::type' \(aka 'const base::NoRefChild \*const'\)"]
118 // Const argument used with non-const pointer parameter of super type.
120 // This is just a const-correctness check.
122 const NoRefChild* const_child_ptr;
123 Callback<NoRefParent*(void)> pointer_super_cb =
124 Bind(&PolymorphicIdentity<NoRefParent*>, const_child_ptr);
125 pointer_super_cb.Run();
128 #elif defined(DISABLED_NCTEST_DISALLOW_NON_CONST_REF_PARAM) // [r"fatal error: no member named 'AddRef' in 'base::NoRef'"]
129 // TODO(dcheng): I think there's a type safety promotion issue here where we can
130 // pass a const ref to a non const-ref function, or vice versa accidentally. Or
131 // we make a copy accidentally. Check.
133 // Functions with reference parameters, unsupported.
135 // First, non-const reference parameters are disallowed by the Google
136 // style guide. Second, since we are doing argument forwarding it becomes
137 // very tricky to avoid copies, maintain const correctness, and not
138 // accidentally have the function be modifying a temporary, or a copy.
141 Callback<int(Parent&)> ref_arg_cb = Bind(&UnwrapParentRef);
145 #elif defined(NCTEST_DISALLOW_BIND_TO_NON_CONST_REF_PARAM) // [r"fatal error: static_assert failed \"do_not_bind_functions_with_nonconst_ref\""]
147 // Binding functions with reference parameters, unsupported.
149 // See comment in NCTEST_DISALLOW_NON_CONST_REF_PARAM
152 Callback<int(void)> ref_cb = Bind(&UnwrapParentRef, p);
156 #elif defined(NCTEST_NO_IMPLICIT_ARRAY_PTR_CONVERSION) // [r"fatal error: static_assert failed \"first_bound_argument_to_method_cannot_be_array\""]
158 // A method should not be bindable with an array of objects.
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
162 // implicitly convert an array type to a pointer type.
165 Callback<void(void)> method_bound_to_array_cb =
166 Bind(&HasRef::VoidMethod0, p);
167 method_bound_to_array_cb.Run();
170 #elif defined(NCTEST_NO_RAW_PTR_FOR_REFCOUNTED_TYPES) // [r"fatal error: static_assert failed \"a_parameter_is_refcounted_type_and_needs_scoped_refptr\""]
172 // Refcounted types should not be bound as a raw pointer.
176 Callback<void(void)> ref_count_as_raw_ptr_a =
177 Bind(&VoidPolymorphic1<int*>, &a);
178 Callback<void(void)> ref_count_as_raw_ptr =
179 Bind(&VoidPolymorphic1<HasRef*>, &for_raw_ptr);
182 #elif defined(NCTEST_WEAKPTR_BIND_MUST_RETURN_VOID) // [r"fatal error: static_assert failed \"weak_ptrs_can_only_bind_to_methods_without_return_values\""]
184 // WeakPtrs cannot be bound to methods with return types.
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();
193 #elif defined(NCTEST_DISALLOW_ASSIGN_DIFFERENT_TYPES) // [r"fatal error: no viable conversion from 'Callback<typename internal::BindState<typename internal::FunctorTraits<void \(\*\)\(int\)>::RunnableType, typename internal::FunctorTraits<void \(\*\)\(int\)>::RunType, internal::TypeList<> >::UnboundRunType>' to 'Callback<void \(\)>'"]
195 // Bind result cannot be assigned to Callbacks with a mismatching type.
197 Closure callback_mismatches_bind_type = Bind(&VoidPolymorphic1<int>);