OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #ifndef MOJO_PUBLIC_CPP_BINDINGS_CALLBACK_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_CALLBACK_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_CALLBACK_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_CALLBACK_H_ |
7 | 7 |
8 #include "mojo/public/cpp/bindings/lib/callback_internal.h" | 8 #include "mojo/public/cpp/bindings/lib/callback_internal.h" |
9 #include "mojo/public/cpp/bindings/lib/shared_ptr.h" | 9 #include "mojo/public/cpp/bindings/lib/shared_ptr.h" |
10 #include "mojo/public/cpp/bindings/lib/template_util.h" | 10 #include "mojo/public/cpp/bindings/lib/template_util.h" |
(...skipping 19 matching lines...) Expand all Loading... |
30 }; | 30 }; |
31 | 31 |
32 // Constructs a "null" callback that does nothing. | 32 // Constructs a "null" callback that does nothing. |
33 Callback() {} | 33 Callback() {} |
34 | 34 |
35 // Constructs a callback that will run |runnable|. The callback takes | 35 // Constructs a callback that will run |runnable|. The callback takes |
36 // ownership of |runnable|. | 36 // ownership of |runnable|. |
37 explicit Callback(Runnable* runnable) : sink_(runnable) {} | 37 explicit Callback(Runnable* runnable) : sink_(runnable) {} |
38 | 38 |
39 // As above, but can take an object that isn't derived from Runnable, so long | 39 // As above, but can take an object that isn't derived from Runnable, so long |
40 // as it has a compatible operator() or Run() method. operator() will be | 40 // as it has a compatible operator() or Run() method. operator() will be |
41 // preferred if the type has both. | 41 // preferred if the type has both. |
42 template <typename Sink> | 42 template <typename Sink> |
43 Callback(const Sink& sink) { | 43 Callback(const Sink& sink) { |
44 using sink_type = typename internal::Conditional< | 44 using sink_type = typename internal::Conditional< |
45 internal::HasCompatibleCallOperator<Sink, Args...>::value, | 45 internal::HasCompatibleCallOperator<Sink, Args...>::value, |
46 FunctorAdapter<Sink>, RunnableAdapter<Sink>>::type; | 46 FunctorAdapter<Sink>, RunnableAdapter<Sink>>::type; |
47 sink_ = internal::SharedPtr<Runnable>(new sink_type(sink)); | 47 sink_ = internal::SharedPtr<Runnable>(new sink_type(sink)); |
48 } | 48 } |
49 | 49 |
| 50 // As above, but can take a compatible function pointer. |
| 51 Callback(void (*function_ptr)( |
| 52 typename internal::Callback_ParamTraits<Args>::ForwardType...)) |
| 53 : sink_(new FunctionPtrAdapter(function_ptr)) {} |
| 54 |
50 // Executes the callback function, invoking Pass() on move-only types. | 55 // Executes the callback function, invoking Pass() on move-only types. |
51 void Run(typename internal::Callback_ParamTraits<Args>::ForwardType... args) | 56 void Run(typename internal::Callback_ParamTraits<Args>::ForwardType... args) |
52 const { | 57 const { |
53 if (sink_.get()) | 58 if (sink_.get()) |
54 sink_->Run(internal::Forward(args)...); | 59 sink_->Run(internal::Forward(args)...); |
55 } | 60 } |
56 | 61 |
57 bool is_null() const { return !sink_.get(); } | 62 bool is_null() const { return !sink_.get(); } |
58 | 63 |
59 // Resets the callback to the "null" state. | 64 // Resets the callback to the "null" state. |
60 void reset() { sink_.reset(); } | 65 void reset() { sink_.reset(); } |
61 | 66 |
62 private: | 67 private: |
63 // Adapts a class that has a Run() method but is not derived from Runnable to | 68 // Adapts a class that has a Run() method but is not derived from Runnable to |
64 // be callable by Callback. | 69 // be callable by Callback. |
65 template <typename Sink> | 70 template <typename Sink> |
66 struct RunnableAdapter : public Runnable { | 71 struct RunnableAdapter : public Runnable { |
67 explicit RunnableAdapter(const Sink& sink) : sink(sink) {} | 72 explicit RunnableAdapter(const Sink& sink) : sink(sink) {} |
68 virtual void Run( | 73 virtual void Run( |
69 typename internal::Callback_ParamTraits<Args>::ForwardType... args) | 74 typename internal::Callback_ParamTraits<Args>::ForwardType... args) |
70 const override { | 75 const override { |
71 sink.Run(internal::Forward(args)...); | 76 sink.Run(internal::Forward(args)...); |
72 } | 77 } |
73 Sink sink; | 78 Sink sink; |
74 }; | 79 }; |
75 | 80 |
76 // Adapts a class that has a compatible operator() callable by Callback. | 81 // Adapts a class that has a compatible operator() to be callable by Callback. |
77 template <typename Sink> | 82 template <typename Sink> |
78 struct FunctorAdapter : public Runnable { | 83 struct FunctorAdapter : public Runnable { |
79 explicit FunctorAdapter(const Sink& sink) : sink(sink) {} | 84 explicit FunctorAdapter(const Sink& sink) : sink(sink) {} |
80 virtual void Run( | 85 virtual void Run( |
81 typename internal::Callback_ParamTraits<Args>::ForwardType... args) | 86 typename internal::Callback_ParamTraits<Args>::ForwardType... args) |
82 const override { | 87 const override { |
83 sink.operator()(internal::Forward(args)...); | 88 sink.operator()(internal::Forward(args)...); |
84 } | 89 } |
85 Sink sink; | 90 Sink sink; |
86 }; | 91 }; |
87 | 92 |
| 93 // Adapts a function pointer. |
| 94 struct FunctionPtrAdapter : public Runnable { |
| 95 explicit FunctionPtrAdapter(void (*function_ptr)( |
| 96 typename internal::Callback_ParamTraits<Args>::ForwardType...)) |
| 97 : function_ptr(function_ptr) {} |
| 98 virtual void Run( |
| 99 typename internal::Callback_ParamTraits<Args>::ForwardType... args) |
| 100 const override { |
| 101 (*function_ptr)(internal::Forward(args)...); |
| 102 } |
| 103 void (*function_ptr)( |
| 104 typename internal::Callback_ParamTraits<Args>::ForwardType...); |
| 105 }; |
| 106 |
88 internal::SharedPtr<Runnable> sink_; | 107 internal::SharedPtr<Runnable> sink_; |
89 }; | 108 }; |
90 | 109 |
91 // A specialization of Callback which takes no parameters. | 110 // A specialization of Callback which takes no parameters. |
92 typedef Callback<void()> Closure; | 111 typedef Callback<void()> Closure; |
93 | 112 |
94 } // namespace mojo | 113 } // namespace mojo |
95 | 114 |
96 #endif // MOJO_PUBLIC_CPP_BINDINGS_CALLBACK_H_ | 115 #endif // MOJO_PUBLIC_CPP_BINDINGS_CALLBACK_H_ |
OLD | NEW |