1 // Copyright 2017 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 TOOLS_CLANG_BASE_BIND_REWRITERS_TESTS_CALLBACK_H_
6 #define TOOLS_CLANG_BASE_BIND_REWRITERS_TESTS_CALLBACK_H_
7 
8 #include <type_traits>
9 #include <utility>
10 
11 namespace base {
12 namespace internal {
13 
14 template <typename T>
15 class PassedWrapper {
16  public:
PassedWrapper(T && scoper)17   explicit PassedWrapper(T&& scoper) {}
PassedWrapper(PassedWrapper && other)18   PassedWrapper(PassedWrapper&& other) {}
19 };
20 
21 }  // namespace internal
22 
23 template <typename T,
24           std::enable_if_t<!std::is_lvalue_reference<T>::value>* = nullptr>
Passed(T && scoper)25 internal::PassedWrapper<T> Passed(T&& scoper) {
26   return internal::PassedWrapper<T>(std::move(scoper));
27 }
28 
29 template <typename T>
Passed(T * scoper)30 internal::PassedWrapper<T> Passed(T* scoper) {
31   return internal::PassedWrapper<T>(std::move(*scoper));
32 }
33 
34 template <typename Signature>
35 class OnceCallback;
36 
37 template <typename Signature>
38 class RepeatingCallback;
39 
40 template <typename Signature>
41 using Callback = RepeatingCallback<Signature>;
42 
43 using OnceClosure = OnceCallback<void()>;
44 using RepeatingClosure = RepeatingCallback<void()>;
45 using Closure = Callback<void()>;
46 
47 template <typename R, typename... Args>
48 class OnceCallback<R(Args...)> {
49  public:
OnceCallback()50   OnceCallback() {}
51 
52   OnceCallback(OnceCallback&&) = default;
53   OnceCallback& operator=(OnceCallback&&) = default;
54 
55   OnceCallback(const OnceCallback&) = delete;
56   OnceCallback& operator=(const OnceCallback&) = delete;
57 
OnceCallback(RepeatingCallback<R (Args...)> other)58   OnceCallback(RepeatingCallback<R(Args...)> other) {}
59   OnceCallback& operator=(RepeatingCallback<R(Args...)> other) { return *this; }
60 
Run(Args...args)61   R Run(Args... args) const & {
62     static_assert(!sizeof(*this), "");
63     return R();
64   }
Run(Args...args)65   R Run(Args... args) && { return R(); }
66 };
67 
68 template <typename R, typename... Args>
69 class RepeatingCallback<R(Args...)> {
70  public:
RepeatingCallback()71   RepeatingCallback() {}
72 
73   RepeatingCallback(const RepeatingCallback&) = default;
74   RepeatingCallback& operator=(const RepeatingCallback&) = default;
75 
76   RepeatingCallback(RepeatingCallback&&) = default;
77   RepeatingCallback& operator=(RepeatingCallback&&) = default;
78 
Run(Args...args)79   R Run(Args... args) const & { return R(); }
Run(Args...args)80   R Run(Args... args) && { return R(); }
81 };
82 
83 template <typename Functor, typename... Args>
Bind(Functor,Args &&...)84 Callback<void()> Bind(Functor, Args&&...) {
85   return Callback<void()>();
86 }
87 
88 template <typename Functor, typename... Args>
BindOnce(Functor,Args &&...)89 OnceCallback<void()> BindOnce(Functor, Args&&...) {
90   return OnceCallback<void()>();
91 }
92 
93 template <typename Functor, typename... Args>
BindRepeating(Functor,Args &&...)94 RepeatingCallback<void()> BindRepeating(Functor, Args&&...) {
95   return RepeatingCallback<void()>();
96 }
97 
AdaptCallbackForRepeating(OnceCallback<void ()> callback)98 RepeatingCallback<void()> AdaptCallbackForRepeating(
99     OnceCallback<void()> callback) {
100   return Callback<void()>();
101 }
102 
103 }  // namespace base
104 
105 #endif  // TOOLS_CLANG_BASE_BIND_REWRITERS_TESTS_CALLBACK_H_
106