ref_count.h 2.9 KB

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