xref: /base/inputmethod/imf/common/src/message.cpp (revision 22736c2f)
1/*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include "message.h"
17
18namespace OHOS {
19namespace MiscServices {
20/*! Constructor
21 * @param msgId a message Id
22 * @param msgContent the content of a message
23 */
24Message::Message(int32_t msgId, MessageParcel *msgContent)
25{
26    msgId_ = msgId;
27    msgContent_ = msgContent;
28    if (msgContent_ != nullptr) {
29        msgContent_->RewindRead(0);
30    }
31}
32
33/*!Constructor
34 * @param msg a source message
35 */
36Message::Message(const Message &msg)
37{
38    msgId_ = msg.msgId_;
39    if (msgContent_ != nullptr) {
40        delete msgContent_;
41        msgContent_ = nullptr;
42    }
43    MessageParcel *src = msg.msgContent_;
44    if (src != nullptr) {
45        msgContent_ = new MessageParcel();
46        msgContent_->ParseFrom(src->GetData(), src->GetDataSize());
47    }
48}
49
50Message &Message::operator=(const Message &msg)
51{
52    if (this == &msg) {
53        return *this;
54    }
55    msgId_ = msg.msgId_;
56    if (msgContent_ != nullptr) {
57        delete msgContent_;
58        msgContent_ = nullptr;
59    }
60    if (msg.msgContent_ != nullptr) {
61        msgContent_ = new MessageParcel();
62        msgContent_->ParseFrom(msg.msgContent_->GetData(), msg.msgContent_->GetDataSize());
63        msgContent_->RewindRead(0);
64    }
65    return *this;
66}
67
68Message::~Message()
69{
70    if (msgContent_ != nullptr) {
71        delete msgContent_;
72        msgContent_ = nullptr;
73    }
74}
75} // namespace MiscServices
76} // namespace OHOS
77