/* * Copyright (c) 2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef FOUNDATION_ABILITY_RUNTIME_SIMULATOR_COMMON_BINDABLE_H #define FOUNDATION_ABILITY_RUNTIME_SIMULATOR_COMMON_BINDABLE_H #include namespace OHOS { namespace AbilityRuntime { class Runtime; class BindingObject final { public: template BindingObject(Runtime &runtime, T *ptr) : runtime_(runtime), object_(ptr, SimpleRelease) {} ~BindingObject() = default; template T *Get() { return static_cast(object_.get()); } void Reset() { object_.reset(); } void Unbind() { if (object_) { object_.release(); } } Runtime &GetRuntime() { return runtime_; } BindingObject(const BindingObject &) = delete; BindingObject &operator=(const BindingObject &) = delete; BindingObject(BindingObject &&) = delete; BindingObject &operator=(BindingObject &&) = delete; private: template static void SimpleRelease(void *ptr) { delete static_cast(ptr); } Runtime &runtime_; std::unique_ptr object_; }; class Bindable { public: virtual ~Bindable() = default; template void Bind(Runtime &runtime, T *object) { object_ = std::make_unique(runtime, object); } void Unbind() { if (object_) { object_->Unbind(); } } const std::unique_ptr &GetBindingObject() const { return object_; } protected: Bindable() = default; private: std::unique_ptr object_; }; } // namespace AbilityRuntime } // namespace OHOS #endif // FOUNDATION_ABILITY_RUNTIME_SIMULATOR_COMMON_BINDABLE_H