ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [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 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 5 | #ifndef BASE_BIND_HELPERS_H_ |
| 6 | #define BASE_BIND_HELPERS_H_ |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 7 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
danakj | a7589e7 | 2015-12-08 09:44:41 +0900 | [diff] [blame] | 10 | #include <type_traits> |
| 11 | #include <utility> |
| 12 | |
Peter Kasting | 24efe5e | 2018-02-24 09:03:01 +0900 | [diff] [blame^] | 13 | #include "base/bind.h" |
ajwong@chromium.org | 718dddf | 2011-09-28 09:26:37 +0900 | [diff] [blame] | 14 | #include "base/callback.h" |
ajwong@chromium.org | c711b82 | 2011-05-17 07:35:14 +0900 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 16 | #include "build/build_config.h" |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 17 | |
Peter Kasting | 2ea0236 | 2018-02-15 17:30:23 +0900 | [diff] [blame] | 18 | // This defines a set of simple functions and utilities that people want when |
| 19 | // using Callback<> and Bind(). |
| 20 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 21 | namespace base { |
tzik | c44f1fd | 2016-06-14 22:17:31 +0900 | [diff] [blame] | 22 | |
Peter Kasting | 24efe5e | 2018-02-24 09:03:01 +0900 | [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 | }; |
ajwong@chromium.org | e4f3dc3 | 2012-01-07 07:12:28 +0900 | [diff] [blame] | 45 | |
Peter Kasting | 2ea0236 | 2018-02-15 17:30:23 +0900 | [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. |
ajwong@chromium.org | e4f3dc3 | 2012-01-07 07:12:28 +0900 | [diff] [blame] | 49 | template<typename T> |
| 50 | void DeletePointer(T* obj) { |
| 51 | delete obj; |
| 52 | } |
| 53 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 54 | } // namespace base |
| 55 | |
| 56 | #endif // BASE_BIND_HELPERS_H_ |