blob: 856e85b558673c93dc8b3ac72a00f906a1257deb [file] [log] [blame]
$$ 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"
#include "mojo/public/cpp/bindings/lib/template_util.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($if ARITY > 0 [[
]]$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($if ARITY > 1 [[
]]$for ARG ,
[[typename internal::Callback_ParamTraits<A$(ARG)>::ForwardType a$(ARG)]]) const {
if (sink_.get())
sink_->Run($if ARITY > 1 [[
]]$for ARG ,
[[internal::Forward(a$(ARG))]]);
}
bool is_null() const {
return !sink_.get();
}
void reset() {
sink_.reset();
}
private:
template <typename Sink>
struct Adapter : public Runnable {
explicit Adapter(const Sink& sink) : sink(sink) {}
virtual void Run($if ARITY > 0 [[
]]$for ARG ,
[[typename internal::Callback_ParamTraits<A$(ARG)>::ForwardType a$(ARG)]]) const override {
sink.Run($if ARITY > 1 [[
]]$for ARG ,
[[internal::Forward(a$(ARG))]]);
}
Sink sink;
};
internal::SharedPtr<Runnable> sink_;
};
]] $$ for ARITY
typedef Callback<void()> Closure;
} // namespace mojo
#endif // MOJO_PUBLIC_CPP_BINDINGS_CALLBACK_H_