• 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_JS_REF_PTR_H
17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_JS_REF_PTR_H
18 
19 #ifdef USE_V8_ENGINE
20 #include "frameworks/bridge/declarative_frontend/engine/v8/v8_ref.h"
21 #elif USE_QUICKJS_ENGINE
22 #include "frameworks/bridge/declarative_frontend/engine/quickjs/qjs_ref.h"
23 #elif USE_ARK_ENGINE
24 #include "frameworks/bridge/declarative_frontend/engine/jsi/jsi_ref.h"
25 #endif
26 
27 #include "frameworks/bridge/declarative_frontend/engine/js_types.h"
28 
29 namespace OHOS::Ace::Framework {
30 
31 template<typename T, typename ImplDetail>
32 class JSRefPtrImpl {
33 public:
JSRefPtrImpl()34     JSRefPtrImpl() {}
~JSRefPtrImpl()35     ~JSRefPtrImpl() {}
36 
37     // Conversion from the implementation detail to JSRefPtr
JSRefPtrImpl(const ImplDetail & rhs)38     explicit JSRefPtrImpl(const ImplDetail& rhs) : object_(rhs) {}
JSRefPtrImpl(ImplDetail && rhs)39     explicit JSRefPtrImpl(ImplDetail&& rhs) : object_(std::move(rhs)) {}
40 
JSRefPtrImpl(const JSRefPtrImpl<T,ImplDetail> & rhs)41     JSRefPtrImpl(const JSRefPtrImpl<T, ImplDetail>& rhs) : object_(rhs.object_) {}
JSRefPtrImpl(JSRefPtrImpl<T,ImplDetail> && rhs)42     JSRefPtrImpl(JSRefPtrImpl<T, ImplDetail>&& rhs) : object_(std::move(rhs.object_)) {}
43     JSRefPtrImpl<T, ImplDetail>& operator=(const JSRefPtrImpl<T, ImplDetail>& rhs)
44     {
45         object_.Reset();
46         object_ = rhs.object_;
47         return *this;
48     }
49 
50     JSRefPtrImpl<T, ImplDetail>& operator=(JSRefPtrImpl<T, ImplDetail>&& rhs)
51     {
52         object_.Reset();
53         object_ = std::move(rhs.object_);
54         return *this;
55     }
56 
57     T* operator->() const
58     {
59         return object_.template Unwrap<T>();
60     }
61 
Reset()62     void Reset()
63     {
64         object_.Reset();
65     }
66 
67     operator bool() const
68     {
69         return !object_.IsEmpty();
70     }
71 
ImplDetail()72     operator ImplDetail() const
73     {
74         return object_;
75     }
76 
Get()77     const ImplDetail& Get() const
78     {
79         return object_;
80     }
81 
82 private:
83     ImplDetail object_;
84 };
85 
86 #ifdef USE_QUICKJS_ENGINE
87 template<typename T>
88 using JSRef = QJSRef<T>;
89 template<typename T>
90 using JSWeak = QJSWeak<T>;
91 template<typename T>
92 using JSRefPtr = JSRefPtrImpl<T, QJSRef<QJSObject>>;
93 template<typename T>
94 using JSWeakPtr = JSRefPtrImpl<T, QJSWeak<QJSObject>>;
95 
96 #elif USE_V8_ENGINE
97 template<typename T>
98 using JSRef = V8Ref<T>;
99 template<typename T>
100 using JSWeak = V8Weak<T>;
101 template<typename T>
102 using JSRefPtr = JSRefPtrImpl<T, V8Ref<V8Object>>;
103 template<typename T>
104 using JSWeakPtr = JSRefPtrImpl<T, V8Weak<V8Object>>;
105 
106 #elif USE_ARK_ENGINE
107 template<typename T>
108 using JSRef = JsiRef<T>;
109 template<typename T>
110 using JSWeak = JsiWeak<T>;
111 template<typename T>
112 using JSRefPtr = JSRefPtrImpl<T, JsiRef<JsiObject>>;
113 template<typename T>
114 using JSWeakPtr = JSRefPtrImpl<T, JsiWeak<JsiObject>>;
115 #endif
116 
117 } // namespace OHOS::Ace::Framework
118 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_JS_REF_PTR
119