Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [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 | |
Hector Dearman | e1e56b6 | 2018-02-21 19:11:58 +0000 | [diff] [blame] | 17 | #include "perfetto/protozero/message_handle.h" |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 18 | |
| 19 | #include <utility> |
| 20 | |
Hector Dearman | e1e56b6 | 2018-02-21 19:11:58 +0000 | [diff] [blame] | 21 | #include "perfetto/protozero/message.h" |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 22 | |
| 23 | namespace protozero { |
| 24 | |
Hector Dearman | aaa4c19 | 2018-02-19 11:57:35 +0000 | [diff] [blame] | 25 | MessageHandleBase::MessageHandleBase(Message* message) : message_(message) { |
Florian Mayer | 8a8044f | 2018-01-11 16:03:09 +0000 | [diff] [blame] | 26 | #if PERFETTO_DCHECK_IS_ON() |
| 27 | generation_ = message_ ? message->generation_ : 0; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 28 | if (message_) |
| 29 | message_->set_handle(this); |
| 30 | #endif |
| 31 | } |
| 32 | |
Hector Dearman | aaa4c19 | 2018-02-19 11:57:35 +0000 | [diff] [blame] | 33 | MessageHandleBase::~MessageHandleBase() { |
Florian Mayer | 8a8044f | 2018-01-11 16:03:09 +0000 | [diff] [blame] | 34 | if (message_) { |
| 35 | #if PERFETTO_DCHECK_IS_ON() |
| 36 | PERFETTO_DCHECK(generation_ == message_->generation_); |
| 37 | #endif |
Primiano Tucci | 3a91887 | 2017-12-18 10:53:39 +0100 | [diff] [blame] | 38 | message_->Finalize(); |
Florian Mayer | 8a8044f | 2018-01-11 16:03:09 +0000 | [diff] [blame] | 39 | } |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Hector Dearman | aaa4c19 | 2018-02-19 11:57:35 +0000 | [diff] [blame] | 42 | MessageHandleBase::MessageHandleBase(MessageHandleBase&& other) noexcept { |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 43 | Move(std::move(other)); |
| 44 | } |
| 45 | |
Hector Dearman | aaa4c19 | 2018-02-19 11:57:35 +0000 | [diff] [blame] | 46 | MessageHandleBase& MessageHandleBase::operator=(MessageHandleBase&& other) { |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 47 | // If the current handle was pointing to a message and is being reset to a new |
Lalit Maganti | 3dc757b | 2018-01-09 16:48:04 +0000 | [diff] [blame] | 48 | // one, finalize the old message. However, if the other message is the same as |
| 49 | // the one we point to, don't finalize. |
| 50 | if (message_ && message_ != other.message_) |
Primiano Tucci | 3a91887 | 2017-12-18 10:53:39 +0100 | [diff] [blame] | 51 | message_->Finalize(); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 52 | Move(std::move(other)); |
| 53 | return *this; |
| 54 | } |
| 55 | |
Hector Dearman | aaa4c19 | 2018-02-19 11:57:35 +0000 | [diff] [blame] | 56 | void MessageHandleBase::Move(MessageHandleBase&& other) { |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 57 | message_ = other.message_; |
| 58 | other.message_ = nullptr; |
Florian Mayer | 8a8044f | 2018-01-11 16:03:09 +0000 | [diff] [blame] | 59 | #if PERFETTO_DCHECK_IS_ON() |
Primiano Tucci | 9ce66e8 | 2018-04-30 00:28:25 +0100 | [diff] [blame] | 60 | if (message_) { |
| 61 | generation_ = message_->generation_; |
| 62 | message_->set_handle(this); |
| 63 | } |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 64 | #endif |
| 65 | } |
| 66 | |
| 67 | } // namespace protozero |