blob: 7bebd823746ec4edfa9b5ecee3e00b8cf01540f5 [file] [log] [blame]
perkj14f41442015-11-30 22:15:45 -08001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_KEEP_REF_UNTIL_DONE_H_
12#define RTC_BASE_KEEP_REF_UNTIL_DONE_H_
perkj14f41442015-11-30 22:15:45 -080013
Mirko Bonadeid9708072019-01-25 20:26:48 +010014#include "api/scoped_refptr.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/bind.h"
16#include "rtc_base/callback.h"
Steve Anton10542f22019-01-11 09:11:00 -080017#include "rtc_base/ref_count.h"
perkj14f41442015-11-30 22:15:45 -080018
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020019namespace rtc {
20
21namespace impl {
22template <class T>
23static inline void DoNothing(const scoped_refptr<T>& object) {}
24} // namespace impl
25
26// KeepRefUntilDone keeps a reference to |object| until the returned
27// callback goes out of scope. If the returned callback is copied, the
28// reference will be released when the last callback goes out of scope.
29template <class ObjectT>
30static inline Callback0<void> KeepRefUntilDone(ObjectT* object) {
31 return rtc::Bind(&impl::DoNothing<ObjectT>, scoped_refptr<ObjectT>(object));
32}
33
34template <class ObjectT>
35static inline Callback0<void> KeepRefUntilDone(
36 const scoped_refptr<ObjectT>& object) {
37 return rtc::Bind(&impl::DoNothing<ObjectT>, object);
38}
39
40} // namespace rtc
41
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#endif // RTC_BASE_KEEP_REF_UNTIL_DONE_H_