blob: e44a1fc83b81bac13b99b75cb07a70d5bad79128 [file] [log] [blame]
Primiano Tucci2a29ac72017-10-24 17:47:19 +01001/*
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 "protozero/protozero_message_handle.h"
18
19#include <utility>
20
Primiano Tuccid7d1be02017-10-30 17:41:34 +000021#include "base/logging.h"
Primiano Tucci2a29ac72017-10-24 17:47:19 +010022#include "protozero/protozero_message.h"
23
24namespace protozero {
25
Primiano Tucci2a29ac72017-10-24 17:47:19 +010026ProtoZeroMessageHandleBase::ProtoZeroMessageHandleBase(
27 ProtoZeroMessage* message)
28 : message_(message) {
29#if PROTOZERO_ENABLE_HANDLE_DEBUGGING()
Hector Dearman8d8ccd32017-11-27 16:06:34 +000030 if (message_)
31 message_->set_handle(this);
Primiano Tucci2a29ac72017-10-24 17:47:19 +010032#endif
33}
34
35ProtoZeroMessageHandleBase::~ProtoZeroMessageHandleBase() {
Primiano Tucci25aff4e2017-11-23 12:18:52 +000036 Finalize();
Primiano Tucci2a29ac72017-10-24 17:47:19 +010037}
38
39ProtoZeroMessageHandleBase::ProtoZeroMessageHandleBase(
40 ProtoZeroMessageHandleBase&& other) noexcept {
41 Move(std::move(other));
42}
43
44ProtoZeroMessageHandleBase& ProtoZeroMessageHandleBase::operator=(
45 ProtoZeroMessageHandleBase&& other) {
46 // If the current handle was pointing to a message and is being reset to a new
47 // one, finalize the old message.
Primiano Tucci25aff4e2017-11-23 12:18:52 +000048 Finalize();
Primiano Tucci2a29ac72017-10-24 17:47:19 +010049 Move(std::move(other));
50 return *this;
51}
52
Primiano Tucci25aff4e2017-11-23 12:18:52 +000053void ProtoZeroMessageHandleBase::Finalize() {
54 if (!message_)
55 return;
56 const size_t size = message_->Finalize();
57 if (on_finalize_) {
58 on_finalize_(size);
59 on_finalize_ = nullptr;
60 }
61}
62
Primiano Tucci2a29ac72017-10-24 17:47:19 +010063void ProtoZeroMessageHandleBase::Move(ProtoZeroMessageHandleBase&& other) {
64 // In theory other->message_ could be nullptr, if |other| is a handle that has
65 // been std::move-d (and hence empty). There isn't a legitimate use case for
66 // doing so, though. Therefore this case is deliberately ignored (if hit, it
67 // will manifest as a segfault when dereferencing |message_| below) to avoid a
68 // useless null-check.
69 message_ = other.message_;
70 other.message_ = nullptr;
Primiano Tucci25aff4e2017-11-23 12:18:52 +000071 on_finalize_ = std::move(other.on_finalize_);
72 other.on_finalize_ = nullptr;
Primiano Tucci2a29ac72017-10-24 17:47:19 +010073#if PROTOZERO_ENABLE_HANDLE_DEBUGGING()
74 message_->set_handle(this);
75#endif
76}
77
78} // namespace protozero