• 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 #ifndef FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_QUICKJS_QJS_REF_H
17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_QUICKJS_QJS_REF_H
18 
19 #include "frameworks/bridge/declarative_frontend/engine/quickjs/qjs_types.h"
20 
21 namespace OHOS::Ace::Framework {
22 
23 template<typename T>
24 class QJSRef {
25 public:
26     using wrappedT = JSValue;
27 
QJSRef()28     QJSRef() {}
29 
QJSRef(const T & rhs)30     explicit QJSRef(const T& rhs) : value_(rhs)
31     {
32         value_.Dup();
33     }
34 
35     template<typename S>
QJSRef(const QJSRef<S> & that)36     QJSRef(const QJSRef<S>& that) : value_(that.Get().GetHandle())
37     {
38         value_.Dup();
39     }
40 
QJSRef(const QJSWeak<T> & rhs)41     explicit QJSRef(const QJSWeak<T>& rhs)
42     {
43         *this = rhs.Lock();
44     }
45 
46     template<typename... Args>
Make(Args &&...args)47     static QJSRef<T> Make(Args&&... args)
48     {
49         return QJSRef<T>(T { std::forward<Args>(args)... });
50     }
51 
52     template<typename Arg>
Claim(Arg && val)53     static QJSRef<T> Claim(Arg&& val)
54     {
55         QJSRef<T> newVal;
56         newVal = T { std::forward<Arg>(val) };
57         return newVal;
58     }
59 
60     template<typename... Args>
New(Args &&...args)61     static QJSRef<T> New(Args&&... args)
62     {
63         return QJSRef<T>::Make(T::New(std::forward<Args>(args)...));
64     }
65 
66     template<typename S>
Cast(const QJSRef<S> & that)67     static QJSRef<T> Cast(const QJSRef<S>& that)
68     {
69         return QJSRef<T>(that);
70     }
71 
~QJSRef()72     ~QJSRef()
73     {
74         value_.Free();
75     }
76 
QJSRef(const QJSRef<T> & rhs)77     QJSRef(const QJSRef<T>& rhs) : value_(rhs.value_)
78     {
79         value_.Dup();
80     }
81 
QJSRef(QJSRef<T> && rhs)82     QJSRef(QJSRef<T>&& rhs) : value_(std::move(rhs.value_))
83     {
84         rhs.value_.Reset();
85     }
86 
87     QJSRef<T>& operator=(const QJSRef<T>& rhs)
88     {
89         value_.Free();
90         value_ = rhs.value_;
91         value_.Dup();
92         return *this;
93     }
94 
95     QJSRef<T>& operator=(QJSRef<T>&& rhs)
96     {
97         value_.Free();
98         value_ = std::move(rhs.value_);
99         rhs.value_.Reset();
100         return *this;
101     }
102 
IsEmpty()103     bool IsEmpty() const
104     {
105         return value_.IsEmpty();
106     }
107 
Reset()108     void Reset()
109     {
110         value_.Free();
111     }
112 
113     template<typename U>
Unwrap()114     typename std::enable_if_t<std::is_same_v<T, QJSObject>, U*> Unwrap() const
115     {
116         return value_.template Unwrap<U>();
117     }
118 
Get()119     T Get() const
120     {
121         return value_;
122     }
123 
124     const T& operator->() const
125     {
126         return value_;
127     }
128 
129 private:
130     QJSRef<T>& operator=(const T& val)
131     {
132         value_.Free();
133         value_ = val;
134         return *this;
135     }
136 
137     T value_;
138 };
139 
140 template<typename T>
141 class QJSWeak {
142 public:
143     using wrappedT = JSValue;
144 
QJSWeak()145     QJSWeak() {}
146 
~QJSWeak()147     ~QJSWeak() {}
148 
QJSWeak(const QJSWeak<T> & rhs)149     QJSWeak(const QJSWeak<T>& rhs)
150     {
151         value_ = rhs.value_;
152     }
153 
QJSWeak(const QJSRef<T> & rhs)154     explicit QJSWeak(const QJSRef<T>& rhs) : value_(rhs.Get()) {}
155 
QJSWeak(QJSWeak<T> && rhs)156     QJSWeak(QJSWeak<T>&& rhs) : value_(std::move(rhs)) {}
157 
158     QJSWeak<T>& operator=(const QJSWeak<T>& rhs)
159     {
160         value_ = rhs.value_;
161         return *this;
162     }
163 
164     QJSWeak& operator=(const QJSRef<T>& rhs)
165     {
166         value_ = rhs.Get();
167         return *this;
168     }
169 
170     QJSWeak<T>& operator=(QJSWeak<T>&& rhs)
171     {
172         value_ = std::move(rhs.value_);
173         return *this;
174     }
175 
IsEmpty()176     bool IsEmpty() const
177     {
178         return value_.IsEmpty();
179     }
180 
Reset()181     void Reset()
182     {
183         value_.Reset();
184     }
185 
Lock()186     QJSRef<T> Lock()
187     {
188         return QJSRef<T>(value_);
189     }
190 
191 private:
192     T value_;
193 };
194 
195 } // namespace OHOS::Ace::Framework
196 #endif
197