OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 template <typename> | 5 #include <utility> |
6 class scoped_refptr { | 6 |
| 7 namespace base { |
| 8 namespace internal { |
| 9 |
| 10 enum class CopyMode { MoveOnly, Copyable }; |
| 11 enum class RepeatMode { Once, Repeating }; |
| 12 |
| 13 } // namespace internal |
| 14 |
| 15 template <typename Signature, |
| 16 internal::CopyMode copy_mode = internal::CopyMode::Copyable, |
| 17 internal::RepeatMode repeat_mode = internal::RepeatMode::Repeating> |
| 18 class Callback; |
| 19 |
| 20 template <typename Signature> |
| 21 using OnceCallback = Callback<Signature, |
| 22 internal::CopyMode::MoveOnly, |
| 23 internal::RepeatMode::Once>; |
| 24 template <typename Signature> |
| 25 using RepeatingCallback = Callback<Signature, |
| 26 internal::CopyMode::Copyable, |
| 27 internal::RepeatMode::Repeating>; |
| 28 |
| 29 using Closure = Callback<void()>; |
| 30 using OnceClosure = OnceCallback<void()>; |
| 31 using RepeatingClosure = RepeatingCallback<void()>; |
| 32 |
| 33 namespace internal { |
| 34 |
| 35 template <typename From, typename To> |
| 36 struct IsCallbackConvertible : std::false_type {}; |
| 37 |
| 38 template <typename Signature> |
| 39 struct IsCallbackConvertible<RepeatingCallback<Signature>, |
| 40 OnceCallback<Signature>> : std::true_type {}; |
| 41 |
| 42 } // namespace internal |
| 43 |
| 44 template <typename Signature, internal::CopyMode, internal::RepeatMode> |
| 45 class Callback { |
7 public: | 46 public: |
8 void* get() { return 0; } | 47 Callback() {} |
| 48 Callback(const Callback&) {} |
| 49 Callback(Callback&&) {} |
| 50 |
| 51 template <typename OtherCallback, |
| 52 typename = typename std::enable_if< |
| 53 internal::IsCallbackConvertible<OtherCallback, |
| 54 Callback>::value>::type> |
| 55 Callback(OtherCallback other) {} |
9 }; | 56 }; |
10 | 57 |
11 namespace base { | 58 template <typename Functor, typename... Args> |
| 59 Callback<void()> Bind(Functor, Args&&...) { |
| 60 return Callback<void()>(); |
| 61 } |
12 | 62 |
13 template <typename Functor, typename... Args> | 63 template <typename Functor, typename... Args> |
14 void Bind(Functor&&, Args&&...) {} | 64 OnceCallback<void()> BindOnce(Functor, Args&&...) { |
| 65 return Callback<void()>(); |
| 66 } |
15 | 67 |
16 } // namespace base | 68 } // namespace base |
17 | 69 |
18 struct Foo { | 70 void foo(base::OnceClosure) {} |
19 void Bar(); | |
20 static void Baz(); | |
21 }; | |
22 | 71 |
23 void Test() { | 72 void Test() { |
24 using base::Bind; | 73 base::OnceClosure cb = base::Bind([] {}); |
25 scoped_refptr<int> foo; | 74 foo(base::Bind([] {})); |
26 base::Bind(&Foo::Bar, foo.get()); | 75 |
27 Bind(&Foo::Bar, foo.get()); | 76 using namespace base; |
28 base::Bind(&Foo::Bar, (&foo)->get()); | 77 |
29 base::Bind(&Foo::Bar, foo.get( | 78 OnceClosure cb2 = Bind([] {}); |
30 )); | 79 foo(Bind([] {})); |
31 base::Bind(&Foo::Bar, foo | 80 |
32 .get()); | 81 Closure cb3 = base::Bind([] {}); |
33 base::Bind(&Foo::Bar, foo.get(), foo.get()); | |
34 base::Bind(&Foo::Baz, foo.get()); | |
35 base::Bind(&Foo::Bar, foo.scoped_refptr<int>::get()); | |
36 base::Bind(&Foo::Bar, (&foo)->scoped_refptr<int>::get()); | |
37 } | 82 } |
OLD | NEW |