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 #ifndef BASE_REFCOUNTED_H_
6 #define BASE_REFCOUNTED_H_
19 class RefCountedThreadSafe
{
21 RefCountedThreadSafe() {}
23 ~RefCountedThreadSafe() {}
28 // Ignore classes whose inheritance tree ends in WebKit's RefCounted base
29 // class. Though prone to error, this pattern is very prevalent in WebKit
30 // code, so do not issue any warnings.
42 // Unsafe; should error.
43 class PublicRefCountedDtorInHeader
44 : public base::RefCounted
<PublicRefCountedDtorInHeader
> {
46 PublicRefCountedDtorInHeader() {}
47 ~PublicRefCountedDtorInHeader() {}
50 friend class base::RefCounted
<PublicRefCountedDtorInHeader
>;
53 // Unsafe; should error.
54 class PublicRefCountedThreadSafeDtorInHeader
55 : public base::RefCountedThreadSafe
<
56 PublicRefCountedThreadSafeDtorInHeader
> {
58 PublicRefCountedThreadSafeDtorInHeader() {}
59 ~PublicRefCountedThreadSafeDtorInHeader() {}
62 friend class base::RefCountedThreadSafe
<
63 PublicRefCountedThreadSafeDtorInHeader
>;
66 // Unsafe; should error.
67 class ProtectedRefCountedDtorInHeader
68 : public base::RefCounted
<ProtectedRefCountedDtorInHeader
> {
70 ProtectedRefCountedDtorInHeader() {}
73 ~ProtectedRefCountedDtorInHeader() {}
76 friend class base::RefCounted
<ProtectedRefCountedDtorInHeader
>;
79 // Safe; should not have errors
80 class ProtectedRefCountedVirtualDtorInHeader
81 : public base::RefCounted
<ProtectedRefCountedVirtualDtorInHeader
> {
83 ProtectedRefCountedVirtualDtorInHeader() {}
86 virtual ~ProtectedRefCountedVirtualDtorInHeader() {}
89 friend class base::RefCounted
<ProtectedRefCountedVirtualDtorInHeader
>;
93 // Safe; should not have errors.
94 class PrivateRefCountedDtorInHeader
95 : public base::RefCounted
<PrivateRefCountedDtorInHeader
> {
97 PrivateRefCountedDtorInHeader() {}
100 ~PrivateRefCountedDtorInHeader() {}
101 friend class base::RefCounted
<PrivateRefCountedDtorInHeader
>;
104 // Unsafe; A grandchild class ends up exposing their parent and grandparent's
106 class DerivedProtectedToPublicInHeader
107 : public ProtectedRefCountedVirtualDtorInHeader
{
109 DerivedProtectedToPublicInHeader() {}
110 ~DerivedProtectedToPublicInHeader() override
{}
113 // Unsafe; A grandchild ends up implicitly exposing their parent and
114 // grantparent's destructors.
115 class ImplicitDerivedProtectedToPublicInHeader
116 : public ProtectedRefCountedVirtualDtorInHeader
{
118 ImplicitDerivedProtectedToPublicInHeader() {}
121 // Unsafe-but-ignored; should not have errors.
122 class WebKitPublicDtorInHeader
123 : public WebKit::RefCounted
<WebKitPublicDtorInHeader
> {
125 WebKitPublicDtorInHeader() {}
126 ~WebKitPublicDtorInHeader() {}
129 // Unsafe-but-ignored; should not have errors.
130 class WebKitDerivedPublicDtorInHeader
131 : public WebKitPublicDtorInHeader
{
133 WebKitDerivedPublicDtorInHeader() {}
134 ~WebKitDerivedPublicDtorInHeader() {}
137 class APublicInterface
{
139 virtual ~APublicInterface() {}
140 virtual void DoFoo() = 0;
143 // Unsafe. "ImplementsAPublicInterface* foo" can be deleted via
144 // "delete (APublicInterface*)foo;".
145 class ImplementsAPublicInterface
146 : public APublicInterface
,
147 public base::RefCounted
<ImplementsAPublicInterface
> {
149 void DoFoo() override
{}
152 ~ImplementsAPublicInterface() override
{}
155 friend class base::RefCounted
<ImplementsAPublicInterface
>;
158 class AnImplicitInterface
{
160 virtual void DoBar() {}
164 class ImplementsAnImplicitInterface
165 : public AnImplicitInterface
,
166 public base::RefCounted
<ImplementsAnImplicitInterface
> {
168 void DoBar() override
{}
171 friend class base::RefCounted
<ImplementsAnImplicitInterface
>;
172 ~ImplementsAnImplicitInterface() {}
175 // Safe. Private inheritance does not expose the base destructor.
176 class PrivatelyImplementsAPublicInterface
177 : private APublicInterface
,
178 public base::RefCounted
<PrivatelyImplementsAPublicInterface
> {
180 void DoFoo() override
{}
183 friend class base::RefCounted
<PrivatelyImplementsAPublicInterface
>;
184 ~PrivatelyImplementsAPublicInterface() override
{}
188 class BaseInterface
{
190 virtual ~BaseInterface() {}
191 virtual void DoFoo() {}
193 class DerivedInterface
: public BaseInterface
{
195 ~DerivedInterface() override
{}
197 class SomeOtherInterface
{
199 virtual ~SomeOtherInterface() {}
200 virtual void DoBar() {}
202 class RefcountedType
: public base::RefCounted
<RefcountedType
> {
206 friend class base::RefCounted
<RefcountedType
>;
208 class UnsafeInheritanceChain
209 : public DerivedInterface
,
210 public SomeOtherInterface
,
211 public RefcountedType
{
214 void DoFoo() override
{}
216 // SomeOtherInterface
217 void DoBar() override
{}
220 ~UnsafeInheritanceChain() override
{}
223 #endif // BASE_REFCOUNTED_H_