[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 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. | ||||
4 | |||||
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 5 | #ifndef BASE_BIND_HELPERS_H_ |
6 | #define BASE_BIND_HELPERS_H_ | ||||
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 7 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 8 | #include <stddef.h> |
9 | |||||
danakj | 314d1f4 | 2015-12-08 00:44:41 | [diff] [blame] | 10 | #include <type_traits> |
11 | #include <utility> | ||||
12 | |||||
Peter Kasting | 341e1fb | 2018-02-24 00:03:01 | [diff] [blame] | 13 | #include "base/bind.h" |
[email protected] | e8bfc31d | 2011-09-28 00:26:37 | [diff] [blame] | 14 | #include "base/callback.h" |
[email protected] | 9354058 | 2011-05-16 22:35:14 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 16 | #include "build/build_config.h" |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 17 | |
Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 18 | // This defines a set of simple functions and utilities that people want when |
19 | // using Callback<> and Bind(). | ||||
20 | |||||
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 21 | namespace base { |
tzik | 1ae80b2 | 2016-06-14 13:17:31 | [diff] [blame] | 22 | |
Peter Kasting | 341e1fb | 2018-02-24 00:03:01 | [diff] [blame] | 23 | // Creates a callback that does nothing when called. |
24 | class 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] | c694427 | 2012-01-06 22:12:28 | [diff] [blame] | 45 | |
Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 46 | // 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] | c694427 | 2012-01-06 22:12:28 | [diff] [blame] | 49 | template<typename T> |
50 | void DeletePointer(T* obj) { | ||||
51 | delete obj; | ||||
52 | } | ||||
53 | |||||
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 54 | } // namespace base |
55 | |||||
56 | #endif // BASE_BIND_HELPERS_H_ |