blob: fca3939a6e54921b9e20d50cf66b58f5b27df15e [file] [log] [blame]
[email protected]b38d3572011-02-15 01:27:381// 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
[email protected]b38d3572011-02-15 01:27:385#ifndef BASE_BIND_HELPERS_H_
6#define BASE_BIND_HELPERS_H_
[email protected]b38d3572011-02-15 01:27:387
avi9b6f42932015-12-26 22:15:148#include <stddef.h>
9
danakj314d1f42015-12-08 00:44:4110#include <type_traits>
11#include <utility>
12
Peter Kasting341e1fb2018-02-24 00:03:0113#include "base/bind.h"
[email protected]e8bfc31d2011-09-28 00:26:3714#include "base/callback.h"
[email protected]93540582011-05-16 22:35:1415#include "base/memory/weak_ptr.h"
avi9b6f42932015-12-26 22:15:1416#include "build/build_config.h"
[email protected]b38d3572011-02-15 01:27:3817
Peter Kastinga85265e32018-02-15 08:30:2318// This defines a set of simple functions and utilities that people want when
19// using Callback<> and Bind().
20
[email protected]b38d3572011-02-15 01:27:3821namespace base {
tzik1ae80b22016-06-14 13:17:3122
Peter Kasting341e1fb2018-02-24 00:03:0123// 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};
[email protected]c6944272012-01-06 22:12:2845
Peter Kastinga85265e32018-02-15 08:30:2346// 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.
[email protected]c6944272012-01-06 22:12:2849template<typename T>
50void DeletePointer(T* obj) {
51 delete obj;
52}
53
[email protected]b38d3572011-02-15 01:27:3854} // namespace base
55
56#endif // BASE_BIND_HELPERS_H_