• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1$$ This is a pump file for generating file templates.  Pump is a python
2$$ script that is part of the Google Test suite of utilities.  Description
3$$ can be found here:
4$$
5$$ http://code.google.com/p/googletest/wiki/PumpManual
6$$
7
8$var MAX_ARITY = 7
9
10// Copyright 2014 The Chromium Authors. All rights reserved.
11// Use of this source code is governed by a BSD-style license that can be
12// found in the LICENSE file.
13
14#ifndef MOJO_PUBLIC_CPP_BINDINGS_CALLBACK_H_
15#define MOJO_PUBLIC_CPP_BINDINGS_CALLBACK_H_
16
17#include "mojo/public/cpp/bindings/lib/callback_internal.h"
18#include "mojo/public/cpp/bindings/lib/shared_ptr.h"
19#include "mojo/public/cpp/bindings/lib/template_util.h"
20
21namespace mojo {
22
23template <typename Sig>
24class Callback;
25
26$range ARITY 0..MAX_ARITY
27$for ARITY [[
28$range ARG 1..ARITY
29
30template <$for ARG , [[typename A$(ARG)]]>
31class Callback<void($for ARG , [[A$(ARG)]])> {
32 public:
33  struct Runnable {
34    virtual ~Runnable() {}
35    virtual void Run($if ARITY > 0 [[
36
37        ]]$for ARG ,
38        [[typename internal::Callback_ParamTraits<A$(ARG)>::ForwardType a$(ARG)]]) const = 0;
39  };
40
41  Callback() {}
42
43  // The Callback assumes ownership of |runnable|.
44  explicit Callback(Runnable* runnable) : sink_(runnable) {}
45
46  // Any class that is copy-constructable and has a compatible Run method may
47  // be adapted to a Callback using this constructor.
48  template <typename Sink>
49  Callback(const Sink& sink) : sink_(new Adapter<Sink>(sink)) {}
50
51  void Run($if ARITY > 1 [[
52
53      ]]$for ARG ,
54      [[typename internal::Callback_ParamTraits<A$(ARG)>::ForwardType a$(ARG)]]) const {
55    if (sink_.get())
56      sink_->Run($if ARITY > 1 [[
57
58          ]]$for ARG ,
59          [[internal::Forward(a$(ARG))]]);
60  }
61
62  bool is_null() const {
63    return !sink_.get();
64  }
65
66 private:
67  template <typename Sink>
68  struct Adapter : public Runnable {
69    explicit Adapter(const Sink& sink) : sink(sink) {}
70    virtual void Run($if ARITY > 0 [[
71
72        ]]$for ARG ,
73        [[typename internal::Callback_ParamTraits<A$(ARG)>::ForwardType a$(ARG)]]) const MOJO_OVERRIDE {
74      sink.Run($if ARITY > 1 [[
75
76          ]]$for ARG ,
77          [[internal::Forward(a$(ARG))]]);
78    }
79    Sink sink;
80  };
81
82  internal::SharedPtr<Runnable> sink_;
83};
84
85]]  $$ for ARITY
86
87typedef Callback<void()> Closure;
88
89}  // namespace mojo
90
91#endif  // MOJO_PUBLIC_CPP_BINDINGS_CALLBACK_H_
92