• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
5 #ifndef BASE_BIND_HELPERS_H_
6 #define BASE_BIND_HELPERS_H_
7 
8 #include <stddef.h>
9 
10 #include <type_traits>
11 #include <utility>
12 
13 #include "base/bind.h"
14 #include "base/callback.h"
15 #include "base/memory/weak_ptr.h"
16 #include "build/build_config.h"
17 
18 // This defines a set of simple functions and utilities that people want when
19 // using Callback<> and Bind().
20 
21 namespace base {
22 
23 // Creates a null callback.
24 class BASE_EXPORT NullCallback {
25  public:
26   template <typename R, typename... Args>
27   operator RepeatingCallback<R(Args...)>() const {
28     return RepeatingCallback<R(Args...)>();
29   }
30   template <typename R, typename... Args>
31   operator OnceCallback<R(Args...)>() const {
32     return OnceCallback<R(Args...)>();
33   }
34 };
35 
36 // Creates a callback that does nothing when called.
37 class BASE_EXPORT DoNothing {
38  public:
39   template <typename... Args>
40   operator RepeatingCallback<void(Args...)>() const {
41     return Repeatedly<Args...>();
42   }
43   template <typename... Args>
44   operator OnceCallback<void(Args...)>() const {
45     return Once<Args...>();
46   }
47   // Explicit way of specifying a specific callback type when the compiler can't
48   // deduce it.
49   template <typename... Args>
Repeatedly()50   static RepeatingCallback<void(Args...)> Repeatedly() {
51     return BindRepeating([](Args... args) {});
52   }
53   template <typename... Args>
Once()54   static OnceCallback<void(Args...)> Once() {
55     return BindOnce([](Args... args) {});
56   }
57 };
58 
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
61 // fit.
62 template <typename T>
DeletePointer(T * obj)63 void DeletePointer(T* obj) {
64   delete obj;
65 }
66 
67 }  // namespace base
68 
69 #endif  // BASE_BIND_HELPERS_H_
70