OLD | NEW |
(Empty) | |
| 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 |
| 11 // Bind() is an overloaded function that converts method calls into function |
| 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. |
| 19 // |
| 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 // |
| 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 // } |
| 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. |
| 48 // invoker.AsyncInvoke(RTC_FROM_HERE,rtc::Bind(&Bar::Test, this)); |
| 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 // |
| 60 |
| 61 #ifndef WEBRTC_BASE_BIND_H_ |
| 62 #define WEBRTC_BASE_BIND_H_ |
| 63 |
| 64 |
| 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" |
| 68 |
| 69 #endif // WEBRTC_BASE_BIND_H_ |
OLD | NEW |