summaryrefslogtreecommitdiffstats
path: root/mojo/public/cpp/bindings/callback.h.pump
blob: 5452abb2c967d2d0225b6073785bb5b6c830d0ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
$$ This is a pump file for generating file templates.  Pump is a python
$$ script that is part of the Google Test suite of utilities.  Description
$$ can be found here:
$$
$$ http://code.google.com/p/googletest/wiki/PumpManual
$$

$var MAX_ARITY = 7

// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MOJO_PUBLIC_CPP_BINDINGS_CALLBACK_H_
#define MOJO_PUBLIC_CPP_BINDINGS_CALLBACK_H_

#include "mojo/public/cpp/bindings/lib/callback_internal.h"
#include "mojo/public/cpp/bindings/lib/shared_ptr.h"

namespace mojo {

template <typename Sig>
class Callback;

$range ARITY 0..MAX_ARITY
$for ARITY [[
$range ARG 1..ARITY

template <$for ARG , [[typename A$(ARG)]]>
class Callback<void($for ARG , [[A$(ARG)]])> {
 public:
  struct Runnable {
    virtual ~Runnable() {}
    virtual void Run(
        $for ARG ,
        [[typename internal::Callback_ParamTraits<A$(ARG)>::ForwardType a$(ARG)]]) const = 0;
  };

  Callback() {}

  // The Callback assumes ownership of |runnable|.
  explicit Callback(Runnable* runnable) : sink_(runnable) {}

  // Any class that is copy-constructable and has a compatible Run method may
  // be adapted to a Callback using this constructor.
  template <typename Sink>
  Callback(const Sink& sink) : sink_(new Adapter<Sink>(sink)) {}

  void Run(
      $for ARG ,
      [[typename internal::Callback_ParamTraits<A$(ARG)>::ForwardType a$(ARG)]]) const {
    if (sink_.get())
      sink_->Run(
          $for ARG ,
          [[internal::Callback_Forward(a$(ARG))]]);
  }

 private:
  template <typename Sink>
  struct Adapter : public Runnable {
    explicit Adapter(const Sink& sink) : sink(sink) {}
    virtual void Run(
        $for ARG ,
        [[typename internal::Callback_ParamTraits<A$(ARG)>::ForwardType a$(ARG)]]) const MOJO_OVERRIDE {
      sink.Run(
          $for ARG ,
          [[internal::Callback_Forward(a$(ARG))]]);
    }
    Sink sink;
  };

  internal::SharedPtr<Runnable> sink_;
};

]]  $$ for ARITY

}  // namespace mojo

#endif  // MOJO_PUBLIC_CPP_BINDINGS_CALLBACK_H_