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