/* Copyright (c) 2023 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #include "utilities/UndoStack.h" #define stemobject_implementation UndoStack stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_end(); UndoStack * UndoStack_create(void * target) { stemobject_create_implementation(init, target) } bool UndoStack_init(UndoStack * self, void * target) { call_super(init, self); self->target = target; self->nodeCount = 0; self->nodeAllocatedCount = 0; self->nodes = NULL; self->nextNodeID = 1; self->currentDepth = 0; return true; } void UndoStack_dispose(UndoStack * self) { for (unsigned int nodeIndex = 0; nodeIndex < self->nodeCount; nodeIndex++) { call_virtual(dispose, self->nodes[nodeIndex].stateDelta); } free(self->nodes); call_super_virtual(dispose, self); } UndoStack * UndoStack_copy(UndoStack * self) { UndoStack * copy = UndoStack_create(self->target); copy->nodeCount = self->nodeCount; copy->nodeAllocatedCount = copy->nodeCount; copy->nodes = malloc(copy->nodeCount * sizeof(*copy->nodes)); for (unsigned int nodeIndex = 0; nodeIndex < copy->nodeCount; nodeIndex++) { copy->nodes[nodeIndex] = self->nodes[nodeIndex]; copy->nodes[nodeIndex].stateDelta = call_virtual(copy, self->nodes[nodeIndex].stateDelta); } copy->nextNodeID = self->nextNodeID; copy->currentDepth = self->currentDepth; return copy; } void UndoStack_appendNode(UndoStack * self, compat_type(UndoStateDelta *) stateDelta) { for (unsigned int nodeIndex = self->currentDepth; nodeIndex < self->nodeCount; nodeIndex++) { call_virtual(dispose, self->nodes[nodeIndex].stateDelta); } self->nodeCount = self->currentDepth; if (self->nodeCount >= self->nodeAllocatedCount) { self->nodeAllocatedCount = self->nodeAllocatedCount * 2 + (self->nodeAllocatedCount == 0) * 32; self->nodes = realloc(self->nodes, self->nodeAllocatedCount * sizeof(*self->nodes)); } self->nodes[self->nodeCount].uniqueID = self->nextNodeID++; self->nodes[self->nodeCount].stateDelta = stateDelta; self->currentDepth++; self->nodeCount++; } bool UndoStack_canUndo(UndoStack * self) { return self->currentDepth > 0; } bool UndoStack_canRedo(UndoStack * self) { return self->currentDepth < self->nodeCount; } UndoStateDelta * UndoStack_getStateDeltaForUndo(UndoStack * self) { if (self->currentDepth == 0) { return NULL; } return self->nodes[self->currentDepth - 1].stateDelta; } UndoStateDelta * UndoStack_getStateDeltaForRedo(UndoStack * self) { if (self->currentDepth >= self->nodeCount) { return NULL; } return self->nodes[self->currentDepth].stateDelta; } bool UndoStack_undo(UndoStack * self) { if (self->currentDepth == 0) { return false; } call_virtual(revert, self->nodes[self->currentDepth - 1].stateDelta, self->target); self->currentDepth--; return true; } bool UndoStack_redo(UndoStack * self) { if (self->currentDepth >= self->nodeCount) { return false; } call_virtual(apply, self->nodes[self->currentDepth].stateDelta, self->target); self->currentDepth++; return true; } unsigned int UndoStack_getCurrentNodeID(UndoStack * self) { if (self->currentDepth == 0 || self->currentDepth > self->nodeCount) { return 0; } return self->nodes[self->currentDepth - 1].uniqueID; } void UndoStack_removeAllNodes(UndoStack * self) { for (unsigned int nodeIndex = 0; nodeIndex < self->nodeCount; nodeIndex++) { call_virtual(dispose, self->nodes[nodeIndex].stateDelta); } self->nodeCount = 0; self->currentDepth = 0; }