1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_BIND_HELPERS_H_
6 #define BASE_BIND_HELPERS_H_
10 #include <type_traits>
13 #include "base/bind.h"
14 #include "base/callback.h"
15 #include "base/memory/weak_ptr.h"
16 #include "build/build_config.h"
18 // This defines a set of simple functions and utilities that people want when
19 // using Callback<> and Bind().
23 // Creates a null callback.
24 class BASE_EXPORT NullCallback {
26 template <typename R, typename... Args>
27 operator RepeatingCallback<R(Args...)>() const {
28 return RepeatingCallback<R(Args...)>();
30 template <typename R, typename... Args>
31 operator OnceCallback<R(Args...)>() const {
32 return OnceCallback<R(Args...)>();
36 // Creates a callback that does nothing when called.
37 class BASE_EXPORT DoNothing {
39 template <typename... Args>
40 operator RepeatingCallback<void(Args...)>() const {
41 return Repeatedly<Args...>();
43 template <typename... Args>
44 operator OnceCallback<void(Args...)>() const {
45 return Once<Args...>();
47 // Explicit way of specifying a specific callback type when the compiler can't
49 template <typename... Args>
50 static RepeatingCallback<void(Args...)> Repeatedly() {
51 return BindRepeating([](Args... args) {});
53 template <typename... Args>
54 static OnceCallback<void(Args...)> Once() {
55 return BindOnce([](Args... args) {});
59 // Useful for creating a Closure that will delete a pointer when invoked. Only
60 // use this when necessary. In most cases MessageLoop::DeleteSoon() is a better
63 void DeletePointer(T* obj) {
69 #endif // BASE_BIND_HELPERS_H_