blob: d8e5ad235be98669c2d967096ee9828b89c8b454 [file] [log] [blame]
Primiano Tucci4f9b6d72017-12-05 20:59:16 +00001/*
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 Dearmane1e56b62018-02-21 19:11:58 +000017#include "perfetto/protozero/message_handle.h"
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000018
19#include <utility>
20
Hector Dearmane1e56b62018-02-21 19:11:58 +000021#include "perfetto/protozero/message.h"
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000022
23namespace protozero {
24
Hector Dearmanaaa4c192018-02-19 11:57:35 +000025MessageHandleBase::MessageHandleBase(Message* message) : message_(message) {
Florian Mayer8a8044f2018-01-11 16:03:09 +000026#if PERFETTO_DCHECK_IS_ON()
27 generation_ = message_ ? message->generation_ : 0;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000028 if (message_)
29 message_->set_handle(this);
30#endif
31}
32
Hector Dearmanaaa4c192018-02-19 11:57:35 +000033MessageHandleBase::~MessageHandleBase() {
Florian Mayer8a8044f2018-01-11 16:03:09 +000034 if (message_) {
35#if PERFETTO_DCHECK_IS_ON()
36 PERFETTO_DCHECK(generation_ == message_->generation_);
37#endif
Primiano Tucci3a918872017-12-18 10:53:39 +010038 message_->Finalize();
Florian Mayer8a8044f2018-01-11 16:03:09 +000039 }
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000040}
41
Hector Dearmanaaa4c192018-02-19 11:57:35 +000042MessageHandleBase::MessageHandleBase(MessageHandleBase&& other) noexcept {
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000043 Move(std::move(other));
44}
45
Hector Dearmanaaa4c192018-02-19 11:57:35 +000046MessageHandleBase& MessageHandleBase::operator=(MessageHandleBase&& other) {
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000047 // If the current handle was pointing to a message and is being reset to a new
Lalit Maganti3dc757b2018-01-09 16:48:04 +000048 // 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 Tucci3a918872017-12-18 10:53:39 +010051 message_->Finalize();
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000052 Move(std::move(other));
53 return *this;
54}
55
Hector Dearmanaaa4c192018-02-19 11:57:35 +000056void MessageHandleBase::Move(MessageHandleBase&& other) {
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000057 message_ = other.message_;
58 other.message_ = nullptr;
Florian Mayer8a8044f2018-01-11 16:03:09 +000059#if PERFETTO_DCHECK_IS_ON()
Primiano Tucci9ce66e82018-04-30 00:28:25 +010060 if (message_) {
61 generation_ = message_->generation_;
62 message_->set_handle(this);
63 }
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000064#endif
65}
66
67} // namespace protozero