blob: d2a6228c46eea5f71a5481585677c93e48620123 [file] [log] [blame]
[email protected]ac4c6682012-01-04 00:57:391// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]2041cf342010-02-19 03:15:592// 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_CALLBACK_H_
6#define BASE_CALLBACK_H_
7
[email protected]d7f75242011-12-07 21:44:068#include "base/callback_forward.h"
[email protected]59eff912011-02-18 23:29:319#include "base/callback_internal.h"
[email protected]2041cf342010-02-19 03:15:5910
[email protected]d7f75242011-12-07 21:44:0611// NOTE: Header files that do not require the full definition of Callback or
12// Closure should #include "base/callback_forward.h" instead of this file.
13
[email protected]24292642012-07-12 20:06:4014// -----------------------------------------------------------------------------
tzik703f1562016-09-02 07:36:5515// Usage documentation
[email protected]24292642012-07-12 20:06:4016// -----------------------------------------------------------------------------
[email protected]2041cf342010-02-19 03:15:5917//
tzik703f1562016-09-02 07:36:5518// See //docs/callback.md for documentation.
[email protected]4346ef912011-02-19 00:52:1519
tzik77d411392016-03-09 09:47:0320namespace base {
[email protected]e24f8762011-12-20 00:10:0421
tzik27d1e312016-09-13 05:28:5922namespace internal {
tzikcaf1d84b2016-06-28 12:22:2123
tzik27d1e312016-09-13 05:28:5924template <typename From, typename To>
25struct IsCallbackConvertible : std::false_type {};
26
27template <typename Signature>
dcheng77172b92016-11-22 07:46:0628struct IsCallbackConvertible<RepeatingCallback<Signature>,
29 OnceCallback<Signature>> : std::true_type {};
tzik27d1e312016-09-13 05:28:5930
31} // namespace internal
32
33template <typename R,
34 typename... Args,
35 internal::CopyMode copy_mode,
36 internal::RepeatMode repeat_mode>
37class Callback<R(Args...), copy_mode, repeat_mode>
tzikecb1b242017-03-21 07:25:5438 : public internal::CallbackBase<copy_mode> {
tzik27d1e312016-09-13 05:28:5939 public:
40 static_assert(repeat_mode != internal::RepeatMode::Once ||
41 copy_mode == internal::CopyMode::MoveOnly,
42 "OnceCallback must be MoveOnly.");
43
44 using RunType = R(Args...);
tzikecb1b242017-03-21 07:25:5445 using PolymorphicInvoke = R (*)(internal::BindStateBase*, Args&&...);
[email protected]b38d3572011-02-15 01:27:3846
tzik77d411392016-03-09 09:47:0347 Callback() : internal::CallbackBase<copy_mode>(nullptr) {}
[email protected]b38d3572011-02-15 01:27:3848
tzik1886c272016-09-08 05:45:3849 explicit Callback(internal::BindStateBase* bind_state)
tzik77d411392016-03-09 09:47:0350 : internal::CallbackBase<copy_mode>(bind_state) {
[email protected]2041cf342010-02-19 03:15:5951 }
52
Jeremy Roman35a317432017-08-16 22:20:5353 template <
54 typename OtherCallback,
55 typename = std::enable_if_t<
56 internal::IsCallbackConvertible<OtherCallback, Callback>::value>>
tzik27d1e312016-09-13 05:28:5957 Callback(OtherCallback other)
58 : internal::CallbackBase<copy_mode>(std::move(other)) {}
59
Jeremy Roman35a317432017-08-16 22:20:5360 template <
61 typename OtherCallback,
62 typename = std::enable_if_t<
63 internal::IsCallbackConvertible<OtherCallback, Callback>::value>>
tzik27d1e312016-09-13 05:28:5964 Callback& operator=(OtherCallback other) {
65 static_cast<internal::CallbackBase<copy_mode>&>(*this) = std::move(other);
66 return *this;
67 }
68
[email protected]481915a772011-09-10 03:14:3569 bool Equals(const Callback& other) const {
tzik77d411392016-03-09 09:47:0370 return this->EqualsInternal(other);
[email protected]481915a772011-09-10 03:14:3571 }
72
tzikecb1b242017-03-21 07:25:5473 R Run(Args... args) const & {
74 static_assert(repeat_mode == internal::RepeatMode::Repeating,
75 "OnceCallback::Run() may only be invoked on a non-const "
76 "rvalue, i.e. std::move(callback).Run().");
77
78 PolymorphicInvoke f =
79 reinterpret_cast<PolymorphicInvoke>(this->polymorphic_invoke());
80 return f(this->bind_state_.get(), std::forward<Args>(args)...);
81 }
82
83 R Run(Args... args) && {
84 // Move the callback instance into a local variable before the invocation,
85 // that ensures the internal state is cleared after the invocation.
86 // It's not safe to touch |this| after the invocation, since running the
87 // bound function may destroy |this|.
88 Callback cb = std::move(*this);
89 PolymorphicInvoke f =
90 reinterpret_cast<PolymorphicInvoke>(cb.polymorphic_invoke());
91 return f(cb.bind_state_.get(), std::forward<Args>(args)...);
92 }
[email protected]2041cf342010-02-19 03:15:5993};
94
[email protected]b38d3572011-02-15 01:27:3895} // namespace base
[email protected]2041cf342010-02-19 03:15:5996
tzikc87149e2014-11-20 01:08:2097#endif // BASE_CALLBACK_H_