• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_NAPI_COMMON_NAPI_N_CLASS_H
17 #define INTERFACES_KITS_NAPI_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.get(),
60             [](napi_env env, void *data, void *hint) {
61                 auto entity = static_cast<T *>(data);
62                 delete entity;
63             },
64             nullptr,
65             nullptr);
66         entity.release();
67         return status == napi_ok;
68     }
69 
70 private:
71     NClass() = default;
72     ~NClass() = default;
73     std::map<std::string, napi_ref> exClassMap;
74     std::mutex exClassMapLock;
75 };
76 } // namespace DistributedFS
77 } // namespace OHOS
78 #endif