OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_CALLBACK_H_ | 5 #ifndef BASE_CALLBACK_H_ |
6 #define BASE_CALLBACK_H_ | 6 #define BASE_CALLBACK_H_ |
7 | 7 |
8 #include "base/callback_forward.h" | 8 #include "base/callback_forward.h" |
9 #include "base/callback_internal.h" | 9 #include "base/callback_internal.h" |
10 | 10 |
(...skipping 23 matching lines...) Expand all Loading... |
34 | 34 |
35 // Specialization for OnceCallback. | 35 // Specialization for OnceCallback. |
36 template <typename R, typename... Args> | 36 template <typename R, typename... Args> |
37 class RunMixin<OnceCallback<R(Args...)>> { | 37 class RunMixin<OnceCallback<R(Args...)>> { |
38 private: | 38 private: |
39 using CallbackType = OnceCallback<R(Args...)>; | 39 using CallbackType = OnceCallback<R(Args...)>; |
40 | 40 |
41 public: | 41 public: |
42 using PolymorphicInvoke = R(*)(internal::BindStateBase*, Args&&...); | 42 using PolymorphicInvoke = R(*)(internal::BindStateBase*, Args&&...); |
43 | 43 |
44 R Run(Args... args) & { | 44 R Run(Args... args) const & { |
45 // Note: even though this static_assert will trivially always fail, it | 45 // Note: even though this static_assert will trivially always fail, it |
46 // cannot be simply replaced with static_assert(false, ...) because: | 46 // cannot be simply replaced with static_assert(false, ...) because: |
47 // - Per [dcl.dcl]/p4, a program is ill-formed if the constant-expression | 47 // - Per [dcl.dcl]/p4, a program is ill-formed if the constant-expression |
48 // argument does not evaluate to true. | 48 // argument does not evaluate to true. |
49 // - Per [temp.res]/p8, if no valid specialization can be generated for a | 49 // - Per [temp.res]/p8, if no valid specialization can be generated for a |
50 // template definition, and that template is not instantiated, the | 50 // template definition, and that template is not instantiated, the |
51 // template definition is ill-formed, no diagnostic required. | 51 // template definition is ill-formed, no diagnostic required. |
52 // These two clauses, taken together, would allow a conforming C++ compiler | 52 // These two clauses, taken together, would allow a conforming C++ compiler |
53 // to immediately reject static_assert(false, ...), even inside an | 53 // to immediately reject static_assert(false, ...), even inside an |
54 // uninstantiated template. | 54 // uninstantiated template. |
55 static_assert(!IsOnceCallback<CallbackType>::value, | 55 static_assert(!IsOnceCallback<CallbackType>::value, |
56 "OnceCallback::Run() may only be invoked on an rvalue, i.e. " | 56 "OnceCallback::Run() may only be invoked on a non-const " |
57 "std::move(callback).Run()."); | 57 "rvalue, i.e. std::move(callback).Run()."); |
58 } | 58 } |
59 | 59 |
60 R Run(Args... args) && { | 60 R Run(Args... args) && { |
61 // Move the callback instance into a local variable before the invocation, | 61 // Move the callback instance into a local variable before the invocation, |
62 // that ensures the internal state is cleared after the invocation. | 62 // that ensures the internal state is cleared after the invocation. |
63 // It's not safe to touch |this| after the invocation, since running the | 63 // It's not safe to touch |this| after the invocation, since running the |
64 // bound function may destroy |this|. | 64 // bound function may destroy |this|. |
65 CallbackType cb = static_cast<CallbackType&&>(*this); | 65 CallbackType cb = static_cast<CallbackType&&>(*this); |
66 PolymorphicInvoke f = | 66 PolymorphicInvoke f = |
67 reinterpret_cast<PolymorphicInvoke>(cb.polymorphic_invoke()); | 67 reinterpret_cast<PolymorphicInvoke>(cb.polymorphic_invoke()); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 bool Equals(const Callback& other) const { | 134 bool Equals(const Callback& other) const { |
135 return this->EqualsInternal(other); | 135 return this->EqualsInternal(other); |
136 } | 136 } |
137 | 137 |
138 friend class internal::RunMixin<Callback>; | 138 friend class internal::RunMixin<Callback>; |
139 }; | 139 }; |
140 | 140 |
141 } // namespace base | 141 } // namespace base |
142 | 142 |
143 #endif // BASE_CALLBACK_H_ | 143 #endif // BASE_CALLBACK_H_ |
OLD | NEW |