1/* 2 * Copyright (c) 2023-2024 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 16package std.core; 17 18/** 19 * WeakRef - weak reference to object. 20 * A weak reference to an object is a reference that does not prevent the object 21 * from being reclaimed by the GC. 22 * @remark It's important to avoid relying on any specific GC behaviors. 23 * 24 * @tparam T Object type for weak reference 25 */ 26export final class WeakRef<T extends Object> { 27 // Reference to target object 28 private referent: T | undefined; 29 30 /** 31 * Constructs weak reference object referring to a given target object 32 * 33 * @param target - target object for weak reference 34 */ 35 public constructor(target: T) { 36 this.referent = target; 37 } 38 39 /** 40 * Returns underlying parameter 41 * 42 * @returns instance's target object, or undefined if the target object has been collected 43 */ 44 public deref(): T | undefined { 45 return this.referent; 46 } 47} 48