summaryrefslogtreecommitdiffstats
path: root/mojo
diff options
context:
space:
mode:
authoramistry <amistry@chromium.org>2016-03-23 15:26:37 -0700
committerCommit bot <commit-bot@chromium.org>2016-03-23 22:29:39 +0000
commit5b55f8a878c389f61a434c48a7b75b838124b7e4 (patch)
tree17fd84e2dc551d37036163c3a727a1e612caacd7 /mojo
parent2b42dcd3cdcdc63eb695a0e94a79e3cc0f0580e4 (diff)
downloadchromium_src-5b55f8a878c389f61a434c48a7b75b838124b7e4.zip
chromium_src-5b55f8a878c389f61a434c48a7b75b838124b7e4.tar.gz
chromium_src-5b55f8a878c389f61a434c48a7b75b838124b7e4.tar.bz2
[mojo-sdk] Disable the RunnableAdapter constructor if the sink is a Runnable*.
This allows the user to pass a Runnable* to the Callback's constructor without explicitly down casting it to a Runnable*. BUG=None Review URL: https://codereview.chromium.org/1813383003 Cr-Commit-Position: refs/heads/master@{#382946}
Diffstat (limited to 'mojo')
-rw-r--r--mojo/public/cpp/bindings/callback.h19
1 files changed, 18 insertions, 1 deletions
diff --git a/mojo/public/cpp/bindings/callback.h b/mojo/public/cpp/bindings/callback.h
index 60dea1c..4e9a933 100644
--- a/mojo/public/cpp/bindings/callback.h
+++ b/mojo/public/cpp/bindings/callback.h
@@ -5,6 +5,7 @@
#ifndef MOJO_PUBLIC_CPP_BINDINGS_CALLBACK_H_
#define MOJO_PUBLIC_CPP_BINDINGS_CALLBACK_H_
+#include <type_traits>
#include <utility>
#include "base/memory/ref_counted.h"
@@ -46,7 +47,23 @@ class Callback<void(Args...)> {
// As above, but can take an object that isn't derived from Runnable, so long
// as it has a compatible operator() or Run() method. operator() will be
// preferred if the type has both.
- template <typename Sink>
+ //
+ // The std::enable_if is used to disable this constructor if the argument is
+ // derived from Runnable. This is needed because the compiler will pick this
+ // constructor instead of the Runnable* one above when the argument is of the
+ // type of the derived class instead of down casted to a Runnable. i.e:
+ // class Foo : public Callback::Runnable {
+ // ...
+ // };
+ // Callback cb(new Foo);
+ //
+ // The call site can fix this by using a static_cast to down cast to a
+ // Runnable*, but that shouldn't be necessary.
+ template <
+ typename Sink,
+ typename std::enable_if<!std::is_base_of<
+ Runnable,
+ typename std::remove_pointer<Sink>::type>::value>::type* = nullptr>
Callback(const Sink& sink) {
using sink_type = typename internal::Conditional<
internal::HasCompatibleCallOperator<Sink, Args...>::value,