/src/perfetto/src/ipc/deferred.cc
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2017 The Android Open Source Project |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | * you may not use this file except in compliance with the License. |
6 | | * You may obtain a copy of the License at |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | * See the License for the specific language governing permissions and |
14 | | * limitations under the License. |
15 | | */ |
16 | | |
17 | | #include "perfetto/ext/ipc/deferred.h" |
18 | | |
19 | | #include "perfetto/base/logging.h" |
20 | | #include "perfetto/ext/base/utils.h" |
21 | | |
22 | | namespace perfetto { |
23 | | namespace ipc { |
24 | | |
25 | | DeferredBase::DeferredBase( |
26 | | std::function<void(AsyncResult<ProtoMessage>)> callback) |
27 | 402k | : callback_(std::move(callback)) {} |
28 | | |
29 | 803k | DeferredBase::~DeferredBase() { |
30 | 803k | if (callback_) |
31 | 197k | Reject(); |
32 | 803k | } |
33 | | |
34 | | // Can't just use "= default" here because the default move operator for |
35 | | // std::function doesn't necessarily swap and hence can leave a copy of the |
36 | | // bind state around, which is undesirable. |
37 | 401k | DeferredBase::DeferredBase(DeferredBase&& other) noexcept { |
38 | 401k | Move(other); |
39 | 401k | } |
40 | | |
41 | 568 | DeferredBase& DeferredBase::operator=(DeferredBase&& other) { |
42 | 568 | if (callback_) |
43 | 0 | Reject(); |
44 | 568 | Move(other); |
45 | 568 | return *this; |
46 | 568 | } |
47 | | |
48 | 402k | void DeferredBase::Move(DeferredBase& other) { |
49 | 402k | callback_ = std::move(other.callback_); |
50 | 402k | other.callback_ = nullptr; |
51 | 402k | } |
52 | | |
53 | | void DeferredBase::Bind( |
54 | 198k | std::function<void(AsyncResult<ProtoMessage>)> callback) { |
55 | 198k | callback_ = std::move(callback); |
56 | 198k | } |
57 | | |
58 | 4.97k | bool DeferredBase::IsBound() const { |
59 | 4.97k | return !!callback_; |
60 | 4.97k | } |
61 | | |
62 | 200k | void DeferredBase::Resolve(AsyncResult<ProtoMessage> async_result) { |
63 | 200k | if (!callback_) { |
64 | 1 | PERFETTO_DFATAL("No callback set."); |
65 | 1 | return; |
66 | 1 | } |
67 | 200k | bool has_more = async_result.has_more(); |
68 | 200k | callback_(std::move(async_result)); |
69 | 200k | if (!has_more) |
70 | 198k | callback_ = nullptr; |
71 | 200k | } |
72 | | |
73 | | // Resolves with a nullptr |msg_|, signalling failure to |callback_|. |
74 | 197k | void DeferredBase::Reject() { |
75 | 197k | Resolve(AsyncResult<ProtoMessage>()); |
76 | 197k | } |
77 | | |
78 | | } // namespace ipc |
79 | | } // namespace perfetto |