private bool SetValueFast(object?value, DependencyPropertyValuePrecedences precedence)
        {
            if (_stack == null && precedence == DependencyPropertyValuePrecedences.Local)
            {
                var valueIsUnsetValue = value is UnsetValue;

                if (HasWeakStorage)
                {
                    if (_fastLocalValue is ManagedWeakReference mwr2)
                    {
                        WeakReferencePool.ReturnWeakReference(this, mwr2);
                    }

                    _fastLocalValue = Validate(value);
                }
                else
                {
                    _fastLocalValue = ValidateNoWrap(value);
                }

                _highestPrecedence = valueIsUnsetValue
                                        ? DependencyPropertyValuePrecedences.DefaultValue
                                        : DependencyPropertyValuePrecedences.Local;

                return(true);
            }

            return(false);
        }
Пример #2
0
        /// <summary>
        /// Sets the value at the given level in the stack
        /// </summary>
        /// <param name="value">The value to set</param>
        /// <param name="precedence">The precedence level to set the value at</param>
        internal void SetValue(object value, DependencyPropertyValuePrecedences precedence)
        {
            if (_hasWeakStorage && _stack[(int)precedence] is ManagedWeakReference mwr)
            {
                WeakReferencePool.ReturnWeakReference(this, mwr);
            }

            _stack[(int)precedence] = Validate(value);

            // After setting the value, we need to update the current highest precedence if needed
            // If a higher value precedence was set, then this is the new highest
            if (!(value is UnsetValue) && precedence < _highestPrecedence)
            {
                _highestPrecedence = precedence;
                return;
            }

            // If we were unsetting the current highest precedence value, we need to find the next highest
            if (value is UnsetValue && precedence == _highestPrecedence)
            {
                // Start from current precedence and find next highest
                for (int i = (int)precedence; i < (int)DependencyPropertyValuePrecedences.DefaultValue; i++)
                {
                    if (_stack[i] != DependencyProperty.UnsetValue)
                    {
                        _highestPrecedence = (DependencyPropertyValuePrecedences)i;
                        return;
                    }
                }

                _highestPrecedence = DependencyPropertyValuePrecedences.DefaultValue;
            }
        }
Пример #3
0
        public void When_Null_Target()
        {
            var mr1 = WeakReferencePool.RentWeakReference(this, null);

            WeakReferencePool.ReturnWeakReference(this, mr1);
            Assert.AreEqual(1, WeakReferencePool.PooledReferences);
        }
Пример #4
0
        internal void SetDirectManipulationStateChangeHandler(IDirectManipulationStateChangeHandler?handler)
        {
            _directManipulationHandlerSubscription?.Dispose();

            if (handler is null)
            {
                return;
            }

            var weakHandler = WeakReferencePool.RentWeakReference(this, handler);

            UpdatesMode  = ScrollViewerUpdatesMode.Synchronous;
            ViewChanged += OnViewChanged;
            _directManipulationHandlerSubscription = Disposable.Create(() =>
            {
                ViewChanged -= OnViewChanged;
                WeakReferencePool.ReturnWeakReference(this, weakHandler);
            });

            void OnViewChanged(object?sender, ScrollViewerViewChangedEventArgs args)
            {
                if (args.IsIntermediate)
                {
                    return;
                }

                if (weakHandler.Target is IDirectManipulationStateChangeHandler h)
                {
                    h.NotifyStateChange(DMManipulationState.DMManipulationCompleted, default, default, default, default, default, default, default, default);
Пример #5
0
        public void When_Null_Target_And_Owner()
        {
            var o1 = new Object();

            var mr1 = WeakReferencePool.RentWeakReference(null, null);

            WeakReferencePool.ReturnWeakReference(null, mr1);
            Assert.AreEqual(0, WeakReferencePool.PooledReferences);
        }
Пример #6
0
        public void When_WeakReferenceProvider_Collected()
        {
            var o1 = new Object();

            void test1()
            {
                var mr1 = WeakReferencePool.RentWeakReference(this, o1);

                WeakReferencePool.ReturnWeakReference(this, mr1);
            }

            test1();

            Assert.AreEqual(2, WeakReferencePool.PooledReferences);

            void test2()
            {
                var mr2 = WeakReferencePool.RentWeakReference(this, o1);

                WeakReferencePool.ReturnWeakReference(this, mr2);
            }

            test2();

            Assert.AreEqual(2, WeakReferencePool.PooledReferences);

            (WeakReference refref, WeakReference ownerRef, WeakReference targetRef) CreateUnreferenced()
            {
                var o2  = new Object();
                var mr2 = WeakReferencePool.RentWeakReference(o1, o2);

                return(new WeakReference(mr2), new WeakReference(mr2.GetUnsafeOwnerHandle()), new WeakReference(mr2.GetUnsafeTargetHandle()));
            }

            var r = CreateUnreferenced();

            Assert.IsNotNull(r.ownerRef.Target);
            Assert.IsNotNull(r.targetRef.Target);
            Assert.IsNotNull(r.refref.Target);

            Assert.AreEqual(0, WeakReferencePool.PooledReferences);

            GCCondition(5, () => r.refref.Target == null);

            Assert.IsNull(r.refref.Target);

            GCCondition(5, () => r.ownerRef.Target == null && r.targetRef.Target == null);

            Assert.IsNull(r.ownerRef.Target);
            Assert.IsNull(r.targetRef.Target);

            Assert.AreEqual(0, WeakReferencePool.PooledReferences);
        }
Пример #7
0
        public void When_WeakReferenceProvider_Reused()
        {
            var o1 = new Object();

            var mr1 = WeakReferencePool.RentWeakReference(this, o1);

            WeakReferencePool.ReturnWeakReference(this, mr1);

            var target = new MyProvider();

            Assert.AreEqual(target, target.WeakReference.Target);

            var mr2 = WeakReferencePool.RentWeakReference(target, o1);

            WeakReferencePool.ReturnWeakReference(target, mr2);

            Assert.AreEqual(target, target.WeakReference.Target);
        }
        private void SetValueFull(object?value, DependencyPropertyValuePrecedences precedence)
        {
            var valueIsUnsetValue = value is UnsetValue;

            var stackAlias = Stack;

            if (HasWeakStorage)
            {
                if (stackAlias[(int)precedence] is ManagedWeakReference mwr)
                {
                    WeakReferencePool.ReturnWeakReference(this, mwr);
                }

                stackAlias[(int)precedence] = Validate(value);
            }
            else
            {
                stackAlias[(int)precedence] = ValidateNoWrap(value);
            }

            // After setting the value, we need to update the current highest precedence if needed
            // If a higher value precedence was set, then this is the new highest
            if (!valueIsUnsetValue && precedence < _highestPrecedence)
            {
                _highestPrecedence = precedence;
                return;
            }

            // If we were unsetting the current highest precedence value, we need to find the next highest
            if (valueIsUnsetValue && precedence == _highestPrecedence)
            {
                // Start from current precedence and find next highest
                for (int i = (int)precedence; i < (int)DependencyPropertyValuePrecedences.DefaultValue; i++)
                {
                    if (stackAlias[i] != DependencyProperty.UnsetValue)
                    {
                        _highestPrecedence = (DependencyPropertyValuePrecedences)i;
                        return;
                    }
                }

                _highestPrecedence = DependencyPropertyValuePrecedences.DefaultValue;
            }
        }
Пример #9
0
        public void When_Not_Owned_Returned()
        {
            var target1 = new object();
            var target2 = new object();

            var mr1 = WeakReferencePool.RentWeakReference(target1, target1);

            var wr = mr1.GetUnsafeTargetHandle();

            Assert.AreEqual(target1, mr1.GetUnsafeTargetHandle().Target);
            Assert.AreEqual(target1, mr1.Owner);

            WeakReferencePool.ReturnWeakReference(this, mr1);

            var mr2 = WeakReferencePool.RentWeakReference(this, target2);

            Assert.AreEqual(target2, mr2.GetUnsafeTargetHandle().Target);
            Assert.AreEqual(this, mr2.Owner);
            Assert.AreNotSame(wr, mr2.GetUnsafeTargetHandle());
        }