1 /*
2  * Copyright (c) 2024 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 NAPI_STRUCTS_BASE_H
17 #define NAPI_STRUCTS_BASE_H
18 
19 #include <functional>
20 
21 #include "business_error.h"
22 #include "napi_common_define.h"
23 
24 namespace OHOS::UpdateEngine {
25 struct SessionParams {
26     uint32_t type;
27     size_t callbackStartIndex;
28     bool isNeedBusinessError;
29     bool isAsyncCompleteWork;
30 
31     SessionParams(uint32_t typeValue = UINT32_MAX, size_t callbackPosition = CALLBACK_POSITION_ONE,
32         bool isNeedBusinessErrorValue = false, bool isAsyncCompleteWorkValue = false)
33         {
34             type = typeValue;
35             isNeedBusinessError = isNeedBusinessErrorValue;
36             isAsyncCompleteWork = isAsyncCompleteWorkValue;
37             callbackStartIndex = (callbackPosition < 1) ? CALLBACK_POSITION_ONE : INDEX(callbackPosition);
38         }
39 };
40 
41 struct NapiResult {
42     uint32_t type;
43     BusinessError businessError;
44 
45     template <typename T>
AssignValueNapiResult46     void AssignValue(T *const source, T *&target)
47     {
48         if (target == nullptr) {
49             target = new (std::nothrow) T();
50         }
51         if ((target != nullptr) && (source != nullptr)) {
52             *(target) = *(source);
53         }
54     }
55 
56     template <typename T>
ReleaseValueNapiResult57     void ReleaseValue(T *&obj)
58     {
59         delete obj;
60         obj = nullptr;
61     }
62 };
63 } // namespace OHOS::UpdateEngine
64 #endif // NAPI_STRUCTS_BASE_H