Example #1
0
 /// <summary>
 /// Can this state be locked by the intended owner?
 /// Return true is this entity and all underlying entities are not locked.
 /// </summary>
 public bool CanLock(ILocState owner, out ILocState lockedBy)
 {
     if ((!IsReadyForUse) || ((this.lockedBy != null) && (this.lockedBy != owner)))
     {
         lockedBy = this.lockedBy;
         return(false);
     }
     foreach (var item in UnderlyingLockableEntities)
     {
         if (!CanLockUnderlyingEntity(item, owner, out lockedBy))
         {
             return(false);
         }
     }
     lockedBy = null;
     return(true);
 }
Example #2
0
        /// <summary>
        /// Lock this state by the given owner.
        /// Also lock all underlying entities.
        /// </summary>
        public void Lock(ILocState owner)
        {
            if (owner == null)
            {
                throw new ArgumentNullException("owner");
            }

            // Lock if not already locked
            var lockedNow = new List <ILockableState>();

            if (owner != lockedBy)
            {
                var locState = (LocState)owner;
                if (lockedBy != null)
                {
                    throw new LockException("Entity already locked");
                }
                // Lock myself
                lockedBy = locState;
                locState.AddLockedEntity(this);
                lockedNow.Add(this);
            }

            // Lock underlying entities);
            foreach (var state in UnderlyingLockableEntities)
            {
                try
                {
                    lockedNow.Add(state);
                    state.Lock(owner);
                }
                catch
                {
                    // Underlying lock failed, unlock everything
                    foreach (var x in lockedNow)
                    {
                        x.Unlock(null);
                    }
                    throw;
                }
            }
            OnActualStateChanged();
        }
Example #3
0
 /// <summary>
 /// Unlock this state from the given owner.
 /// Also unlock all underlying entities except those where the given predicate returns true.
 /// </summary>
 public void Unlock(ILockableState exclusion)
 {
     if (lockedBy != null)
     {
         // Unlock underlying entities
         foreach (var item in UnderlyingLockableEntities)
         {
             item.Unlock(exclusion);
         }
         // Unlock me
         if (!ExcludeMe(exclusion))
         {
             lockedBy.RemoveLockedEntity(this);
             lockedBy = null;
         }
         OnActualStateChanged();
         AfterUnlock();
     }
 }
Example #4
0
 /// <summary>
 /// Default ctor
 /// </summary>
 public SpeedProperty(LocState owner)
 {
     this.owner = owner;
 }