henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 11 | // Bind() is an overloaded function that converts method calls into function |
Magnus Jedvert | d3de9c5 | 2015-08-20 16:03:52 +0200 | [diff] [blame] | 12 | // 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 19 | // |
deadbeef | ccaaffb | 2017-02-25 12:56:20 -0800 | [diff] [blame] | 20 | // 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 24 | // 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 Jedvert | d3de9c5 | 2015-08-20 16:03:52 +0200 | [diff] [blame] | 39 | // |
| 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 Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 48 | // invoker.AsyncInvoke(RTC_FROM_HERE,rtc::Bind(&Bar::Test, this)); |
Magnus Jedvert | d3de9c5 | 2015-08-20 16:03:52 +0200 | [diff] [blame] | 49 | // } |
| 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 60 | |
| 61 | #ifndef WEBRTC_BASE_BIND_H_ |
| 62 | #define WEBRTC_BASE_BIND_H_ |
| 63 | |
deadbeef | 08187d4 | 2017-02-25 11:21:18 -0800 | [diff] [blame] | 64 | |
Henrik Kjellander | 6776518 | 2017-06-28 20:58:07 +0200 | [diff] [blame] | 65 | // 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 68 | |
| 69 | #endif // WEBRTC_BASE_BIND_H_ |