| 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 <functional> |
| 8 #include <type_traits> | 9 #include <type_traits> |
| 9 | 10 |
| 10 #include "mojo/public/cpp/bindings/lib/callback_internal.h" | 11 #include "mojo/public/cpp/bindings/lib/callback_internal.h" |
| 11 #include "mojo/public/cpp/bindings/lib/shared_ptr.h" | 12 #include "mojo/public/cpp/bindings/lib/shared_ptr.h" |
| 12 #include "mojo/public/cpp/bindings/lib/template_util.h" | 13 #include "mojo/public/cpp/bindings/lib/template_util.h" |
| 13 | 14 |
| 14 namespace mojo { | 15 namespace mojo { |
| 15 | 16 |
| 16 template <typename Sig> | 17 template <typename Sig> |
| 17 class Callback; | 18 class Callback; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 31 const = 0; | 32 const = 0; |
| 32 }; | 33 }; |
| 33 | 34 |
| 34 // Constructs a "null" callback that does nothing. | 35 // Constructs a "null" callback that does nothing. |
| 35 Callback() {} | 36 Callback() {} |
| 36 | 37 |
| 37 // Constructs a callback that will run |runnable|. The callback takes | 38 // Constructs a callback that will run |runnable|. The callback takes |
| 38 // ownership of |runnable|. | 39 // ownership of |runnable|. |
| 39 explicit Callback(Runnable* runnable) : sink_(runnable) {} | 40 explicit Callback(Runnable* runnable) : sink_(runnable) {} |
| 40 | 41 |
| 41 // As above, but can take an object that isn't derived from Runnable, so long | 42 // Adapts any type that is copy-constructable and has a compatible Run method |
| 42 // as it has a compatible operator() or Run() method. operator() will be | 43 // or is convertible to std::function<void(Args...)> (such as a lambda of the |
| 43 // preferred if the type has both. | 44 // correct type). |
| 44 template <typename Sink> | 45 template <typename Sink> |
| 45 Callback(const Sink& sink) { | 46 Callback(const Sink& sink) { |
| 46 using sink_type = typename std::conditional< | 47 using sink_type = typename std::conditional< |
| 47 internal::HasCompatibleCallOperator<Sink, Args...>::value, | 48 std::is_convertible<Sink, std::function<void(Args...)>>::value, |
| 48 FunctorAdapter<Sink>, RunnableAdapter<Sink>>::type; | 49 FunctorAdapter<Sink>, RunnableAdapter<Sink>>::type; |
| 49 sink_ = internal::SharedPtr<Runnable>(new sink_type(sink)); | 50 sink_ = internal::SharedPtr<Runnable>(new sink_type(sink)); |
| 50 } | 51 } |
| 51 | 52 |
| 52 // As above, but can take a compatible function pointer. | 53 // As above, but can take a compatible function pointer. |
| 53 Callback(void (*function_ptr)( | 54 Callback(void (*function_ptr)( |
| 54 typename internal::Callback_ParamTraits<Args>::ForwardType...)) | 55 typename internal::Callback_ParamTraits<Args>::ForwardType...)) |
| 55 : sink_(new FunctionPtrAdapter(function_ptr)) {} | 56 : sink_(new FunctionPtrAdapter(function_ptr)) {} |
| 56 | 57 |
| 57 // Executes the callback function, invoking Pass() on move-only types. | 58 // Executes the callback function, invoking Pass() on move-only types. |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 | 109 |
| 109 internal::SharedPtr<Runnable> sink_; | 110 internal::SharedPtr<Runnable> sink_; |
| 110 }; | 111 }; |
| 111 | 112 |
| 112 // A specialization of Callback which takes no parameters. | 113 // A specialization of Callback which takes no parameters. |
| 113 typedef Callback<void()> Closure; | 114 typedef Callback<void()> Closure; |
| 114 | 115 |
| 115 } // namespace mojo | 116 } // namespace mojo |
| 116 | 117 |
| 117 #endif // MOJO_PUBLIC_CPP_BINDINGS_CALLBACK_H_ | 118 #endif // MOJO_PUBLIC_CPP_BINDINGS_CALLBACK_H_ |
| OLD | NEW |