keep_ref_until_done.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. * Copyright 2015 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_KEEP_REF_UNTIL_DONE_H_
  11. #define RTC_BASE_KEEP_REF_UNTIL_DONE_H_
  12. #include "api/scoped_refptr.h"
  13. #include "rtc_base/bind.h"
  14. #include "rtc_base/callback.h"
  15. #include "rtc_base/ref_count.h"
  16. namespace rtc {
  17. namespace impl {
  18. template <class T>
  19. static inline void DoNothing(const scoped_refptr<T>& object) {}
  20. } // namespace impl
  21. // KeepRefUntilDone keeps a reference to |object| until the returned
  22. // callback goes out of scope. If the returned callback is copied, the
  23. // reference will be released when the last callback goes out of scope.
  24. template <class ObjectT>
  25. static inline Callback0<void> KeepRefUntilDone(ObjectT* object) {
  26. return rtc::Bind(&impl::DoNothing<ObjectT>, scoped_refptr<ObjectT>(object));
  27. }
  28. template <class ObjectT>
  29. static inline Callback0<void> KeepRefUntilDone(
  30. const scoped_refptr<ObjectT>& object) {
  31. return rtc::Bind(&impl::DoNothing<ObjectT>, object);
  32. }
  33. } // namespace rtc
  34. #endif // RTC_BASE_KEEP_REF_UNTIL_DONE_H_