1 /*
2  * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 #ifndef OHOS_INTER_IPC_CODEC_H
17 #define OHOS_INTER_IPC_CODEC_H
18 
19 #include "iremote_object.h"
20 #include "message_parcel.h"
21 
22 namespace OHOS {
23 namespace Sharing {
24 namespace IPC_CODEC {
25 
26 bool IpcEncodeBindAttr(MessageParcel &pIpcMsg, bool attr);
27 bool IpcEncodeBindAttr(MessageParcel &pIpcMsg, int8_t attr);
28 bool IpcEncodeBindAttr(MessageParcel &pIpcMsg, int16_t attr);
29 bool IpcEncodeBindAttr(MessageParcel &pIpcMsg, int32_t attr);
30 bool IpcEncodeBindAttr(MessageParcel &pIpcMsg, int64_t attr);
31 bool IpcEncodeBindAttr(MessageParcel &pIpcMsg, uint8_t attr);
32 bool IpcEncodeBindAttr(MessageParcel &pIpcMsg, uint16_t attr);
33 bool IpcEncodeBindAttr(MessageParcel &pIpcMsg, uint32_t attr);
34 bool IpcEncodeBindAttr(MessageParcel &pIpcMsg, uint64_t attr);
35 bool IpcEncodeBindAttr(MessageParcel &pIpcMsg, float attr);
36 bool IpcEncodeBindAttr(MessageParcel &pIpcMsg, double attr);
37 bool IpcEncodeBindAttr(MessageParcel &pIpcMsg, std::string &attr);
38 
39 bool IpcEncodeBindAttr(MessageParcel &pIpcMsg, sptr<IRemoteObject> &attr);
40 
41 template <typename TYPE>
IpcEncodeBindAttr(MessageParcel &pIpcMsg, TYPE &t)42 bool IpcEncodeBindAttr(MessageParcel &pIpcMsg, TYPE &t)
43 {
44     if (0 == t.IpcSerialize(pIpcMsg)) {
45         return true;
46     }
47 
48     return false;
49 }
50 
51 template <typename TYPE>
IpcEncodeBindAttr(MessageParcel &pIpcMsg, std::vector<TYPE> &vec)52 bool IpcEncodeBindAttr(MessageParcel &pIpcMsg, std::vector<TYPE> &vec)
53 {
54     int32_t size = (int32_t)vec.size();
55     IpcEncodeBindAttr(pIpcMsg, size);
56     for (auto &item : vec) {
57         if (!IpcEncodeBindAttr(pIpcMsg, item)) {
58             return false;
59         }
60     }
61 
62     return true;
63 }
64 
65 bool IpcDecodeBindAttr(MessageParcel &pIpcMsg, bool &attr);
66 bool IpcDecodeBindAttr(MessageParcel &pIpcMsg, int8_t &attr);
67 bool IpcDecodeBindAttr(MessageParcel &pIpcMsg, int16_t &attr);
68 bool IpcDecodeBindAttr(MessageParcel &pIpcMsg, int32_t &attr);
69 bool IpcDecodeBindAttr(MessageParcel &pIpcMsg, int64_t &attr);
70 bool IpcDecodeBindAttr(MessageParcel &pIpcMsg, uint8_t &attr);
71 bool IpcDecodeBindAttr(MessageParcel &pIpcMsg, uint16_t &attr);
72 bool IpcDecodeBindAttr(MessageParcel &pIpcMsg, uint32_t &attr);
73 bool IpcDecodeBindAttr(MessageParcel &pIpcMsg, uint64_t &attr);
74 bool IpcDecodeBindAttr(MessageParcel &pIpcMsg, float &attr);
75 bool IpcDecodeBindAttr(MessageParcel &pIpcMsg, double &attr);
76 bool IpcDecodeBindAttr(MessageParcel &pIpcMsg, std::string &attr);
77 bool IpcDecodeBindAttr(MessageParcel &pIpcMsg, sptr<IRemoteObject> &attr);
78 
79 template <typename TYPE>
IpcDecodeBindAttr(MessageParcel &pIpcMsg, TYPE &t)80 bool IpcDecodeBindAttr(MessageParcel &pIpcMsg, TYPE &t)
81 {
82     if (0 == t.IpcDeserialize(pIpcMsg)) {
83         return true;
84     }
85 
86     return false;
87 }
88 
89 template <typename TYPE>
IpcDecodeBindAttr(MessageParcel &pIpcMsg, std::vector<TYPE> &vec)90 bool IpcDecodeBindAttr(MessageParcel &pIpcMsg, std::vector<TYPE> &vec)
91 {
92     int32_t size;
93     IpcDecodeBindAttr(pIpcMsg, size);
94     vec.resize(size);
95 
96     for (int i = 0; i < size; i++) {
97         if (!IpcDecodeBindAttr(pIpcMsg, vec[i])) {
98             return false;
99         }
100     }
101 
102     return true;
103 }
104 
105 } // namespace IPC_CODEC
106 } // namespace Sharing
107 } // namespace OHOS
108 
109 #endif
110