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