• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 INTERFACES_KITS_JS_SRC_COMMON_NAPI_N_CLASS_H
17 #define INTERFACES_KITS_JS_SRC_COMMON_NAPI_N_CLASS_H
18 
19 #include <map>
20 
21 #include "log.h"
22 #include "uni_header.h"
23 
24 namespace OHOS {
25 namespace DistributedFS {
26 class NClass final {
27 public:
28     NClass(const NClass &) = delete;
29     NClass &operator = (const NClass &) = delete;
30     static NClass &GetInstance();
31 
32     static std::tuple<bool, napi_value> DefineClass(napi_env env,
33                                                     std::string className,
34                                                     napi_callback constructor,
35                                                     std::vector<napi_property_descriptor> &&properties);
36     static bool SaveClass(napi_env env, std::string className, napi_value exClass);
37     static napi_value InstantiateClass(napi_env env, const std::string& className, const std::vector<napi_value>& args);
38 
GetEntityOf(napi_env env,napi_value objStat)39     template <class T> static T *GetEntityOf(napi_env env, napi_value objStat)
40     {
41         if (!env || !objStat) {
42             HILOGE("Empty input: env %d, obj %d", env == nullptr, objStat == nullptr);
43             return nullptr;
44         }
45         T *t = nullptr;
46         napi_status status = napi_unwrap(env, objStat, (void **)&t);
47         if (status != napi_ok) {
48             HILOGE("Cannot umwarp for pointer: %d", status);
49             return nullptr;
50         }
51         return t;
52     }
53 
SetEntityFor(napi_env env,napi_value obj,std::unique_ptr<T> entity)54     template <class T> static bool SetEntityFor(napi_env env, napi_value obj, std::unique_ptr<T> entity)
55     {
56         napi_status status = napi_wrap(
57             env,
58             obj,
59             entity.release(),
60             [](napi_env env, void *data, void *hint) {
61                 std::unique_ptr<T>(static_cast<T *>(data));
62             },
63             nullptr,
64             nullptr);
65         return status == napi_ok;
66     }
67 
RemoveEntityOfFinal(napi_env env,napi_value objStat)68     template <class T> static T *RemoveEntityOfFinal(napi_env env, napi_value objStat)
69     {
70         if (!env || !objStat) {
71             HILOGD("Empty input: env %d,obj %d", env == nullptr, objStat == nullptr);
72             return nullptr;
73         }
74         T *t = nullptr;
75         napi_status status = napi_remove_wrap(env, objStat, (void **)&t);
76         if (status != napi_ok) {
77             HILOGD("Cannot umwrap for pointer: %d", status);
78             return nullptr;
79         }
80         return t;
81     }
82 
83 private:
84     NClass() = default;
85     ~NClass() = default;
86     std::map<std::string, napi_ref> exClassMap;
87     std::mutex exClassMapLock;
88 };
89 } // namespace DistributedFS
90 } // namespace OHOS
91 #endif // INTERFACES_KITS_JS_SRC_COMMON_NAPI_N_CLASS_H