/// <summary> /// Create an UndoService. /// </summary> /// <param name="getState">Method to get the state of the tracked object</param> /// <param name="setState">Method to set the state of the tracked object</param> /// <param name="cap">Capacity of Undo history</param> public UndoService(GetState <T> getState, SetState <T> setState, int?cap = null) { GetState = getState ?? throw new ArgumentNullException(nameof(getState)); SetState = setState ?? throw new ArgumentNullException(nameof(setState)); var stackFactory = new StackFactory <StateRecord <T> >(); _undoStack = stackFactory.MakeStack(cap); // T currentState; GetState(out T currentState); _originalState = currentState; _currentState = new StateRecord <T> { State = currentState }; _redoStack = new StandardStack <StateRecord <T> >(); _undoServiceValidator = new UndoServiceValidator <StateRecord <T> >(_undoStack, _redoStack); _undoStack.HasItemsChanged += UndoStack_HasItemsChanged; _redoStack.HasItemsChanged += RedoStack_HasItemsChanged; }
/// <summary> /// Creates an aggregate of UndoServices. /// </summary> /// <param name="subUndoServices"></param> public UndoServiceAggregate(IUndoService[] subUndoServices) { _undoStack = new IntStackWithDelete(); _redoStack = new IntStackWithDelete(); _subUndoServices = new List <SubUndoService>(); _clearStackInvocationsCount = 0; if (subUndoServices == null) { throw new ArgumentNullException(nameof(subUndoServices)); } for (var i = 0; i < subUndoServices.Length; i++) { AddUndoService(subUndoServices[i]); } _undoServiceValidator = new UndoServiceValidator <int>(_undoStack, _redoStack); _undoStack.HasItemsChanged += UndoStack_HasItemsChanged; _redoStack.HasItemsChanged += RedoStack_HasItemsChanged; }