1 // Copyright (c) 2012 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/memory/weak_ptr.h"
9 struct Producer : SupportsWeakPtr<Producer> {};
10 struct DerivedProducer : Producer {};
11 struct OtherDerivedProducer : Producer {};
12 struct MultiplyDerivedProducer : Producer,
13 SupportsWeakPtr<MultiplyDerivedProducer> {};
15 struct DerivedUnrelated : Unrelated {};
17 #if defined(NCTEST_AUTO_DOWNCAST) // [r"fatal error: cannot initialize a member subobject of type 'base::DerivedProducer \*' with an lvalue of type 'base::Producer \*const'"]
21 WeakPtr<Producer> ptr = f.AsWeakPtr();
22 WeakPtr<DerivedProducer> derived_ptr = ptr;
25 #elif defined(NCTEST_STATIC_DOWNCAST) // [r"fatal error: cannot initialize a member subobject of type 'base::DerivedProducer \*' with an lvalue of type 'base::Producer \*const'"]
29 WeakPtr<Producer> ptr = f.AsWeakPtr();
30 WeakPtr<DerivedProducer> derived_ptr =
31 static_cast<WeakPtr<DerivedProducer> >(ptr);
34 #elif defined(NCTEST_AUTO_REF_DOWNCAST) // [r"fatal error: non-const lvalue reference to type 'WeakPtr<base::DerivedProducer>' cannot bind to a value of unrelated type 'WeakPtr<base::Producer>'"]
38 WeakPtr<Producer> ptr = f.AsWeakPtr();
39 WeakPtr<DerivedProducer>& derived_ptr = ptr;
42 #elif defined(NCTEST_STATIC_REF_DOWNCAST) // [r"fatal error: non-const lvalue reference to type 'WeakPtr<base::DerivedProducer>' cannot bind to a value of unrelated type 'WeakPtr<base::Producer>'"]
46 WeakPtr<Producer> ptr = f.AsWeakPtr();
47 WeakPtr<DerivedProducer>& derived_ptr =
48 static_cast<WeakPtr<DerivedProducer>&>(ptr);
51 #elif defined(NCTEST_STATIC_ASWEAKPTR_DOWNCAST) // [r"no matching function"]
55 WeakPtr<DerivedProducer> ptr =
56 SupportsWeakPtr<Producer>::StaticAsWeakPtr<DerivedProducer>(&f);
59 #elif defined(NCTEST_UNSAFE_HELPER_DOWNCAST) // [r"fatal error: cannot initialize a member subobject of type 'base::DerivedProducer \*' with an lvalue of type 'base::Producer \*const'"]
63 WeakPtr<DerivedProducer> ptr = AsWeakPtr(&f);
66 #elif defined(NCTEST_UNSAFE_INSTANTIATED_HELPER_DOWNCAST) // [r"no matching function"]
70 WeakPtr<DerivedProducer> ptr = AsWeakPtr<DerivedProducer>(&f);
73 #elif defined(NCTEST_UNSAFE_WRONG_INSANTIATED_HELPER_DOWNCAST) // [r"fatal error: cannot initialize a member subobject of type 'base::DerivedProducer \*' with an lvalue of type 'base::Producer \*const'"]
77 WeakPtr<DerivedProducer> ptr = AsWeakPtr<Producer>(&f);
80 #elif defined(NCTEST_UNSAFE_HELPER_CAST) // [r"fatal error: cannot initialize a member subobject of type 'base::OtherDerivedProducer \*' with an lvalue of type 'base::DerivedProducer \*const'"]
84 WeakPtr<OtherDerivedProducer> ptr = AsWeakPtr(&f);
87 #elif defined(NCTEST_UNSAFE_INSTANTIATED_HELPER_SIDECAST) // [r"fatal error: no matching function for call to 'AsWeakPtr'"]
91 WeakPtr<OtherDerivedProducer> ptr = AsWeakPtr<OtherDerivedProducer>(&f);
94 #elif defined(NCTEST_UNSAFE_WRONG_INSTANTIATED_HELPER_SIDECAST) // [r"fatal error: cannot initialize a member subobject of type 'base::OtherDerivedProducer \*' with an lvalue of type 'base::DerivedProducer \*const'"]
98 WeakPtr<OtherDerivedProducer> ptr = AsWeakPtr<DerivedProducer>(&f);
101 #elif defined(NCTEST_UNRELATED_HELPER) // [r"fatal error: cannot initialize a member subobject of type 'base::Unrelated \*' with an lvalue of type 'base::DerivedProducer \*const'"]
105 WeakPtr<Unrelated> ptr = AsWeakPtr(&f);
108 #elif defined(NCTEST_UNRELATED_INSTANTIATED_HELPER) // [r"no matching function"]
112 WeakPtr<Unrelated> ptr = AsWeakPtr<Unrelated>(&f);
115 #elif defined(NCTEST_COMPLETELY_UNRELATED_HELPER) // [r"fatal error: static_assert failed \"AsWeakPtr_argument_inherits_from_SupportsWeakPtr\""]
119 WeakPtr<Unrelated> ptr = AsWeakPtr(&f);
122 #elif defined(NCTEST_DERIVED_COMPLETELY_UNRELATED_HELPER) // [r"fatal error: static_assert failed \"AsWeakPtr_argument_inherits_from_SupportsWeakPtr\""]
126 WeakPtr<Unrelated> ptr = AsWeakPtr(&f);
129 #elif defined(NCTEST_AMBIGUOUS_ANCESTORS) // [r"fatal error: ambiguous conversion from derived class 'base::MultiplyDerivedProducer' to base class 'base::internal::SupportsWeakPtrBase':"]
132 MultiplyDerivedProducer f;
133 WeakPtr<MultiplyDerivedProducer> ptr = AsWeakPtr(&f);