ref_counted_base.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*
  2. * Copyright 2017 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 API_REF_COUNTED_BASE_H_
  11. #define API_REF_COUNTED_BASE_H_
  12. #include "rtc_base/constructor_magic.h"
  13. #include "rtc_base/ref_count.h"
  14. #include "rtc_base/ref_counter.h"
  15. namespace rtc {
  16. class RefCountedBase {
  17. public:
  18. RefCountedBase() = default;
  19. void AddRef() const { ref_count_.IncRef(); }
  20. RefCountReleaseStatus Release() const {
  21. const auto status = ref_count_.DecRef();
  22. if (status == RefCountReleaseStatus::kDroppedLastRef) {
  23. delete this;
  24. }
  25. return status;
  26. }
  27. protected:
  28. virtual ~RefCountedBase() = default;
  29. private:
  30. mutable webrtc::webrtc_impl::RefCounter ref_count_{0};
  31. RTC_DISALLOW_COPY_AND_ASSIGN(RefCountedBase);
  32. };
  33. } // namespace rtc
  34. #endif // API_REF_COUNTED_BASE_H_