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
18 namespace OHOS {
19 namespace MiscServices {
20 /*! Constructor
21 * @param msgId a message Id
22 * @param msgContent the content of a message
23 */
Message(int32_t msgId,MessageParcel * msgContent)24 Message::Message(int32_t msgId, MessageParcel *msgContent)
25 {
26 msgId_ = msgId;
27 msgContent_ = msgContent;
28 if (msgContent_ != nullptr) {
29 msgContent_->RewindRead(0);
30 }
31 }
32
Message(int32_t msgId,MessageParcel * msgContent,const std::shared_ptr<BlockData<std::u16string>> & textResultHandler)33 Message::Message(
34 int32_t msgId, MessageParcel *msgContent, const std::shared_ptr<BlockData<std::u16string>> &textResultHandler)
35 : Message(msgId, msgContent)
36 {
37 textResultHandler_ = textResultHandler;
38 }
39
Message(int32_t msgId,MessageParcel * msgContent,const std::shared_ptr<BlockData<int32_t>> & indexResultHandler)40 Message::Message(
41 int32_t msgId, MessageParcel *msgContent, const std::shared_ptr<BlockData<int32_t>> &indexResultHandler)
42 : Message(msgId, msgContent)
43 {
44 indexResultHandler_ = indexResultHandler;
45 }
46
47 /*!Constructor
48 * @param msg a source message
49 */
Message(const Message & msg)50 Message::Message(const Message &msg)
51 {
52 msgId_ = msg.msgId_;
53 if (msgContent_ != nullptr) {
54 delete msgContent_;
55 msgContent_ = nullptr;
56 }
57 MessageParcel *src = msg.msgContent_;
58 if (src != nullptr) {
59 msgContent_ = new MessageParcel();
60 msgContent_->ParseFrom(src->GetData(), src->GetDataSize());
61 }
62 }
63
operator =(const Message & msg)64 Message &Message::operator=(const Message &msg)
65 {
66 if (this == &msg) {
67 return *this;
68 }
69 msgId_ = msg.msgId_;
70 if (msgContent_ != nullptr) {
71 delete msgContent_;
72 msgContent_ = nullptr;
73 }
74 if (msg.msgContent_ != nullptr) {
75 msgContent_ = new MessageParcel();
76 msgContent_->ParseFrom(msg.msgContent_->GetData(), msg.msgContent_->GetDataSize());
77 msgContent_->RewindRead(0);
78 }
79 return *this;
80 }
81
~Message()82 Message::~Message()
83 {
84 if (msgContent_ != nullptr) {
85 delete msgContent_;
86 msgContent_ = nullptr;
87 }
88 }
89 } // namespace MiscServices
90 } // namespace OHOS
91