15bebb993Sopenharmony_ci/*
25bebb993Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
35bebb993Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
45bebb993Sopenharmony_ci * you may not use this file except in compliance with the License.
55bebb993Sopenharmony_ci * You may obtain a copy of the License at
65bebb993Sopenharmony_ci *
75bebb993Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
85bebb993Sopenharmony_ci *
95bebb993Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
105bebb993Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
115bebb993Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
125bebb993Sopenharmony_ci * See the License for the specific language governing permissions and
135bebb993Sopenharmony_ci * limitations under the License.
145bebb993Sopenharmony_ci */
155bebb993Sopenharmony_ci
165bebb993Sopenharmony_ci#ifndef FILEMGMT_LIBN_N_CLASS_H
175bebb993Sopenharmony_ci#define FILEMGMT_LIBN_N_CLASS_H
185bebb993Sopenharmony_ci
195bebb993Sopenharmony_ci#include <map>
205bebb993Sopenharmony_ci#include <memory>
215bebb993Sopenharmony_ci#include <mutex>
225bebb993Sopenharmony_ci#include <string>
235bebb993Sopenharmony_ci#include <tuple>
245bebb993Sopenharmony_ci#include <vector>
255bebb993Sopenharmony_ci
265bebb993Sopenharmony_ci#include "n_napi.h"
275bebb993Sopenharmony_ci#include "filemgmt_libhilog.h"
285bebb993Sopenharmony_ci
295bebb993Sopenharmony_cinamespace OHOS {
305bebb993Sopenharmony_cinamespace FileManagement {
315bebb993Sopenharmony_cinamespace LibN {
325bebb993Sopenharmony_ciclass NClass final {
335bebb993Sopenharmony_cipublic:
345bebb993Sopenharmony_ci    NClass(const NClass&) = delete;
355bebb993Sopenharmony_ci    NClass &operator=(const NClass&) = delete;
365bebb993Sopenharmony_ci    static std::tuple<bool, napi_value> DefineClass(napi_env env,
375bebb993Sopenharmony_ci                                                    std::string className,
385bebb993Sopenharmony_ci                                                    napi_callback constructor,
395bebb993Sopenharmony_ci                                                    std::vector<napi_property_descriptor> &&properties);
405bebb993Sopenharmony_ci    static bool SaveClass(napi_env env, std::string className, napi_value exClass);
415bebb993Sopenharmony_ci    static void CleanClass(void *arg);
425bebb993Sopenharmony_ci    static napi_value InstantiateClass(napi_env env, const std::string& className, const std::vector<napi_value>& args);
435bebb993Sopenharmony_ci
445bebb993Sopenharmony_ci    template <class T> static T *GetEntityOf(napi_env env, napi_value objStat)
455bebb993Sopenharmony_ci    {
465bebb993Sopenharmony_ci        if (!env || !objStat) {
475bebb993Sopenharmony_ci            HILOGE("Empty input: env %d, obj %d", env == nullptr, objStat == nullptr);
485bebb993Sopenharmony_ci            return nullptr;
495bebb993Sopenharmony_ci        }
505bebb993Sopenharmony_ci        T *t = nullptr;
515bebb993Sopenharmony_ci        napi_status status = napi_unwrap(env, objStat, (void **)&t);
525bebb993Sopenharmony_ci        if (status != napi_ok) {
535bebb993Sopenharmony_ci            HILOGE("Cannot umwarp for pointer: %d", status);
545bebb993Sopenharmony_ci            return nullptr;
555bebb993Sopenharmony_ci        }
565bebb993Sopenharmony_ci        return t;
575bebb993Sopenharmony_ci    }
585bebb993Sopenharmony_ci
595bebb993Sopenharmony_ci    template <class T> static bool SetEntityFor(napi_env env, napi_value obj, std::unique_ptr<T> entity)
605bebb993Sopenharmony_ci    {
615bebb993Sopenharmony_ci        napi_status status = napi_wrap(
625bebb993Sopenharmony_ci            env, obj, entity.release(),
635bebb993Sopenharmony_ci            [](napi_env env, void *data, void *hint) {
645bebb993Sopenharmony_ci                std::unique_ptr<T>(static_cast<T *>(data));
655bebb993Sopenharmony_ci            },
665bebb993Sopenharmony_ci            nullptr, nullptr);
675bebb993Sopenharmony_ci        return status == napi_ok;
685bebb993Sopenharmony_ci    }
695bebb993Sopenharmony_ci
705bebb993Sopenharmony_ci    template <class T> static T *RemoveEntityOfFinal(napi_env env, napi_value objStat)
715bebb993Sopenharmony_ci    {
725bebb993Sopenharmony_ci        if (!env || !objStat) {
735bebb993Sopenharmony_ci            HILOGD("Empty input: env %d,obj %d", env == nullptr, objStat == nullptr);
745bebb993Sopenharmony_ci            return nullptr;
755bebb993Sopenharmony_ci        }
765bebb993Sopenharmony_ci        T *t = nullptr;
775bebb993Sopenharmony_ci        napi_status status = napi_remove_wrap(env, objStat, (void **)&t);
785bebb993Sopenharmony_ci        if (status != napi_ok) {
795bebb993Sopenharmony_ci            HILOGD("Cannot umwrap for pointer: %d", status);
805bebb993Sopenharmony_ci            return nullptr;
815bebb993Sopenharmony_ci        }
825bebb993Sopenharmony_ci        return t;
835bebb993Sopenharmony_ci    }
845bebb993Sopenharmony_ci
855bebb993Sopenharmony_ciprivate:
865bebb993Sopenharmony_ci    NClass() : addCleanHook(false) {};
875bebb993Sopenharmony_ci    ~NClass() = default;
885bebb993Sopenharmony_ci    static NClass &GetInstance();
895bebb993Sopenharmony_ci    std::map<std::string, napi_ref> exClassMap;
905bebb993Sopenharmony_ci    std::mutex exClassMapLock;
915bebb993Sopenharmony_ci    bool addCleanHook;
925bebb993Sopenharmony_ci};
935bebb993Sopenharmony_ci} // namespace LibN
945bebb993Sopenharmony_ci} // namespace FileManagement
955bebb993Sopenharmony_ci} // namespace OHOS
965bebb993Sopenharmony_ci
975bebb993Sopenharmony_ci#endif // FILEMGMT_LIBN_N_CLASS_H
98