15bebb993Sopenharmony_ci/*
25bebb993Sopenharmony_ci * Copyright (c) 2022-2023 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_VAL_H
175bebb993Sopenharmony_ci#define FILEMGMT_LIBN_N_VAL_H
185bebb993Sopenharmony_ci
195bebb993Sopenharmony_ci#include <cstdint>
205bebb993Sopenharmony_ci#include <memory>
215bebb993Sopenharmony_ci#include <tuple>
225bebb993Sopenharmony_ci#include <vector>
235bebb993Sopenharmony_ci
245bebb993Sopenharmony_ci#include "n_napi.h"
255bebb993Sopenharmony_ci
265bebb993Sopenharmony_cinamespace OHOS {
275bebb993Sopenharmony_cinamespace FileManagement {
285bebb993Sopenharmony_cinamespace LibN {
295bebb993Sopenharmony_ciclass NVal final {
305bebb993Sopenharmony_cipublic:
315bebb993Sopenharmony_ci    NVal() = default;
325bebb993Sopenharmony_ci    NVal(napi_env nEnv, napi_value nVal);
335bebb993Sopenharmony_ci    NVal(const NVal &) = default;
345bebb993Sopenharmony_ci    NVal &operator=(const NVal &) = default;
355bebb993Sopenharmony_ci    virtual ~NVal() = default;
365bebb993Sopenharmony_ci
375bebb993Sopenharmony_ci    /* NOTE! env_ and val_ is LIKELY to be null */
385bebb993Sopenharmony_ci    napi_env env_ = nullptr;
395bebb993Sopenharmony_ci    napi_value val_ = nullptr;
405bebb993Sopenharmony_ci
415bebb993Sopenharmony_ci    explicit operator bool() const;
425bebb993Sopenharmony_ci    bool TypeIs(napi_valuetype expType) const;
435bebb993Sopenharmony_ci    bool TypeIsError(bool checkErrno = false) const;
445bebb993Sopenharmony_ci
455bebb993Sopenharmony_ci    /* SHOULD ONLY BE USED FOR EXPECTED TYPE */
465bebb993Sopenharmony_ci    std::tuple<bool, std::unique_ptr<char[]>, size_t> ToUTF8String() const;
475bebb993Sopenharmony_ci    std::tuple<bool, std::unique_ptr<char[]>, size_t> ToUTF8String(std::string defaultValue) const;
485bebb993Sopenharmony_ci    std::tuple<bool, std::unique_ptr<char[]>, size_t> ToUTF16String() const;
495bebb993Sopenharmony_ci    std::tuple<bool, std::unique_ptr<char[]>, size_t> ToUTF8StringPath() const;
505bebb993Sopenharmony_ci    std::tuple<bool, void *> ToPointer() const;
515bebb993Sopenharmony_ci    std::tuple<bool, bool> ToBool() const;
525bebb993Sopenharmony_ci    std::tuple<bool, bool> ToBool(bool defaultValue) const;
535bebb993Sopenharmony_ci    std::tuple<bool, int32_t> ToInt32() const;
545bebb993Sopenharmony_ci    std::tuple<bool, int32_t> ToInt32(int32_t defaultValue) const;
555bebb993Sopenharmony_ci    std::tuple<bool, int64_t> ToInt64() const;
565bebb993Sopenharmony_ci    std::tuple<bool, int64_t> ToInt64(int64_t defaultValue) const;
575bebb993Sopenharmony_ci    std::tuple<bool, void *, size_t> ToArraybuffer() const;
585bebb993Sopenharmony_ci    std::tuple<bool, void *, size_t> ToTypedArray() const;
595bebb993Sopenharmony_ci    std::tuple<bool, std::vector<std::string>, uint32_t> ToStringArray();
605bebb993Sopenharmony_ci    std::tuple<bool, uint64_t, bool> ToUint64() const;
615bebb993Sopenharmony_ci    std::tuple<bool, uint32_t> ToUint32() const;
625bebb993Sopenharmony_ci    std::tuple<bool, double> ToDouble() const;
635bebb993Sopenharmony_ci
645bebb993Sopenharmony_ci    /* Static helpers to create js objects */
655bebb993Sopenharmony_ci    static NVal CreateUndefined(napi_env env);
665bebb993Sopenharmony_ci    static NVal CreateBigInt64(napi_env env, int64_t val);
675bebb993Sopenharmony_ci    static NVal CreateInt64(napi_env env, int64_t val);
685bebb993Sopenharmony_ci    static NVal CreateInt32(napi_env env, int32_t val);
695bebb993Sopenharmony_ci    static NVal CreateUint32(napi_env env, int32_t val);
705bebb993Sopenharmony_ci    static NVal CreateObject(napi_env env);
715bebb993Sopenharmony_ci    static NVal CreateBool(napi_env env, bool val);
725bebb993Sopenharmony_ci    static NVal CreateUTF8String(napi_env env, std::string str);
735bebb993Sopenharmony_ci    static NVal CreateUTF8String(napi_env env, const char *str, ssize_t len);
745bebb993Sopenharmony_ci    static NVal CreateUint8Array(napi_env env, void *buf, size_t bufLen);
755bebb993Sopenharmony_ci    static NVal CreateArrayString(napi_env env, std::vector<std::string> strs);
765bebb993Sopenharmony_ci    static std::tuple<NVal, void *> CreateArrayBuffer(napi_env env, size_t len);
775bebb993Sopenharmony_ci
785bebb993Sopenharmony_ci    /* SHOULD ONLY BE USED FOR OBJECT */
795bebb993Sopenharmony_ci    bool HasProp(std::string propName) const;
805bebb993Sopenharmony_ci#ifdef WIN_PLATFORM
815bebb993Sopenharmony_ci    NVal GetPropValue(std::string propName) const;
825bebb993Sopenharmony_ci#else
835bebb993Sopenharmony_ci    NVal GetProp(std::string propName) const;
845bebb993Sopenharmony_ci#endif
855bebb993Sopenharmony_ci    bool AddProp(std::vector<napi_property_descriptor> &&propVec) const;
865bebb993Sopenharmony_ci    bool AddProp(std::string propName, napi_value nVal) const;
875bebb993Sopenharmony_ci
885bebb993Sopenharmony_ci    /* Static helpers to create prop of js objects */
895bebb993Sopenharmony_ci    static napi_property_descriptor DeclareNapiProperty(const char *name, napi_value val);
905bebb993Sopenharmony_ci    static napi_property_descriptor DeclareNapiStaticProperty(const char *name, napi_value val);
915bebb993Sopenharmony_ci    static napi_property_descriptor DeclareNapiFunction(const char *name, napi_callback func);
925bebb993Sopenharmony_ci    static napi_property_descriptor DeclareNapiStaticFunction(const char *name, napi_callback func);
935bebb993Sopenharmony_ci    static napi_property_descriptor DeclareNapiGetter(const char *name, napi_callback getter);
945bebb993Sopenharmony_ci    static napi_property_descriptor DeclareNapiSetter(const char *name, napi_callback setter);
955bebb993Sopenharmony_ci    static inline napi_property_descriptor
965bebb993Sopenharmony_ci        DeclareNapiGetterSetter(const char *name, napi_callback getter, napi_callback setter);
975bebb993Sopenharmony_ci};
985bebb993Sopenharmony_ci} // namespace LibN
995bebb993Sopenharmony_ci} // namespace FileManagement
1005bebb993Sopenharmony_ci} // namespace OHOS
1015bebb993Sopenharmony_ci
1025bebb993Sopenharmony_ci#endif // FILEMGMT_LIBN_N_VAL_H
103