xref: /aosp_15_r20/external/cronet/base/test/rectify_callback.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2022 The Chromium Authors
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_TEST_RECTIFY_CALLBACK_H_
6 #define BASE_TEST_RECTIFY_CALLBACK_H_
7 
8 #include <utility>
9 
10 #include "base/test/rectify_callback_internal.h"
11 
12 namespace base {
13 
14 // RectifyCallback:
15 //
16 //     CallbackType<DesiredSignature> RectifyCallback<DesiredSignature>(
17 //         CallbackType<ActualSignature> callback)
18 //
19 //     DesiredCallbackType RectifyCallback<DesiredCallbackType>(
20 //         ActualCallbackType callback)
21 //
22 // Rectifies the signature of `callback` with `DesiredSignature` or
23 // `DesiredCallbackType` by ignoring the first N arguments of the desired
24 // callback type. Useful when binding callbacks with lots of arguments you don't
25 // actually care about.
26 //
27 // For now, `ActualSignature` and `DesiredSignature` must have the same return
28 // type, and the common arguments between the two must match.
29 //
30 // Example:
31 //
32 //    using CbType = OnceCallback<bool(A, B, C)>;
33 //    void Fn(CbType);
34 //
35 //    // These all ignore arguments when passing the callback:
36 //    Fn(RectifyCallback<CbType>(BindOnce([](){ return true; })));
37 //    Fn(RectifyCallback<CbType>(BindOnce([](C c){ return true; })));
38 //    Fn(RectifyCallback<CbType>(BindOnce([](B c, C c){ return true; })));
39 //
40 //    // This also works, though it makes no change to the input callback:
41 //    Fn(RectifyCallback<CbType>(
42 //        BindOnce([](A a, B c, C c){ return true; })));
43 //
44 // You can also make RectifyCallback implicit by embedding it in a template
45 // version of your function.
46 //
47 //    template <typename T>
48 //    void Fn(T&& t) { FnImpl(RectifyCallback<CbType>(std::forward<T>(t))); }
49 //
50 template <typename Desired, typename Actual>
RectifyCallback(Actual && callback)51 auto RectifyCallback(Actual&& callback) {
52   using Impl = internal::RectifyCallbackImpl<Desired, std::decay_t<Actual>>;
53   return Impl::Rectify(std::move(callback));
54 }
55 
56 }  // namespace base
57 
58 #endif  // BASE_TEST_RECTIFY_CALLBACK_H_
59