• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 NAPI_API_ARRAY_H
17 #define NAPI_API_ARRAY_H
18 
19 #ifdef __OHOS_PLATFORM__
20 #include "napi/native_api.h"
21 #else
22 #include <node_api.h>
23 #endif
24 
25 #include <base/containers/string.h>
26 
27 #include "value.h"
28 
29 namespace NapiApi {
30 
31 class Array {
32 public:
33     Array() = default;
34     Array(napi_env env, size_t count);
35     Array(napi_env env, napi_value v);
36 
37     operator napi_value() const;
38     napi_env GetEnv() const;
39 
40     size_t Count() const;
41     napi_valuetype Type(size_t index) const;
42 
43     napi_value Get_value(size_t index) const;
44     void Set_value(size_t index, napi_value v) const;
45 
46     template<typename T>
Get(size_t index)47     Value<T> Get(size_t index) const
48     {
49         return Value<T> { env_, Get_value(index) };
50     }
51 
52     template<typename T>
Set(size_t index,T t)53     void Set(size_t index, T t) const
54     {
55         Set_value(index, Value<T>(env_, t).ToNapiValue());
56     }
57 
58 private:
59     napi_env env_ { nullptr };
60     napi_value array_ { nullptr };
61 };
62 
63 } // namespace NapiApi
64 
65 #endif
66