File: base_refcounted.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (223 lines) | stat: -rw-r--r-- 5,419 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_REFCOUNTED_H_
#define BASE_REFCOUNTED_H_

namespace base {

template <typename T>
class RefCounted {
 public:
  RefCounted() {}
 protected:
  ~RefCounted() {}
};

template <typename T>
class RefCountedThreadSafe {
 public:
  RefCountedThreadSafe() {}
 protected:
  ~RefCountedThreadSafe() {}
};

}  // namespace base

// Ignore classes whose inheritance tree ends in WebKit's RefCounted base
// class. Though prone to error, this pattern is very prevalent in WebKit
// code, so do not issue any warnings.
namespace WebKit {

template <typename T>
class RefCounted {
 public:
  RefCounted() {}
  ~RefCounted() {}
};

}  // namespace WebKit

// Unsafe; should error.
class PublicRefCountedDtorInHeader
    : public base::RefCounted<PublicRefCountedDtorInHeader> {
 public:
  PublicRefCountedDtorInHeader() {}
  ~PublicRefCountedDtorInHeader() {}

 private:
  friend class base::RefCounted<PublicRefCountedDtorInHeader>;
};

// Unsafe; should error.
class PublicRefCountedThreadSafeDtorInHeader
    : public base::RefCountedThreadSafe<
          PublicRefCountedThreadSafeDtorInHeader> {
 public:
  PublicRefCountedThreadSafeDtorInHeader() {}
  ~PublicRefCountedThreadSafeDtorInHeader() {}

 private:
  friend class base::RefCountedThreadSafe<
      PublicRefCountedThreadSafeDtorInHeader>;
};

// Unsafe; should error.
class ProtectedRefCountedDtorInHeader
    : public base::RefCounted<ProtectedRefCountedDtorInHeader> {
 public:
  ProtectedRefCountedDtorInHeader() {}

 protected:
  ~ProtectedRefCountedDtorInHeader() {}

 private:
  friend class base::RefCounted<ProtectedRefCountedDtorInHeader>;
};

// Safe; should not have errors
class ProtectedRefCountedVirtualDtorInHeader
    : public base::RefCounted<ProtectedRefCountedVirtualDtorInHeader> {
 public:
  ProtectedRefCountedVirtualDtorInHeader() {}

 protected:
  virtual ~ProtectedRefCountedVirtualDtorInHeader() {}

 private:
  friend class base::RefCounted<ProtectedRefCountedVirtualDtorInHeader>;
};


// Safe; should not have errors.
class PrivateRefCountedDtorInHeader
    : public base::RefCounted<PrivateRefCountedDtorInHeader> {
 public:
  PrivateRefCountedDtorInHeader() {}

 private:
  ~PrivateRefCountedDtorInHeader() {}
  friend class base::RefCounted<PrivateRefCountedDtorInHeader>;
};

// Unsafe; A grandchild class ends up exposing their parent and grandparent's
// destructors.
class DerivedProtectedToPublicInHeader
    : public ProtectedRefCountedVirtualDtorInHeader {
 public:
  DerivedProtectedToPublicInHeader() {}
  ~DerivedProtectedToPublicInHeader() override {}
};

// Unsafe; A grandchild ends up implicitly exposing their parent and
// grantparent's destructors.
class ImplicitDerivedProtectedToPublicInHeader
    : public ProtectedRefCountedVirtualDtorInHeader {
 public:
  ImplicitDerivedProtectedToPublicInHeader() {}
};

// Unsafe-but-ignored; should not have errors.
class WebKitPublicDtorInHeader
    : public WebKit::RefCounted<WebKitPublicDtorInHeader> {
 public:
  WebKitPublicDtorInHeader() {}
  ~WebKitPublicDtorInHeader() {}
};

// Unsafe-but-ignored; should not have errors.
class WebKitDerivedPublicDtorInHeader
    : public WebKitPublicDtorInHeader {
 public:
  WebKitDerivedPublicDtorInHeader() {}
  ~WebKitDerivedPublicDtorInHeader() {}
};

class APublicInterface {
 public:
  virtual ~APublicInterface() {}
  virtual void DoFoo() = 0;
};

// Unsafe. "ImplementsAPublicInterface* foo" can be deleted via
// "delete (APublicInterface*)foo;".
class ImplementsAPublicInterface
    : public APublicInterface,
      public base::RefCounted<ImplementsAPublicInterface> {
 public:
  void DoFoo() override {}

 protected:
  ~ImplementsAPublicInterface() override {}

 private:
  friend class base::RefCounted<ImplementsAPublicInterface>;
};

class AnImplicitInterface {
 public:
  virtual void DoBar() {}
};

// Unsafe.
class ImplementsAnImplicitInterface
    : public AnImplicitInterface,
      public base::RefCounted<ImplementsAnImplicitInterface> {
 public:
  void DoBar() override {}

 private:
  friend class base::RefCounted<ImplementsAnImplicitInterface>;
  ~ImplementsAnImplicitInterface() {}
};

// Safe. Private inheritance does not expose the base destructor.
class PrivatelyImplementsAPublicInterface
    : private APublicInterface,
      public base::RefCounted<PrivatelyImplementsAPublicInterface> {
 public:
  void DoFoo() override {}

 private:
  friend class base::RefCounted<PrivatelyImplementsAPublicInterface>;
  ~PrivatelyImplementsAPublicInterface() override {}
};

// Unsafe.
class BaseInterface {
 public:
  virtual ~BaseInterface() {}
  virtual void DoFoo() {}
};
class DerivedInterface : public BaseInterface {
 protected:
  ~DerivedInterface() override {}
};
class SomeOtherInterface {
 public:
  virtual ~SomeOtherInterface() {}
  virtual void DoBar() {}
};
class RefcountedType : public base::RefCounted<RefcountedType> {
 protected:
  ~RefcountedType() {}
 private:
  friend class base::RefCounted<RefcountedType>;
};
class UnsafeInheritanceChain
    : public DerivedInterface,
      public SomeOtherInterface,
      public RefcountedType {
 public:
  // DerivedInterface
  void DoFoo() override {}

  // SomeOtherInterface
  void DoBar() override {}

 protected:
  ~UnsafeInheritanceChain() override {}
};

#endif  // BASE_REFCOUNTED_H_