//****************************************************************************************************** // WeakActionFast.cs - Gbtc // // Copyright © 2013, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), 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.opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 01/16/2013 - Steven E. Chisholm // Generated original version of source code. // //****************************************************************************************************** using System; using System.Runtime.CompilerServices; namespace GSF.Threading { /// /// Provides a weak referenced action delegate. /// /// /// /// Unlike many implementations of a weak action, this class does not /// use reflection so calls will be faster. However, a strong reference /// *must* be maintained for the callback passed /// to this class. This reference is returned through an out parameter /// in the constructor. /// /// /// Careful consideration must be made when deciding where to store /// the strong reference as this strong reference will need to also /// lose reference. A good place would be as a member variable of the /// object of the target method. /// /// [Serializable] public class WeakAction : WeakReference { /// /// Creates a high speed weak action. /// /// The callback. /// A strong reference that must be maintained in the class that is the target of the action. public WeakAction(Action callback, out object localStrongReference) : base(callback) { localStrongReference = callback; } /// /// Attempts to invoke the delegate to a weak reference object. /// /// true if successful; otherwise, false. [MethodImpl(MethodImplOptions.NoInlining)] public bool TryInvoke() { Action target = Target as Action; if ((object)target == null) return false; target(); return true; } /// /// Clears callback target. /// public void Clear() { Target = null; } } /// /// Provides a weak referenced action delegate. /// /// /// /// Unlike many implementations of a weak action, this class does not /// use reflection so calls will be faster. However, a strong reference /// *must* be maintained for the callback passed /// to this class. This reference is returned through an out parameter /// in the constructor. /// /// /// Careful consideration must be made when deciding where to store /// the strong reference as this strong reference will need to also /// lose reference. A good place would be as a member variable of the /// object of the target method. /// /// [Serializable] public class WeakAction : WeakReference { /// /// Creates a high speed weak action /// /// The callback. /// A strong reference that must be maintained in the class that is the target of the action. public WeakAction(Action callback, out object localStrongReference) : base(callback) { localStrongReference = callback; } /// /// Attempts to invoke the delegate to a weak reference object. /// /// true if successful; otherwise, false. [MethodImpl(MethodImplOptions.NoInlining)] public bool TryInvoke(T args) { Action target = Target as Action; if ((object)target == null) return false; target(args); return true; } /// /// Clears callback target. /// public void Clear() { Target = null; } } }