blob: 39d441f00861643e498e2476dd2aeae99b05b823 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2012 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
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000011// Bind() is an overloaded function that converts method calls into function
Magnus Jedvertd3de9c52015-08-20 16:03:52 +020012// objects (aka functors). The method object is captured as a scoped_refptr<> if
13// possible, and as a raw pointer otherwise. Any arguments to the method are
14// captured by value. The return value of Bind is a stateful, nullary function
15// object. Care should be taken about the lifetime of objects captured by
16// Bind(); the returned functor knows nothing about the lifetime of a non
17// ref-counted method object or any arguments passed by pointer, and calling the
18// functor with a destroyed object will surely do bad things.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019//
deadbeefccaaffb2017-02-25 12:56:20 -080020// To prevent the method object from being captured as a scoped_refptr<>, you
21// can use Unretained. But this should only be done when absolutely necessary,
22// and when the caller knows the extra reference isn't needed.
23//
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000024// Example usage:
25// struct Foo {
26// int Test1() { return 42; }
27// int Test2() const { return 52; }
28// int Test3(int x) { return x*x; }
29// float Test4(int x, float y) { return x + y; }
30// };
31//
32// int main() {
33// Foo foo;
34// cout << rtc::Bind(&Foo::Test1, &foo)() << endl;
35// cout << rtc::Bind(&Foo::Test2, &foo)() << endl;
36// cout << rtc::Bind(&Foo::Test3, &foo, 3)() << endl;
37// cout << rtc::Bind(&Foo::Test4, &foo, 7, 8.5f)() << endl;
38// }
Magnus Jedvertd3de9c52015-08-20 16:03:52 +020039//
40// Example usage of ref counted objects:
41// struct Bar {
42// int AddRef();
43// int Release();
44//
45// void Test() {}
46// void BindThis() {
47// // The functor passed to AsyncInvoke() will keep this object alive.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070048// invoker.AsyncInvoke(RTC_FROM_HERE,rtc::Bind(&Bar::Test, this));
Magnus Jedvertd3de9c52015-08-20 16:03:52 +020049// }
50// };
51//
52// int main() {
53// rtc::scoped_refptr<Bar> bar = new rtc::RefCountedObject<Bar>();
54// auto functor = rtc::Bind(&Bar::Test, bar);
55// bar = nullptr;
56// // The functor stores an internal scoped_refptr<Bar>, so this is safe.
57// functor();
58// }
59//
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000060
61#ifndef WEBRTC_BASE_BIND_H_
62#define WEBRTC_BASE_BIND_H_
63
deadbeef08187d42017-02-25 11:21:18 -080064
Henrik Kjellander67765182017-06-28 20:58:07 +020065// This header is deprecated and is just left here temporarily during
66// refactoring. See https://bugs.webrtc.org/7634 for more details.
67#include "webrtc/rtc_base/bind.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000068
69#endif // WEBRTC_BASE_BIND_H_