blob: 5ae0ed1b210ac936f3506a8fe4056f16a37f1e6e [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/callback.h"
perkj14f41442015-11-30 22:15:45 -080016
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020017namespace rtc {
18
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020019// KeepRefUntilDone keeps a reference to |object| until the returned
20// callback goes out of scope. If the returned callback is copied, the
21// reference will be released when the last callback goes out of scope.
22template <class ObjectT>
23static inline Callback0<void> KeepRefUntilDone(ObjectT* object) {
Niels Möllera68bfc52021-01-11 13:26:35 +010024 scoped_refptr<ObjectT> p(object);
25 return [p] {};
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020026}
27
28template <class ObjectT>
29static inline Callback0<void> KeepRefUntilDone(
30 const scoped_refptr<ObjectT>& object) {
Niels Möllera68bfc52021-01-11 13:26:35 +010031 return [object] {};
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020032}
33
34} // namespace rtc
35
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#endif // RTC_BASE_KEEP_REF_UNTIL_DONE_H_