133eb0b6dSopenharmony_ci/* 233eb0b6dSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 333eb0b6dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 433eb0b6dSopenharmony_ci * you may not use this file except in compliance with the License. 533eb0b6dSopenharmony_ci * You may obtain a copy of the License at 633eb0b6dSopenharmony_ci * 733eb0b6dSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 833eb0b6dSopenharmony_ci * 933eb0b6dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1033eb0b6dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1133eb0b6dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1233eb0b6dSopenharmony_ci * See the License for the specific language governing permissions and 1333eb0b6dSopenharmony_ci * limitations under the License. 1433eb0b6dSopenharmony_ci */ 1533eb0b6dSopenharmony_ci 1633eb0b6dSopenharmony_ci#ifndef FOUNDATION_ACE_NAPI_NATIVE_ENGINE_ARK_FINALIZERS_PACK_H 1733eb0b6dSopenharmony_ci#define FOUNDATION_ACE_NAPI_NATIVE_ENGINE_ARK_FINALIZERS_PACK_H 1833eb0b6dSopenharmony_ci 1933eb0b6dSopenharmony_ci#include "interfaces/inner_api/napi/native_node_api.h" 2033eb0b6dSopenharmony_ci 2133eb0b6dSopenharmony_ciclass NativeEngine; 2233eb0b6dSopenharmony_ci 2333eb0b6dSopenharmony_ciusing RefFinalizer = std::pair<NapiNativeFinalize, std::tuple<NativeEngine*, void*, void*>>; 2433eb0b6dSopenharmony_ciusing ArkFinalizersPackFinishNotify = std::function<void(size_t totalNativeBindingSize)>; 2533eb0b6dSopenharmony_ci 2633eb0b6dSopenharmony_ciclass ArkFinalizersPack { 2733eb0b6dSopenharmony_cipublic: 2833eb0b6dSopenharmony_ci ArkFinalizersPack() = default; 2933eb0b6dSopenharmony_ci ~ArkFinalizersPack() = default; 3033eb0b6dSopenharmony_ci DEFAULT_MOVE_SEMANTIC(ArkFinalizersPack); 3133eb0b6dSopenharmony_ci DEFAULT_COPY_SEMANTIC(ArkFinalizersPack); 3233eb0b6dSopenharmony_ci 3333eb0b6dSopenharmony_ci void Clear() 3433eb0b6dSopenharmony_ci { 3533eb0b6dSopenharmony_ci finalizers_.clear(); 3633eb0b6dSopenharmony_ci totalNativeBindingSize_ = 0; 3733eb0b6dSopenharmony_ci notify_ = nullptr; 3833eb0b6dSopenharmony_ci } 3933eb0b6dSopenharmony_ci bool Empty() const 4033eb0b6dSopenharmony_ci { 4133eb0b6dSopenharmony_ci return finalizers_.empty(); 4233eb0b6dSopenharmony_ci } 4333eb0b6dSopenharmony_ci void RegisterFinishNotify(ArkFinalizersPackFinishNotify notify) 4433eb0b6dSopenharmony_ci { 4533eb0b6dSopenharmony_ci notify_ = notify; 4633eb0b6dSopenharmony_ci } 4733eb0b6dSopenharmony_ci size_t GetNumFinalizers() const 4833eb0b6dSopenharmony_ci { 4933eb0b6dSopenharmony_ci return finalizers_.size(); 5033eb0b6dSopenharmony_ci } 5133eb0b6dSopenharmony_ci void ProcessAll() const 5233eb0b6dSopenharmony_ci { 5333eb0b6dSopenharmony_ci for (auto &iter : finalizers_) { 5433eb0b6dSopenharmony_ci NapiNativeFinalize callback = iter.first; 5533eb0b6dSopenharmony_ci auto &[p0, p1, p2] = iter.second; 5633eb0b6dSopenharmony_ci callback(reinterpret_cast<napi_env>(p0), p1, p2); 5733eb0b6dSopenharmony_ci } 5833eb0b6dSopenharmony_ci NotifyFinish(); 5933eb0b6dSopenharmony_ci } 6033eb0b6dSopenharmony_ci size_t GetTotalNativeBindingSize() const 6133eb0b6dSopenharmony_ci { 6233eb0b6dSopenharmony_ci return totalNativeBindingSize_; 6333eb0b6dSopenharmony_ci } 6433eb0b6dSopenharmony_ci void AddFinalizer(RefFinalizer &finalizer, size_t nativeBindingSize) 6533eb0b6dSopenharmony_ci { 6633eb0b6dSopenharmony_ci finalizers_.emplace_back(finalizer); 6733eb0b6dSopenharmony_ci totalNativeBindingSize_ += nativeBindingSize; 6833eb0b6dSopenharmony_ci } 6933eb0b6dSopenharmony_ciprivate: 7033eb0b6dSopenharmony_ci void NotifyFinish() const 7133eb0b6dSopenharmony_ci { 7233eb0b6dSopenharmony_ci if (notify_ != nullptr) { 7333eb0b6dSopenharmony_ci notify_(totalNativeBindingSize_); 7433eb0b6dSopenharmony_ci } 7533eb0b6dSopenharmony_ci } 7633eb0b6dSopenharmony_ci std::vector<RefFinalizer> finalizers_ {}; 7733eb0b6dSopenharmony_ci size_t totalNativeBindingSize_ {0}; 7833eb0b6dSopenharmony_ci ArkFinalizersPackFinishNotify notify_ {nullptr}; 7933eb0b6dSopenharmony_ci}; 8033eb0b6dSopenharmony_ci 8133eb0b6dSopenharmony_ci#endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_ARK_FINALIZERS_PACK_H */ 82