| 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 <type_traits> | 8 #include <type_traits> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 public: | 26 public: |
| 27 // An interface that may be implemented to define the Run() method. | 27 // An interface that may be implemented to define the Run() method. |
| 28 struct Runnable { | 28 struct Runnable { |
| 29 virtual ~Runnable() {} | 29 virtual ~Runnable() {} |
| 30 virtual void Run( | 30 virtual void Run( |
| 31 // ForwardType ensures String is passed as a const reference. Can't use | 31 // ForwardType ensures String is passed as a const reference. Can't use |
| 32 // universal refs (Args&&) here since this method itself isn't templated | 32 // universal refs (Args&&) here since this method itself isn't templated |
| 33 // because it is a virtual interface. So we have to take the arguments | 33 // because it is a virtual interface. So we have to take the arguments |
| 34 // all by value (except String which we take as a const reference due to | 34 // all by value (except String which we take as a const reference due to |
| 35 // ForwardType). | 35 // ForwardType). |
| 36 typename internal::Callback_ParamTraits<Args>::ForwardType...) | 36 typename internal::Callback_ParamTraits<Args>::ForwardType...) = 0; |
| 37 const = 0; | |
| 38 }; | 37 }; |
| 39 | 38 |
| 40 // Constructs a "null" callback that does nothing. | 39 // Constructs a "null" callback that does nothing. |
| 41 Callback() {} | 40 Callback() {} |
| 42 | 41 |
| 43 // Constructs a callback that will run |runnable|. The callback takes | 42 // Constructs a callback that will run |runnable|. The callback takes |
| 44 // ownership of |runnable|. | 43 // ownership of |runnable|. |
| 45 explicit Callback(Runnable* runnable) : sink_(new RunnableHolder(runnable)) {} | 44 explicit Callback(Runnable* runnable) : sink_(new RunnableHolder(runnable)) {} |
| 46 | 45 |
| 47 // As above, but can take an object that isn't derived from Runnable, so long | 46 // As above, but can take an object that isn't derived from Runnable, so long |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 void reset() { sink_ = nullptr; } | 90 void reset() { sink_ = nullptr; } |
| 92 | 91 |
| 93 private: | 92 private: |
| 94 // Adapts a class that has a Run() method but is not derived from Runnable to | 93 // Adapts a class that has a Run() method but is not derived from Runnable to |
| 95 // be callable by Callback. | 94 // be callable by Callback. |
| 96 template <typename Sink> | 95 template <typename Sink> |
| 97 struct RunnableAdapter : public Runnable { | 96 struct RunnableAdapter : public Runnable { |
| 98 explicit RunnableAdapter(const Sink& sink) : sink(sink) {} | 97 explicit RunnableAdapter(const Sink& sink) : sink(sink) {} |
| 99 virtual void Run( | 98 virtual void Run( |
| 100 typename internal::Callback_ParamTraits<Args>::ForwardType... args) | 99 typename internal::Callback_ParamTraits<Args>::ForwardType... args) |
| 101 const override { | 100 override { |
| 102 sink.Run(std::forward< | 101 sink.Run(std::forward< |
| 103 typename internal::Callback_ParamTraits<Args>::ForwardType>( | 102 typename internal::Callback_ParamTraits<Args>::ForwardType>( |
| 104 args)...); | 103 args)...); |
| 105 } | 104 } |
| 106 Sink sink; | 105 Sink sink; |
| 107 }; | 106 }; |
| 108 | 107 |
| 109 // Adapts a class that has a compatible operator() to be callable by Callback. | 108 // Adapts a class that has a compatible operator() to be callable by Callback. |
| 110 template <typename Sink> | 109 template <typename Sink> |
| 111 struct FunctorAdapter : public Runnable { | 110 struct FunctorAdapter : public Runnable { |
| 112 explicit FunctorAdapter(const Sink& sink) : sink(sink) {} | 111 explicit FunctorAdapter(const Sink& sink) : sink(sink) {} |
| 113 virtual void Run( | 112 virtual void Run( |
| 114 typename internal::Callback_ParamTraits<Args>::ForwardType... args) | 113 typename internal::Callback_ParamTraits<Args>::ForwardType... args) |
| 115 const override { | 114 override { |
| 116 sink.operator()( | 115 sink.operator()( |
| 117 std::forward< | 116 std::forward< |
| 118 typename internal::Callback_ParamTraits<Args>::ForwardType>( | 117 typename internal::Callback_ParamTraits<Args>::ForwardType>( |
| 119 args)...); | 118 args)...); |
| 120 } | 119 } |
| 121 Sink sink; | 120 Sink sink; |
| 122 }; | 121 }; |
| 123 | 122 |
| 124 // Adapts a function pointer. | 123 // Adapts a function pointer. |
| 125 struct FunctionPtrAdapter : public Runnable { | 124 struct FunctionPtrAdapter : public Runnable { |
| 126 private: | 125 private: |
| 127 using FunctionPtr = | 126 using FunctionPtr = |
| 128 void (*)(typename internal::Callback_ParamTraits<Args>::ForwardType...); | 127 void (*)(typename internal::Callback_ParamTraits<Args>::ForwardType...); |
| 129 | 128 |
| 130 public: | 129 public: |
| 131 explicit FunctionPtrAdapter(FunctionPtr function_ptr) | 130 explicit FunctionPtrAdapter(FunctionPtr function_ptr) |
| 132 : function_ptr(function_ptr) {} | 131 : function_ptr(function_ptr) {} |
| 133 virtual void Run( | 132 virtual void Run( |
| 134 typename internal::Callback_ParamTraits<Args>::ForwardType... args) | 133 typename internal::Callback_ParamTraits<Args>::ForwardType... args) |
| 135 const override { | 134 override { |
| 136 (*function_ptr)( | 135 (*function_ptr)( |
| 137 std::forward< | 136 std::forward< |
| 138 typename internal::Callback_ParamTraits<Args>::ForwardType>( | 137 typename internal::Callback_ParamTraits<Args>::ForwardType>( |
| 139 args)...); | 138 args)...); |
| 140 } | 139 } |
| 141 FunctionPtr function_ptr; | 140 FunctionPtr function_ptr; |
| 142 }; | 141 }; |
| 143 | 142 |
| 144 struct RunnableHolder : public base::RefCountedThreadSafe<RunnableHolder> { | 143 struct RunnableHolder : public base::RefCountedThreadSafe<RunnableHolder> { |
| 145 explicit RunnableHolder(Runnable* runnable) : runnable(runnable) {} | 144 explicit RunnableHolder(Runnable* runnable) : runnable(runnable) {} |
| 146 | 145 |
| 147 scoped_ptr<Runnable> runnable; | 146 scoped_ptr<Runnable> runnable; |
| 148 | 147 |
| 149 private: | 148 private: |
| 150 friend class base::RefCountedThreadSafe<RunnableHolder>; | 149 friend class base::RefCountedThreadSafe<RunnableHolder>; |
| 151 ~RunnableHolder() {} | 150 ~RunnableHolder() {} |
| 152 }; | 151 }; |
| 153 | 152 |
| 154 scoped_refptr<RunnableHolder> sink_; | 153 scoped_refptr<RunnableHolder> sink_; |
| 155 }; | 154 }; |
| 156 | 155 |
| 157 // A specialization of Callback which takes no parameters. | 156 // A specialization of Callback which takes no parameters. |
| 158 typedef Callback<void()> Closure; | 157 typedef Callback<void()> Closure; |
| 159 | 158 |
| 160 } // namespace mojo | 159 } // namespace mojo |
| 161 | 160 |
| 162 #endif // MOJO_PUBLIC_CPP_BINDINGS_CALLBACK_H_ | 161 #endif // MOJO_PUBLIC_CPP_BINDINGS_CALLBACK_H_ |
| OLD | NEW |