xref: /aosp_15_r20/external/webrtc/rtc_base/ref_count.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright 2011 The WebRTC Project Authors. All rights reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker #ifndef RTC_BASE_REF_COUNT_H_
11*d9f75844SAndroid Build Coastguard Worker #define RTC_BASE_REF_COUNT_H_
12*d9f75844SAndroid Build Coastguard Worker 
13*d9f75844SAndroid Build Coastguard Worker namespace rtc {
14*d9f75844SAndroid Build Coastguard Worker 
15*d9f75844SAndroid Build Coastguard Worker // Refcounted objects should implement the following informal interface:
16*d9f75844SAndroid Build Coastguard Worker //
17*d9f75844SAndroid Build Coastguard Worker // void AddRef() const ;
18*d9f75844SAndroid Build Coastguard Worker // RefCountReleaseStatus Release() const;
19*d9f75844SAndroid Build Coastguard Worker //
20*d9f75844SAndroid Build Coastguard Worker // You may access members of a reference-counted object, including the AddRef()
21*d9f75844SAndroid Build Coastguard Worker // and Release() methods, only if you already own a reference to it, or if
22*d9f75844SAndroid Build Coastguard Worker // you're borrowing someone else's reference. (A newly created object is a
23*d9f75844SAndroid Build Coastguard Worker // special case: the reference count is zero on construction, and the code that
24*d9f75844SAndroid Build Coastguard Worker // creates the object should immediately call AddRef(), bringing the reference
25*d9f75844SAndroid Build Coastguard Worker // count from zero to one, e.g., by constructing an rtc::scoped_refptr).
26*d9f75844SAndroid Build Coastguard Worker //
27*d9f75844SAndroid Build Coastguard Worker // AddRef() creates a new reference to the object.
28*d9f75844SAndroid Build Coastguard Worker //
29*d9f75844SAndroid Build Coastguard Worker // Release() releases a reference to the object; the caller now has one less
30*d9f75844SAndroid Build Coastguard Worker // reference than before the call. Returns kDroppedLastRef if the number of
31*d9f75844SAndroid Build Coastguard Worker // references dropped to zero because of this (in which case the object destroys
32*d9f75844SAndroid Build Coastguard Worker // itself). Otherwise, returns kOtherRefsRemained, to signal that at the precise
33*d9f75844SAndroid Build Coastguard Worker // time the caller's reference was dropped, other references still remained (but
34*d9f75844SAndroid Build Coastguard Worker // if other threads own references, this may of course have changed by the time
35*d9f75844SAndroid Build Coastguard Worker // Release() returns).
36*d9f75844SAndroid Build Coastguard Worker //
37*d9f75844SAndroid Build Coastguard Worker // The caller of Release() must treat it in the same way as a delete operation:
38*d9f75844SAndroid Build Coastguard Worker // Regardless of the return value from Release(), the caller mustn't access the
39*d9f75844SAndroid Build Coastguard Worker // object. The object might still be alive, due to references held by other
40*d9f75844SAndroid Build Coastguard Worker // users of the object, but the object can go away at any time, e.g., as the
41*d9f75844SAndroid Build Coastguard Worker // result of another thread calling Release().
42*d9f75844SAndroid Build Coastguard Worker //
43*d9f75844SAndroid Build Coastguard Worker // Calling AddRef() and Release() manually is discouraged. It's recommended to
44*d9f75844SAndroid Build Coastguard Worker // use rtc::scoped_refptr to manage all pointers to reference counted objects.
45*d9f75844SAndroid Build Coastguard Worker // Note that rtc::scoped_refptr depends on compile-time duck-typing; formally
46*d9f75844SAndroid Build Coastguard Worker // implementing the below RefCountInterface is not required.
47*d9f75844SAndroid Build Coastguard Worker 
48*d9f75844SAndroid Build Coastguard Worker enum class RefCountReleaseStatus { kDroppedLastRef, kOtherRefsRemained };
49*d9f75844SAndroid Build Coastguard Worker 
50*d9f75844SAndroid Build Coastguard Worker // Interfaces where refcounting is part of the public api should
51*d9f75844SAndroid Build Coastguard Worker // inherit this abstract interface. The implementation of these
52*d9f75844SAndroid Build Coastguard Worker // methods is usually provided by the RefCountedObject template class,
53*d9f75844SAndroid Build Coastguard Worker // applied as a leaf in the inheritance tree.
54*d9f75844SAndroid Build Coastguard Worker class RefCountInterface {
55*d9f75844SAndroid Build Coastguard Worker  public:
56*d9f75844SAndroid Build Coastguard Worker   virtual void AddRef() const = 0;
57*d9f75844SAndroid Build Coastguard Worker   virtual RefCountReleaseStatus Release() const = 0;
58*d9f75844SAndroid Build Coastguard Worker 
59*d9f75844SAndroid Build Coastguard Worker   // Non-public destructor, because Release() has exclusive responsibility for
60*d9f75844SAndroid Build Coastguard Worker   // destroying the object.
61*d9f75844SAndroid Build Coastguard Worker  protected:
~RefCountInterface()62*d9f75844SAndroid Build Coastguard Worker   virtual ~RefCountInterface() {}
63*d9f75844SAndroid Build Coastguard Worker };
64*d9f75844SAndroid Build Coastguard Worker 
65*d9f75844SAndroid Build Coastguard Worker }  // namespace rtc
66*d9f75844SAndroid Build Coastguard Worker 
67*d9f75844SAndroid Build Coastguard Worker #endif  // RTC_BASE_REF_COUNT_H_
68