1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// This file was GENERATED by command:
2a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)//     pump.py callback.h.pump
3a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// DO NOT EDIT BY HAND!!!
4a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// found in the LICENSE file.
10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
11e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#ifndef MOJO_PUBLIC_CPP_BINDINGS_CALLBACK_H_
12e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#define MOJO_PUBLIC_CPP_BINDINGS_CALLBACK_H_
13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
14e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "mojo/public/cpp/bindings/lib/callback_internal.h"
15e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "mojo/public/cpp/bindings/lib/shared_ptr.h"
16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "mojo/public/cpp/bindings/lib/template_util.h"
17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace mojo {
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)template <typename Sig>
21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class Callback;
22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)template <>
24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class Callback<void()> {
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) public:
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  struct Runnable {
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    virtual ~Runnable() {}
28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    virtual void Run() const = 0;
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  };
30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Callback() {}
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // The Callback assumes ownership of |runnable|.
34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  explicit Callback(Runnable* runnable) : sink_(runnable) {}
35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Any class that is copy-constructable and has a compatible Run method may
37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // be adapted to a Callback using this constructor.
38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  template <typename Sink>
39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Callback(const Sink& sink) : sink_(new Adapter<Sink>(sink)) {}
40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void Run() const {
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (sink_.get())
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      sink_->Run();
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool is_null() const {
47116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return !sink_.get();
48116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
49116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) private:
51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  template <typename Sink>
52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  struct Adapter : public Runnable {
53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    explicit Adapter(const Sink& sink) : sink(sink) {}
54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    virtual void Run() const MOJO_OVERRIDE {
55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      sink.Run();
56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    Sink sink;
58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  };
59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  internal::SharedPtr<Runnable> sink_;
61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)};
62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
63a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)template <typename A1>
64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class Callback<void(A1)> {
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) public:
66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  struct Runnable {
67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    virtual ~Runnable() {}
68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    virtual void Run(
69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A1>::ForwardType a1) const = 0;
70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  };
71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
72a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Callback() {}
73a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
74a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // The Callback assumes ownership of |runnable|.
75a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  explicit Callback(Runnable* runnable) : sink_(runnable) {}
76a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
77a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Any class that is copy-constructable and has a compatible Run method may
78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // be adapted to a Callback using this constructor.
79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  template <typename Sink>
80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Callback(const Sink& sink) : sink_(new Adapter<Sink>(sink)) {}
81a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
82116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void Run(typename internal::Callback_ParamTraits<A1>::ForwardType a1) const {
83a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (sink_.get())
84116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      sink_->Run(internal::Forward(a1));
85116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
86116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
87116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool is_null() const {
88116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return !sink_.get();
89a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
90a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
91a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) private:
92a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  template <typename Sink>
93a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  struct Adapter : public Runnable {
94a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    explicit Adapter(const Sink& sink) : sink(sink) {}
95a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    virtual void Run(
96a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A1>::ForwardType a1) const
97a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            MOJO_OVERRIDE {
98116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      sink.Run(internal::Forward(a1));
99a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
100a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    Sink sink;
101a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  };
102a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
103a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  internal::SharedPtr<Runnable> sink_;
104a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)};
105a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
106a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)template <typename A1, typename A2>
107a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class Callback<void(A1, A2)> {
108a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) public:
109a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  struct Runnable {
110a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    virtual ~Runnable() {}
111a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    virtual void Run(
112a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A1>::ForwardType a1,
113a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A2>::ForwardType a2) const = 0;
114a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  };
115a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
116a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Callback() {}
117a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
118a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // The Callback assumes ownership of |runnable|.
119a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  explicit Callback(Runnable* runnable) : sink_(runnable) {}
120a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
121a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Any class that is copy-constructable and has a compatible Run method may
122a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // be adapted to a Callback using this constructor.
123a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  template <typename Sink>
124a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Callback(const Sink& sink) : sink_(new Adapter<Sink>(sink)) {}
125a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
126a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void Run(
127a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      typename internal::Callback_ParamTraits<A1>::ForwardType a1,
128a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      typename internal::Callback_ParamTraits<A2>::ForwardType a2) const {
129a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (sink_.get())
130a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      sink_->Run(
131cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a1),
132cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a2));
133a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
134a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
135116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool is_null() const {
136116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return !sink_.get();
137116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
138116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
139a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) private:
140a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  template <typename Sink>
141a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  struct Adapter : public Runnable {
142a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    explicit Adapter(const Sink& sink) : sink(sink) {}
143a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    virtual void Run(
144a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A1>::ForwardType a1,
145a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A2>::ForwardType a2) const
146a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            MOJO_OVERRIDE {
147a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      sink.Run(
148cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a1),
149cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a2));
150a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
151a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    Sink sink;
152a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  };
153a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
154a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  internal::SharedPtr<Runnable> sink_;
155a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)};
156a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
157a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)template <typename A1, typename A2, typename A3>
158a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class Callback<void(A1, A2, A3)> {
159a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) public:
160a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  struct Runnable {
161a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    virtual ~Runnable() {}
162a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    virtual void Run(
163a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A1>::ForwardType a1,
164a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A2>::ForwardType a2,
165a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A3>::ForwardType a3) const = 0;
166a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  };
167a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
168a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Callback() {}
169a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
170a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // The Callback assumes ownership of |runnable|.
171a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  explicit Callback(Runnable* runnable) : sink_(runnable) {}
172a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
173a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Any class that is copy-constructable and has a compatible Run method may
174a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // be adapted to a Callback using this constructor.
175a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  template <typename Sink>
176a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Callback(const Sink& sink) : sink_(new Adapter<Sink>(sink)) {}
177a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
178a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void Run(
179a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      typename internal::Callback_ParamTraits<A1>::ForwardType a1,
180a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      typename internal::Callback_ParamTraits<A2>::ForwardType a2,
181a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      typename internal::Callback_ParamTraits<A3>::ForwardType a3) const {
182a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (sink_.get())
183a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      sink_->Run(
184cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a1),
185cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a2),
186cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a3));
187a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
188a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
189116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool is_null() const {
190116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return !sink_.get();
191116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
192116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
193a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) private:
194a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  template <typename Sink>
195a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  struct Adapter : public Runnable {
196a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    explicit Adapter(const Sink& sink) : sink(sink) {}
197a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    virtual void Run(
198a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A1>::ForwardType a1,
199a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A2>::ForwardType a2,
200a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A3>::ForwardType a3) const
201a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            MOJO_OVERRIDE {
202a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      sink.Run(
203cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a1),
204cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a2),
205cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a3));
206a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
207a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    Sink sink;
208a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  };
209a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
210a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  internal::SharedPtr<Runnable> sink_;
211a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)};
212a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
213a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)template <typename A1, typename A2, typename A3, typename A4>
214a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class Callback<void(A1, A2, A3, A4)> {
215a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) public:
216a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  struct Runnable {
217a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    virtual ~Runnable() {}
218a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    virtual void Run(
219a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A1>::ForwardType a1,
220a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A2>::ForwardType a2,
221a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A3>::ForwardType a3,
222a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A4>::ForwardType a4) const = 0;
223a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  };
224a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
225a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Callback() {}
226a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
227a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // The Callback assumes ownership of |runnable|.
228a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  explicit Callback(Runnable* runnable) : sink_(runnable) {}
229a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
230a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Any class that is copy-constructable and has a compatible Run method may
231a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // be adapted to a Callback using this constructor.
232a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  template <typename Sink>
233a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Callback(const Sink& sink) : sink_(new Adapter<Sink>(sink)) {}
234a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
235a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void Run(
236a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      typename internal::Callback_ParamTraits<A1>::ForwardType a1,
237a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      typename internal::Callback_ParamTraits<A2>::ForwardType a2,
238a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      typename internal::Callback_ParamTraits<A3>::ForwardType a3,
239a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      typename internal::Callback_ParamTraits<A4>::ForwardType a4) const {
240a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (sink_.get())
241a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      sink_->Run(
242cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a1),
243cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a2),
244cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a3),
245cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a4));
246a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
247a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
248116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool is_null() const {
249116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return !sink_.get();
250116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
251116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
252a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) private:
253a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  template <typename Sink>
254a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  struct Adapter : public Runnable {
255a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    explicit Adapter(const Sink& sink) : sink(sink) {}
256a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    virtual void Run(
257a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A1>::ForwardType a1,
258a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A2>::ForwardType a2,
259a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A3>::ForwardType a3,
260a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A4>::ForwardType a4) const
261a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            MOJO_OVERRIDE {
262a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      sink.Run(
263cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a1),
264cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a2),
265cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a3),
266cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a4));
267a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
268a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    Sink sink;
269a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  };
270a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
271a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  internal::SharedPtr<Runnable> sink_;
272a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)};
273a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
274a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)template <typename A1, typename A2, typename A3, typename A4, typename A5>
275a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class Callback<void(A1, A2, A3, A4, A5)> {
276a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) public:
277a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  struct Runnable {
278a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    virtual ~Runnable() {}
279a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    virtual void Run(
280a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A1>::ForwardType a1,
281a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A2>::ForwardType a2,
282a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A3>::ForwardType a3,
283a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A4>::ForwardType a4,
284a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A5>::ForwardType a5) const = 0;
285a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  };
286a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
287a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Callback() {}
288a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
289a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // The Callback assumes ownership of |runnable|.
290a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  explicit Callback(Runnable* runnable) : sink_(runnable) {}
291a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
292a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Any class that is copy-constructable and has a compatible Run method may
293a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // be adapted to a Callback using this constructor.
294a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  template <typename Sink>
295a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Callback(const Sink& sink) : sink_(new Adapter<Sink>(sink)) {}
296a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
297a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void Run(
298a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      typename internal::Callback_ParamTraits<A1>::ForwardType a1,
299a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      typename internal::Callback_ParamTraits<A2>::ForwardType a2,
300a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      typename internal::Callback_ParamTraits<A3>::ForwardType a3,
301a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      typename internal::Callback_ParamTraits<A4>::ForwardType a4,
302a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      typename internal::Callback_ParamTraits<A5>::ForwardType a5) const {
303a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (sink_.get())
304a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      sink_->Run(
305cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a1),
306cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a2),
307cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a3),
308cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a4),
309cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a5));
310a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
311a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
312116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool is_null() const {
313116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return !sink_.get();
314116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
315116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
316a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) private:
317a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  template <typename Sink>
318a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  struct Adapter : public Runnable {
319a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    explicit Adapter(const Sink& sink) : sink(sink) {}
320a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    virtual void Run(
321a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A1>::ForwardType a1,
322a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A2>::ForwardType a2,
323a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A3>::ForwardType a3,
324a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A4>::ForwardType a4,
325a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A5>::ForwardType a5) const
326a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            MOJO_OVERRIDE {
327a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      sink.Run(
328cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a1),
329cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a2),
330cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a3),
331cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a4),
332cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a5));
333a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
334a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    Sink sink;
335a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  };
336a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
337a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  internal::SharedPtr<Runnable> sink_;
338a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)};
339a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
340a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)template <typename A1, typename A2, typename A3, typename A4, typename A5,
341a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    typename A6>
342a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class Callback<void(A1, A2, A3, A4, A5, A6)> {
343a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) public:
344a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  struct Runnable {
345a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    virtual ~Runnable() {}
346a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    virtual void Run(
347a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A1>::ForwardType a1,
348a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A2>::ForwardType a2,
349a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A3>::ForwardType a3,
350a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A4>::ForwardType a4,
351a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A5>::ForwardType a5,
352a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A6>::ForwardType a6) const = 0;
353a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  };
354a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
355a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Callback() {}
356a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
357a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // The Callback assumes ownership of |runnable|.
358a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  explicit Callback(Runnable* runnable) : sink_(runnable) {}
359a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
360a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Any class that is copy-constructable and has a compatible Run method may
361a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // be adapted to a Callback using this constructor.
362a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  template <typename Sink>
363a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Callback(const Sink& sink) : sink_(new Adapter<Sink>(sink)) {}
364a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
365a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void Run(
366a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      typename internal::Callback_ParamTraits<A1>::ForwardType a1,
367a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      typename internal::Callback_ParamTraits<A2>::ForwardType a2,
368a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      typename internal::Callback_ParamTraits<A3>::ForwardType a3,
369a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      typename internal::Callback_ParamTraits<A4>::ForwardType a4,
370a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      typename internal::Callback_ParamTraits<A5>::ForwardType a5,
371a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      typename internal::Callback_ParamTraits<A6>::ForwardType a6) const {
372a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (sink_.get())
373a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      sink_->Run(
374cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a1),
375cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a2),
376cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a3),
377cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a4),
378cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a5),
379cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a6));
380a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
381a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
382116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool is_null() const {
383116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return !sink_.get();
384116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
385116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
386a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) private:
387a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  template <typename Sink>
388a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  struct Adapter : public Runnable {
389a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    explicit Adapter(const Sink& sink) : sink(sink) {}
390a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    virtual void Run(
391a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A1>::ForwardType a1,
392a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A2>::ForwardType a2,
393a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A3>::ForwardType a3,
394a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A4>::ForwardType a4,
395a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A5>::ForwardType a5,
396a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A6>::ForwardType a6) const
397a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            MOJO_OVERRIDE {
398a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      sink.Run(
399cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a1),
400cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a2),
401cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a3),
402cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a4),
403cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a5),
404cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a6));
405a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
406a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    Sink sink;
407a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  };
408a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
409a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  internal::SharedPtr<Runnable> sink_;
410a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)};
411a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
412a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)template <typename A1, typename A2, typename A3, typename A4, typename A5,
413a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    typename A6, typename A7>
414a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class Callback<void(A1, A2, A3, A4, A5, A6, A7)> {
415a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) public:
416a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  struct Runnable {
417a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    virtual ~Runnable() {}
418a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    virtual void Run(
419a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A1>::ForwardType a1,
420a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A2>::ForwardType a2,
421a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A3>::ForwardType a3,
422a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A4>::ForwardType a4,
423a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A5>::ForwardType a5,
424a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A6>::ForwardType a6,
425a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A7>::ForwardType a7) const = 0;
426a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  };
427a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
428a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Callback() {}
429a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
430a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // The Callback assumes ownership of |runnable|.
431a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  explicit Callback(Runnable* runnable) : sink_(runnable) {}
432a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
433a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Any class that is copy-constructable and has a compatible Run method may
434a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // be adapted to a Callback using this constructor.
435a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  template <typename Sink>
436a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Callback(const Sink& sink) : sink_(new Adapter<Sink>(sink)) {}
437a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
438a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void Run(
439a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      typename internal::Callback_ParamTraits<A1>::ForwardType a1,
440a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      typename internal::Callback_ParamTraits<A2>::ForwardType a2,
441a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      typename internal::Callback_ParamTraits<A3>::ForwardType a3,
442a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      typename internal::Callback_ParamTraits<A4>::ForwardType a4,
443a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      typename internal::Callback_ParamTraits<A5>::ForwardType a5,
444a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      typename internal::Callback_ParamTraits<A6>::ForwardType a6,
445a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      typename internal::Callback_ParamTraits<A7>::ForwardType a7) const {
446a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (sink_.get())
447a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      sink_->Run(
448cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a1),
449cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a2),
450cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a3),
451cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a4),
452cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a5),
453cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a6),
454cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a7));
455a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
456a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
457116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool is_null() const {
458116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return !sink_.get();
459116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
460116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
461a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) private:
462a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  template <typename Sink>
463a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  struct Adapter : public Runnable {
464a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    explicit Adapter(const Sink& sink) : sink(sink) {}
465a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    virtual void Run(
466a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A1>::ForwardType a1,
467a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A2>::ForwardType a2,
468a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A3>::ForwardType a3,
469a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A4>::ForwardType a4,
470a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A5>::ForwardType a5,
471a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A6>::ForwardType a6,
472a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        typename internal::Callback_ParamTraits<A7>::ForwardType a7) const
473a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            MOJO_OVERRIDE {
474a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      sink.Run(
475cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a1),
476cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a2),
477cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a3),
478cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a4),
479cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a5),
480cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a6),
481cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          internal::Forward(a7));
482a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
483a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    Sink sink;
484a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  };
485a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
486a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  internal::SharedPtr<Runnable> sink_;
487a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)};
488a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
4895f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)typedef Callback<void()> Closure;
4905f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
491a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}  // namespace mojo
492a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
493e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#endif  // MOJO_PUBLIC_CPP_BINDINGS_CALLBACK_H_
494