Primiano Tucci | 2a29ac7 | 2017-10-24 17:47:19 +0100 | [diff] [blame] | 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 "protozero/protozero_message_handle.h" |
| 18 | |
| 19 | #include <utility> |
| 20 | |
Primiano Tucci | d7d1be0 | 2017-10-30 17:41:34 +0000 | [diff] [blame] | 21 | #include "base/logging.h" |
Primiano Tucci | 2a29ac7 | 2017-10-24 17:47:19 +0100 | [diff] [blame] | 22 | #include "protozero/protozero_message.h" |
| 23 | |
| 24 | namespace protozero { |
| 25 | |
Primiano Tucci | 2a29ac7 | 2017-10-24 17:47:19 +0100 | [diff] [blame] | 26 | ProtoZeroMessageHandleBase::ProtoZeroMessageHandleBase( |
| 27 | ProtoZeroMessage* message) |
| 28 | : message_(message) { |
| 29 | #if PROTOZERO_ENABLE_HANDLE_DEBUGGING() |
Hector Dearman | 8d8ccd3 | 2017-11-27 16:06:34 +0000 | [diff] [blame] | 30 | if (message_) |
| 31 | message_->set_handle(this); |
Primiano Tucci | 2a29ac7 | 2017-10-24 17:47:19 +0100 | [diff] [blame] | 32 | #endif |
| 33 | } |
| 34 | |
| 35 | ProtoZeroMessageHandleBase::~ProtoZeroMessageHandleBase() { |
Primiano Tucci | 25aff4e | 2017-11-23 12:18:52 +0000 | [diff] [blame] | 36 | Finalize(); |
Primiano Tucci | 2a29ac7 | 2017-10-24 17:47:19 +0100 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | ProtoZeroMessageHandleBase::ProtoZeroMessageHandleBase( |
| 40 | ProtoZeroMessageHandleBase&& other) noexcept { |
| 41 | Move(std::move(other)); |
| 42 | } |
| 43 | |
| 44 | ProtoZeroMessageHandleBase& 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 Tucci | 25aff4e | 2017-11-23 12:18:52 +0000 | [diff] [blame] | 48 | Finalize(); |
Primiano Tucci | 2a29ac7 | 2017-10-24 17:47:19 +0100 | [diff] [blame] | 49 | Move(std::move(other)); |
| 50 | return *this; |
| 51 | } |
| 52 | |
Primiano Tucci | 25aff4e | 2017-11-23 12:18:52 +0000 | [diff] [blame] | 53 | void 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 Tucci | 2a29ac7 | 2017-10-24 17:47:19 +0100 | [diff] [blame] | 63 | void 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 Tucci | 25aff4e | 2017-11-23 12:18:52 +0000 | [diff] [blame] | 71 | on_finalize_ = std::move(other.on_finalize_); |
| 72 | other.on_finalize_ = nullptr; |
Primiano Tucci | 2a29ac7 | 2017-10-24 17:47:19 +0100 | [diff] [blame] | 73 | #if PROTOZERO_ENABLE_HANDLE_DEBUGGING() |
| 74 | message_->set_handle(this); |
| 75 | #endif |
| 76 | } |
| 77 | |
| 78 | } // namespace protozero |