• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 ARRAY_PROPERTY_PROXY_H
17 #define ARRAY_PROPERTY_PROXY_H
18 #include <meta/interface/intf_event.h>
19 #include <meta/interface/intf_task_queue.h>
20 #include <meta/interface/property/array_property.h>
21 #include <meta/interface/property/property.h>
22 #include <meta/interface/property/property_events.h>
23 #include <meta/api/util.h>
24 #include <napi_api.h>
25 
26 #include <base/containers/string.h>
27 #include <base/containers/string_view.h>
28 #include <base/containers/vector.h>
29 
30 class ArrayPropertyProxy {
31 public:
32     explicit ArrayPropertyProxy(META_NS::IProperty::Ptr prop, META_NS::IArrayAny::IndexType index);
33     virtual ~ArrayPropertyProxy();
34 
35     virtual void SetValue(NapiApi::FunctionContext<>& info) = 0;
36     virtual napi_value Value() = 0;
37     virtual void Reset() = 0;
38 
39 protected:
40     /// Returns a Property<Type> instance from underlying property
41     template<typename Type>
GetProperty()42     META_NS::ArrayProperty<Type> GetProperty() const
43     {
44         return META_NS::ArrayProperty<Type>(prop_);
45     }
GetIndex()46     META_NS::IArrayAny::IndexType GetIndex() const
47     {
48         return index_;
49     }
50 
51     /// Returns the underlying property ptr
52     META_NS::IProperty::Ptr GetPropertyPtr() const noexcept;
53 
54 private:
55     META_NS::IProperty::Ptr prop_;
56     META_NS::IArrayAny::IndexType index_;
57 };
58 
59 template<typename Type>
60 class TypeArrayProxy : public ArrayPropertyProxy {
61 public:
TypeArrayProxy(NapiApi::Object obj,META_NS::IProperty::Ptr prop,META_NS::IArrayAny::IndexType index)62     TypeArrayProxy(NapiApi::Object obj, META_NS::IProperty::Ptr prop, META_NS::IArrayAny::IndexType index)
63         : ArrayPropertyProxy(prop, index), obj_(obj)
64     {}
~TypeArrayProxy()65     ~TypeArrayProxy() override
66     {
67         Reset();
68     }
Reset()69     void Reset() override
70     {
71         if (!obj_.IsEmpty()) {
72             if (auto prop = GetPropertyPtr()) {
73                 obj_.GetObject().DeleteProperty(prop->GetName());
74             }
75         }
76         obj_.Reset();
77     }
78 
79 protected:
Value()80     napi_value Value() override
81     {
82         if (auto arrayProp = GetProperty<Type>()) {
83             auto value = arrayProp->GetValueAt(GetIndex());
84             if constexpr (BASE_NS::is_same_v<Type, BASE_NS::string>) {
85                 return NapiApi::Env(obj_.GetEnv()).GetString(value);
86             } else {
87                 return NapiApi::Env(obj_.GetEnv()).GetNumber(value);
88             }
89         }
90         return NapiApi::Env(obj_.GetEnv()).GetUndefined();
91     }
SetValue(NapiApi::FunctionContext<> & info)92     void SetValue(NapiApi::FunctionContext<>& info) override
93     {
94         NapiApi::FunctionContext<Type> data { info };
95         if (data) {
96             Type value = data.template Arg<0>();
97             auto arrayProp = GetProperty<Type>();
98             if (auto arrayProp = GetProperty<Type>()) {
99                 arrayProp->SetValueAt(GetIndex(), value);
100             }
101         }
102     }
SetValue(const Type & v)103     void SetValue(const Type& v)
104     {
105         if (auto arrayProp = GetProperty<Type>()) {
106             arrayProp->SetValueAt(GetIndex(), v);
107         }
108     }
109 
110 private:
111     NapiApi::WeakRef obj_;
112 };
113 
114 template<typename Type>
PropertyToArrayProxy(NapiApi::Object scene,NapiApi::Object obj,META_NS::ArrayProperty<Type> & t,META_NS::IArrayAny::IndexType index)115 BASE_NS::shared_ptr<ArrayPropertyProxy> PropertyToArrayProxy(NapiApi::Object scene, NapiApi::Object obj,
116     META_NS::ArrayProperty<Type>& t, META_NS::IArrayAny::IndexType index)
117 {
118     return BASE_NS::shared_ptr { new TypeArrayProxy<Type>(obj, t, index) };
119 }
120 
121 napi_property_descriptor CreateArrayProxyDesc(const char* name, BASE_NS::shared_ptr<ArrayPropertyProxy> proxy);
122 
123 #endif
124