Skip to content

Commit

Permalink
MemoryCallbackSystem: fix #1159
Browse files Browse the repository at this point in the history
  • Loading branch information
gifvex committed Apr 2, 2018
1 parent f968cbd commit f48a795
Showing 1 changed file with 11 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ public MemoryCallbackSystem(string[] availableScopes)
private readonly ObservableCollection<IMemoryCallback> _writes = new ObservableCollection<IMemoryCallback>();
private readonly ObservableCollection<IMemoryCallback> _execs = new ObservableCollection<IMemoryCallback>();

private bool _empty = true;

private bool _hasReads;
private bool _hasWrites;
private bool _hasExecutes;
Expand All @@ -54,24 +52,19 @@ public void Add(IMemoryCallback callback)
{
case MemoryCallbackType.Execute:
_execs.Add(callback);
_hasExecutes = true;
break;
case MemoryCallbackType.Read:
_reads.Add(callback);
_hasReads = true;
break;
case MemoryCallbackType.Write:
_writes.Add(callback);
_hasWrites = true;
break;
}

if (_empty)
if (UpdateHasVariables())
{
Changes();
}

_empty = false;
}

private static void Call(ObservableCollection<IMemoryCallback> cbs, uint addr, string scope)
Expand Down Expand Up @@ -130,11 +123,17 @@ public bool HasExecutesForScope(string scope)
return _execs.Where(e => e.Scope == scope).Any();
}

private void UpdateHasVariables()
private bool UpdateHasVariables()
{
bool hadReads = _hasReads;
bool hadWrites = _hasWrites;
bool hadExecutes = _hasExecutes;

_hasReads = _reads.Count > 0;
_hasWrites = _writes.Count > 0;
_hasExecutes = _execs.Count > 0;

return (_hasReads != hadReads || _hasWrites != hadWrites || _hasExecutes != hadExecutes);
}

private int RemoveInternal(Action action)
Expand All @@ -158,22 +157,17 @@ private int RemoveInternal(Action action)
_execs.Remove(exec);
}

UpdateHasVariables();

return readsToRemove.Count + writesToRemove.Count + execsToRemove.Count;
}

public void Remove(Action action)
{
if (RemoveInternal(action) > 0)
{
bool newEmpty = !HasReads && !HasWrites && !HasExecutes;
if (newEmpty != _empty)
if (UpdateHasVariables())
{
Changes();
}

_empty = newEmpty;
}
}

Expand All @@ -187,16 +181,11 @@ public void RemoveAll(IEnumerable<Action> actions)

if (changed)
{
bool newEmpty = !HasReads && !HasWrites && !HasExecutes;
if (newEmpty != _empty)
if (UpdateHasVariables())
{
Changes();
}

_empty = newEmpty;
}

UpdateHasVariables();
}

public void Clear()
Expand All @@ -217,14 +206,10 @@ public void Clear()
_execs.RemoveAt(i);
}

if (!_empty)
if (UpdateHasVariables())
{
Changes();
}

_empty = true;

UpdateHasVariables();
}

public delegate void ActiveChangedEventHandler();
Expand Down

0 comments on commit f48a795

Please sign in to comment.