| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_CALLBACK_H_ | |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_CALLBACK_H_ | |
| 7 | |
| 8 #include <functional> | |
| 9 #include <type_traits> | |
| 10 | |
| 11 #include "mojo/public/cpp/bindings/lib/callback_internal.h" | |
| 12 #include "mojo/public/cpp/bindings/lib/shared_ptr.h" | |
| 13 #include "mojo/public/cpp/bindings/lib/template_util.h" | |
| 14 | |
| 15 namespace mojo { | |
| 16 | |
| 17 template <typename Sig> | |
| 18 class Callback; | |
| 19 | |
| 20 // Represents a callback with any number of parameters and no return value. The | |
| 21 // callback is executed by calling its Run() method. The callback may be "null", | |
| 22 // meaning it does nothing. | |
| 23 template <typename... Args> | |
| 24 class Callback<void(Args...)> { | |
| 25 public: | |
| 26 // An interface that may be implemented to define the Run() method. | |
| 27 struct Runnable { | |
| 28 virtual ~Runnable() {} | |
| 29 virtual void Run( | |
| 30 // ForwardType ensures String is passed as a const reference. | |
| 31 typename internal::Callback_ParamTraits<Args>::ForwardType...) | |
| 32 const = 0; | |
| 33 }; | |
| 34 | |
| 35 // Constructs a "null" callback that does nothing. | |
| 36 Callback() {} | |
| 37 | |
| 38 // Constructs a callback that will run |runnable|. The callback takes | |
| 39 // ownership of |runnable|. | |
| 40 explicit Callback(Runnable* runnable) : sink_(runnable) {} | |
| 41 | |
| 42 // Adapts any type that is copy-constructable and has a compatible Run method | |
| 43 // or is convertible to std::function<void(Args...)> (such as a lambda of the | |
| 44 // correct type). | |
| 45 template <typename Sink> | |
| 46 Callback(const Sink& sink) { | |
| 47 using sink_type = typename std::conditional< | |
| 48 std::is_convertible<Sink, std::function<void(Args...)>>::value, | |
| 49 FunctorAdapter<Sink>, RunnableAdapter<Sink>>::type; | |
| 50 sink_ = internal::SharedPtr<Runnable>(new sink_type(sink)); | |
| 51 } | |
| 52 | |
| 53 // As above, but can take a compatible function pointer. | |
| 54 Callback(void (*function_ptr)( | |
| 55 typename internal::Callback_ParamTraits<Args>::ForwardType...)) | |
| 56 : sink_(new FunctionPtrAdapter(function_ptr)) {} | |
| 57 | |
| 58 // Executes the callback function, invoking Pass() on move-only types. | |
| 59 void Run(typename internal::Callback_ParamTraits<Args>::ForwardType... args) | |
| 60 const { | |
| 61 if (sink_.get()) | |
| 62 sink_->Run(internal::Forward(args)...); | |
| 63 } | |
| 64 | |
| 65 bool is_null() const { return !sink_.get(); } | |
| 66 | |
| 67 // Resets the callback to the "null" state. | |
| 68 void reset() { sink_.reset(); } | |
| 69 | |
| 70 private: | |
| 71 // Adapts a class that has a Run() method but is not derived from Runnable to | |
| 72 // be callable by Callback. | |
| 73 template <typename Sink> | |
| 74 struct RunnableAdapter : public Runnable { | |
| 75 explicit RunnableAdapter(const Sink& sink) : sink(sink) {} | |
| 76 virtual void Run( | |
| 77 typename internal::Callback_ParamTraits<Args>::ForwardType... args) | |
| 78 const override { | |
| 79 sink.Run(internal::Forward(args)...); | |
| 80 } | |
| 81 Sink sink; | |
| 82 }; | |
| 83 | |
| 84 // Adapts a class that has a compatible operator() to be callable by Callback. | |
| 85 template <typename Sink> | |
| 86 struct FunctorAdapter : public Runnable { | |
| 87 explicit FunctorAdapter(const Sink& sink) : sink(sink) {} | |
| 88 virtual void Run( | |
| 89 typename internal::Callback_ParamTraits<Args>::ForwardType... args) | |
| 90 const override { | |
| 91 sink.operator()(internal::Forward(args)...); | |
| 92 } | |
| 93 Sink sink; | |
| 94 }; | |
| 95 | |
| 96 // Adapts a function pointer. | |
| 97 struct FunctionPtrAdapter : public Runnable { | |
| 98 explicit FunctionPtrAdapter(void (*function_ptr)( | |
| 99 typename internal::Callback_ParamTraits<Args>::ForwardType...)) | |
| 100 : function_ptr(function_ptr) {} | |
| 101 virtual void Run( | |
| 102 typename internal::Callback_ParamTraits<Args>::ForwardType... args) | |
| 103 const override { | |
| 104 (*function_ptr)(internal::Forward(args)...); | |
| 105 } | |
| 106 void (*function_ptr)( | |
| 107 typename internal::Callback_ParamTraits<Args>::ForwardType...); | |
| 108 }; | |
| 109 | |
| 110 internal::SharedPtr<Runnable> sink_; | |
| 111 }; | |
| 112 | |
| 113 // A specialization of Callback which takes no parameters. | |
| 114 typedef Callback<void()> Closure; | |
| 115 | |
| 116 } // namespace mojo | |
| 117 | |
| 118 #endif // MOJO_PUBLIC_CPP_BINDINGS_CALLBACK_H_ | |
| OLD | NEW |