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 "mojo/public/cpp/bindings/lib/callback_internal.h" | |
9 #include "mojo/public/cpp/bindings/lib/shared_ptr.h" | |
10 #include "mojo/public/cpp/bindings/lib/template_util.h" | |
11 | |
12 namespace mojo { | |
13 | |
14 template <typename Sig> | |
15 class Callback; | |
16 | |
17 template <typename... Args> | |
18 class Callback<void(Args...)> { | |
19 public: | |
20 struct Runnable { | |
21 virtual ~Runnable() {} | |
22 virtual void Run( | |
23 typename internal::Callback_ParamTraits<Args>::ForwardType...) | |
24 const = 0; | |
25 }; | |
26 | |
27 Callback() {} | |
28 | |
29 // The Callback assumes ownership of |runnable|. | |
30 explicit Callback(Runnable* runnable) : sink_(runnable) {} | |
31 | |
32 // Any class that is copy-constructable and has a compatible Run method may | |
33 // be adapted to a Callback using this constructor. | |
34 template <typename Sink> | |
35 Callback(const Sink& sink) | |
36 : sink_(new Adapter<Sink>(sink)) {} | |
37 | |
38 void Run(typename internal::Callback_ParamTraits<Args>::ForwardType... args) | |
39 const { | |
40 if (sink_.get()) | |
41 sink_->Run(internal::Forward(args)...); | |
42 } | |
43 | |
44 bool is_null() const { return !sink_.get(); } | |
45 | |
46 void reset() { sink_.reset(); } | |
47 | |
48 private: | |
49 template <typename Sink> | |
50 struct Adapter : public Runnable { | |
51 explicit Adapter(const Sink& sink) : sink(sink) {} | |
52 virtual void Run( | |
53 typename internal::Callback_ParamTraits<Args>::ForwardType... args) | |
54 const override { | |
55 sink.Run(internal::Forward(args)...); | |
56 } | |
57 Sink sink; | |
58 }; | |
59 | |
60 internal::SharedPtr<Runnable> sink_; | |
61 }; | |
62 | |
63 typedef Callback<void()> Closure; | |
64 | |
65 } // namespace mojo | |
66 | |
67 #endif // MOJO_PUBLIC_CPP_BINDINGS_CALLBACK_H_ | |
OLD | NEW |