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_ARK_ENGINE 20 #include "frameworks/bridge/declarative_frontend/engine/jsi/jsi_ref.h" 21 #endif 22 23 #include "frameworks/bridge/declarative_frontend/engine/js_types.h" 24 25 namespace OHOS::Ace::Framework { 26 27 template<typename T, typename ImplDetail> 28 class JSRefPtrImpl { 29 public: JSRefPtrImpl()30 JSRefPtrImpl() {} ~JSRefPtrImpl()31 ~JSRefPtrImpl() {} 32 33 // Conversion from the implementation detail to JSRefPtr JSRefPtrImpl(const ImplDetail & rhs)34 explicit JSRefPtrImpl(const ImplDetail& rhs) : object_(rhs) {} JSRefPtrImpl(ImplDetail && rhs)35 explicit JSRefPtrImpl(ImplDetail&& rhs) : object_(std::move(rhs)) {} 36 JSRefPtrImpl(const JSRefPtrImpl<T,ImplDetail> & rhs)37 JSRefPtrImpl(const JSRefPtrImpl<T, ImplDetail>& rhs) : object_(rhs.object_) {} JSRefPtrImpl(JSRefPtrImpl<T,ImplDetail> && rhs)38 JSRefPtrImpl(JSRefPtrImpl<T, ImplDetail>&& rhs) : object_(std::move(rhs.object_)) {} 39 JSRefPtrImpl<T, ImplDetail>& operator=(const JSRefPtrImpl<T, ImplDetail>& rhs) 40 { 41 object_.Reset(); 42 object_ = rhs.object_; 43 return *this; 44 } 45 46 JSRefPtrImpl<T, ImplDetail>& operator=(JSRefPtrImpl<T, ImplDetail>&& rhs) 47 { 48 object_.Reset(); 49 object_ = std::move(rhs.object_); 50 return *this; 51 } 52 53 T* operator->() const 54 { 55 return object_.template Unwrap<T>(); 56 } 57 Reset()58 void Reset() 59 { 60 object_.Reset(); 61 } 62 63 operator bool() const 64 { 65 return !object_.IsEmpty(); 66 } 67 ImplDetail()68 operator ImplDetail() const 69 { 70 return object_; 71 } 72 Get()73 const ImplDetail& Get() const 74 { 75 return object_; 76 } 77 78 private: 79 ImplDetail object_; 80 }; 81 82 #ifdef USE_ARK_ENGINE 83 template<typename T> 84 using JSRef = JsiRef<T>; 85 template<typename T> 86 using JSWeak = JsiWeak<T>; 87 template<typename T> 88 using JSRefPtr = JSRefPtrImpl<T, JsiRef<JsiObject>>; 89 template<typename T> 90 using JSWeakPtr = JSRefPtrImpl<T, JsiWeak<JsiObject>>; 91 #endif 92 93 } // namespace OHOS::Ace::Framework 94 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_JS_REF_PTR 95