• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 UDMF_NAPI_DATA_UTILS_H
17 #define UDMF_NAPI_DATA_UTILS_H
18 
19 #include <cstdint>
20 #include <map>
21 #include <memory>
22 #include <string>
23 #include <variant>
24 #include <vector>
25 
26 #include "napi/native_api.h"
27 #include "napi/native_common.h"
28 #include "napi/native_node_api.h"
29 #include "napi_common_want.h"
30 #include "securec.h"
31 
32 #include "logger.h"
33 #include "unified_meta.h"
34 
35 namespace OHOS {
36 namespace UDMF {
37 class NapiDataUtils {
38 public:
39     /* napi_value <-> bool */
40     static napi_status GetValue(napi_env env, napi_value in, bool &out);
41     static napi_status SetValue(napi_env env, const bool &in, napi_value &out);
42 
43     /* napi_value <-> int32_t */
44     static napi_status GetValue(napi_env env, napi_value in, int32_t &out);
45     static napi_status SetValue(napi_env env, const int32_t &in, napi_value &out);
46 
47     /* napi_value <-> int64_t */
48     static napi_status GetValue(napi_env env, napi_value in, int64_t &out);
49     static napi_status SetValue(napi_env env, const int64_t &in, napi_value &out);
50 
51     /* napi_value <-> float */
52     static napi_status GetValue(napi_env env, napi_value in, float &out);
53     static napi_status SetValue(napi_env env, const float &in, napi_value &out);
54 
55     /* napi_value <-> double */
56     static napi_status GetValue(napi_env env, napi_value in, double &out);
57     static napi_status SetValue(napi_env env, const double &in, napi_value &out);
58 
59     /* napi_value <-> std::string */
60     static napi_status GetValue(napi_env env, napi_value in, std::string &out);
61     static napi_status SetValue(napi_env env, const std::string &in, napi_value &out);
62 
63     /* napi_value <-> std::vector<std::string> */
64     static napi_status GetValue(napi_env env, napi_value in, std::vector<std::string> &out);
65     static napi_status SetValue(napi_env env, const std::vector<std::string> &in, napi_value &out);
66 
67     /* napi_value <-> std::vector<uint8_t> */
68     static napi_status GetValue(napi_env env, napi_value in, std::vector<uint8_t> &out);
69     static napi_status SetValue(napi_env env, const std::vector<uint8_t> &in, napi_value &out);
70 
71     /* napi_value <-> std::map<std::string, int32_t> */
72     static napi_status GetValue(napi_env env, napi_value in, std::map<std::string, int32_t> &out);
73     static napi_status SetValue(napi_env env, const std::map<std::string, int32_t> &in, napi_value &out);
74 
75     /* napi_value <-> std::map<std::string, int64_t> */
76     static napi_status GetValue(napi_env env, napi_value in, std::map<std::string, int64_t> &out);
77     static napi_status SetValue(napi_env env, const std::map<std::string, int64_t> &in, napi_value &out);
78 
79     /* napi_value <-> UDVariant */
80     static napi_status GetValue(napi_env env, napi_value in, UDVariant &out);
81     static napi_status SetValue(napi_env env, const UDVariant &in, napi_value &out);
82 
83     /* napi_value <-> UDDetails */
84     static napi_status GetValue(napi_env env, napi_value in, UDDetails &out);
85     static napi_status SetValue(napi_env env, const UDDetails &in, napi_value &out);
86 
87     static bool IsTypeForNapiValue(napi_env env, napi_value param, napi_valuetype expectType);
88 
89     static bool IsNull(napi_env env, napi_value value);
90     /* napi_define_class  wrapper */
91     static napi_value DefineClass(napi_env env, const std::string &name, const napi_property_descriptor *properties,
92         size_t count, napi_callback newcb);
93 
94 private:
95     enum {
96         /* std::map<key, value> to js::tuple<key, value> */
97         TUPLE_KEY = 0,
98         TUPLE_VALUE,
99         TUPLE_SIZE
100     };
101 };
102 
103 #define LOG_ERROR_RETURN(condition, message, retVal)                             \
104     do {                                                                         \
105         if (!(condition)) {                                                      \
106             LOG_ERROR(UDMF_KITS_NAPI, "test (" #condition ") failed: " message); \
107             return retVal;                                                       \
108         }                                                                        \
109     } while (0)
110 
111 #define LOG_ERROR_RETURN_VOID(condition, message)                                \
112     do {                                                                         \
113         if (!(condition)) {                                                      \
114             LOG_ERROR(UDMF_KITS_NAPI, "test (" #condition ") failed: " message); \
115             return;                                                              \
116         }                                                                        \
117     } while (0)
118 } // namespace UDMF
119 } // namespace OHOS
120 #endif // UDMF_NAPI_DATA_UTILS_H
121