1 /*
2  * Copyright (c) 2023 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 #ifndef OHOS_HIVIEWDFX_ADAPTER_SERVICE_IDL_INCLUDE_COLLECT_RESULT_PARCELABLE_H
17 #define OHOS_HIVIEWDFX_ADAPTER_SERVICE_IDL_INCLUDE_COLLECT_RESULT_PARCELABLE_H
18 
19 #include "parcel.h"
20 
21 #include <vector>
22 
23 #include "collect_result.h"
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 template<typename T>
28 struct CollectResultParcelable : public Parcelable {
CollectResultParcelableCollectResultParcelable29     CollectResultParcelable(CollectResult<T>& result)
30     {
31         result_.retCode = result.retCode;
32         if constexpr (std::is_same_v<std::decay_t<T>, int32_t>) {
33             result_.data = result.data;
34         }
35         if constexpr (std::is_same_v<std::decay_t<T>, double>) {
36             result_.data = result.data;
37         }
38         if constexpr (std::is_same_v<std::decay_t<T>, std::vector<std::string>>) {
39             result_.data.insert(result_.data.begin(), result.data.begin(), result.data.end());
40         }
41     }
42 
MarshallingCollectResultParcelable43     bool Marshalling(Parcel& outParcel) const override
44     {
45         if (!outParcel.WriteInt32(result_.retCode)) {
46             return false;
47         }
48         if constexpr (std::is_same_v<std::decay_t<T>, int32_t>) {
49             return outParcel.WriteInt32(result_.data);
50         }
51         if constexpr (std::is_same_v<std::decay_t<T>, double>) {
52             return outParcel.WriteDouble(result_.data);
53         }
54         if constexpr (std::is_same_v<std::decay_t<T>, std::vector<std::string>>) {
55             return outParcel.WriteStringVector(result_.data);
56         }
57         return true;
58     }
59 
UnmarshallingCollectResultParcelable60     static CollectResultParcelable<T>* Unmarshalling(Parcel& inParcel)
61     {
62         int32_t retCode;
63         if (!inParcel.ReadInt32(retCode)) {
64             return nullptr;
65         }
66         T data;
67         if constexpr (std::is_same_v<std::decay_t<T>, int32_t>) {
68             if (!inParcel.ReadInt32(data)) {
69                 return nullptr;
70             }
71         }
72         if constexpr (std::is_same_v<std::decay_t<T>, double>) {
73             if (!inParcel.ReadDouble(data)) {
74                 return nullptr;
75             }
76         }
77         if constexpr (std::is_same_v<std::decay_t<T>, std::vector<std::string>>) {
78             if (!inParcel.ReadStringVector(&data)) {
79                 return nullptr;
80             }
81         }
82         CollectResult<T> result;
83         result.retCode = UCollect::UcError(retCode);
84         result.data = data;
85         return new CollectResultParcelable(result);
86     }
87 
InitCollectResultParcelable88     static CollectResultParcelable<T> Init()
89     {
90         CollectResult<T>  ret;
91         ret.retCode = UCollect::UcError::UNSUPPORT;
92         CollectResultParcelable traceRet(ret);
93         return traceRet;
94     }
95 
96     CollectResult<T> result_;
97 };
98 } // namespace HiviewDFX
99 } // namespace OHOS
100 
101 #endif // OHOS_HIVIEWDFX_ADAPTER_SERVICE_IDL_INCLUDE_COLLECT_RESULT_PARCELABLE_H
102