blob: fca3939a6e54921b9e20d50cf66b58f5b27df15e [file] [log] [blame]
ajwong@chromium.orge2cca632011-02-15 10:27:38 +09001// 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.
4
ajwong@chromium.orge2cca632011-02-15 10:27:38 +09005#ifndef BASE_BIND_HELPERS_H_
6#define BASE_BIND_HELPERS_H_
ajwong@chromium.orge2cca632011-02-15 10:27:38 +09007
avia6a6a682015-12-27 07:15:14 +09008#include <stddef.h>
9
danakja7589e72015-12-08 09:44:41 +090010#include <type_traits>
11#include <utility>
12
Peter Kasting24efe5e2018-02-24 09:03:01 +090013#include "base/bind.h"
ajwong@chromium.org718dddf2011-09-28 09:26:37 +090014#include "base/callback.h"
ajwong@chromium.orgc711b822011-05-17 07:35:14 +090015#include "base/memory/weak_ptr.h"
avia6a6a682015-12-27 07:15:14 +090016#include "build/build_config.h"
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090017
Peter Kasting2ea02362018-02-15 17:30:23 +090018// This defines a set of simple functions and utilities that people want when
19// using Callback<> and Bind().
20
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090021namespace base {
tzikc44f1fd2016-06-14 22:17:31 +090022
Peter Kasting24efe5e2018-02-24 09:03:01 +090023// Creates a callback that does nothing when called.
24class BASE_EXPORT DoNothing {
25 public:
26 template <typename... Args>
27 operator RepeatingCallback<void(Args...)>() const {
28 return Repeatedly<Args...>();
29 }
30 template <typename... Args>
31 operator OnceCallback<void(Args...)>() const {
32 return Once<Args...>();
33 }
34 // Explicit way of specifying a specific callback type when the compiler can't
35 // deduce it.
36 template <typename... Args>
37 static RepeatingCallback<void(Args...)> Repeatedly() {
38 return BindRepeating([](Args... args) {});
39 }
40 template <typename... Args>
41 static OnceCallback<void(Args...)> Once() {
42 return BindOnce([](Args... args) {});
43 }
44};
ajwong@chromium.orge4f3dc32012-01-07 07:12:28 +090045
Peter Kasting2ea02362018-02-15 17:30:23 +090046// Useful for creating a Closure that will delete a pointer when invoked. Only
47// use this when necessary. In most cases MessageLoop::DeleteSoon() is a better
48// fit.
ajwong@chromium.orge4f3dc32012-01-07 07:12:28 +090049template<typename T>
50void DeletePointer(T* obj) {
51 delete obj;
52}
53
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090054} // namespace base
55
56#endif // BASE_BIND_HELPERS_H_