122736c2fSopenharmony_ci/*
222736c2fSopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd.
322736c2fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
422736c2fSopenharmony_ci * you may not use this file except in compliance with the License.
522736c2fSopenharmony_ci * You may obtain a copy of the License at
622736c2fSopenharmony_ci *
722736c2fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
822736c2fSopenharmony_ci *
922736c2fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1022736c2fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1122736c2fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1222736c2fSopenharmony_ci * See the License for the specific language governing permissions and
1322736c2fSopenharmony_ci * limitations under the License.
1422736c2fSopenharmony_ci */
1522736c2fSopenharmony_ci
1622736c2fSopenharmony_ci#include "message.h"
1722736c2fSopenharmony_ci
1822736c2fSopenharmony_cinamespace OHOS {
1922736c2fSopenharmony_cinamespace MiscServices {
2022736c2fSopenharmony_ci/*! Constructor
2122736c2fSopenharmony_ci * @param msgId a message Id
2222736c2fSopenharmony_ci * @param msgContent the content of a message
2322736c2fSopenharmony_ci */
2422736c2fSopenharmony_ciMessage::Message(int32_t msgId, MessageParcel *msgContent)
2522736c2fSopenharmony_ci{
2622736c2fSopenharmony_ci    msgId_ = msgId;
2722736c2fSopenharmony_ci    msgContent_ = msgContent;
2822736c2fSopenharmony_ci    if (msgContent_ != nullptr) {
2922736c2fSopenharmony_ci        msgContent_->RewindRead(0);
3022736c2fSopenharmony_ci    }
3122736c2fSopenharmony_ci}
3222736c2fSopenharmony_ci
3322736c2fSopenharmony_ci/*!Constructor
3422736c2fSopenharmony_ci * @param msg a source message
3522736c2fSopenharmony_ci */
3622736c2fSopenharmony_ciMessage::Message(const Message &msg)
3722736c2fSopenharmony_ci{
3822736c2fSopenharmony_ci    msgId_ = msg.msgId_;
3922736c2fSopenharmony_ci    if (msgContent_ != nullptr) {
4022736c2fSopenharmony_ci        delete msgContent_;
4122736c2fSopenharmony_ci        msgContent_ = nullptr;
4222736c2fSopenharmony_ci    }
4322736c2fSopenharmony_ci    MessageParcel *src = msg.msgContent_;
4422736c2fSopenharmony_ci    if (src != nullptr) {
4522736c2fSopenharmony_ci        msgContent_ = new MessageParcel();
4622736c2fSopenharmony_ci        msgContent_->ParseFrom(src->GetData(), src->GetDataSize());
4722736c2fSopenharmony_ci    }
4822736c2fSopenharmony_ci}
4922736c2fSopenharmony_ci
5022736c2fSopenharmony_ciMessage &Message::operator=(const Message &msg)
5122736c2fSopenharmony_ci{
5222736c2fSopenharmony_ci    if (this == &msg) {
5322736c2fSopenharmony_ci        return *this;
5422736c2fSopenharmony_ci    }
5522736c2fSopenharmony_ci    msgId_ = msg.msgId_;
5622736c2fSopenharmony_ci    if (msgContent_ != nullptr) {
5722736c2fSopenharmony_ci        delete msgContent_;
5822736c2fSopenharmony_ci        msgContent_ = nullptr;
5922736c2fSopenharmony_ci    }
6022736c2fSopenharmony_ci    if (msg.msgContent_ != nullptr) {
6122736c2fSopenharmony_ci        msgContent_ = new MessageParcel();
6222736c2fSopenharmony_ci        msgContent_->ParseFrom(msg.msgContent_->GetData(), msg.msgContent_->GetDataSize());
6322736c2fSopenharmony_ci        msgContent_->RewindRead(0);
6422736c2fSopenharmony_ci    }
6522736c2fSopenharmony_ci    return *this;
6622736c2fSopenharmony_ci}
6722736c2fSopenharmony_ci
6822736c2fSopenharmony_ciMessage::~Message()
6922736c2fSopenharmony_ci{
7022736c2fSopenharmony_ci    if (msgContent_ != nullptr) {
7122736c2fSopenharmony_ci        delete msgContent_;
7222736c2fSopenharmony_ci        msgContent_ = nullptr;
7322736c2fSopenharmony_ci    }
7422736c2fSopenharmony_ci}
7522736c2fSopenharmony_ci} // namespace MiscServices
7622736c2fSopenharmony_ci} // namespace OHOS
77