diff options
author | thankyouverycool <66646555+thankyouverycool@users.noreply.github.com> | 2022-07-04 21:36:30 -0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-07-06 14:25:30 +0200 |
commit | 510551bb4f7c7fd325b753b5654174d4baaa5464 (patch) | |
tree | e81d183e232fc2babfc4711388104a67397805c2 | |
parent | ae333fad98734d9e99ca8cdbf11d99a9bc8a70e4 (diff) | |
download | serenity-510551bb4f7c7fd325b753b5654174d4baaa5464.zip |
LibGUI: Add fallible try_push() variant to UndoStack
-rw-r--r-- | Userland/Libraries/LibGUI/UndoStack.cpp | 13 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/UndoStack.h | 1 |
2 files changed, 11 insertions, 3 deletions
diff --git a/Userland/Libraries/LibGUI/UndoStack.cpp b/Userland/Libraries/LibGUI/UndoStack.cpp index 6ac2205648..cb273f4d37 100644 --- a/Userland/Libraries/LibGUI/UndoStack.cpp +++ b/Userland/Libraries/LibGUI/UndoStack.cpp @@ -46,7 +46,7 @@ void UndoStack::redo() on_state_change(); } -void UndoStack::push(NonnullOwnPtr<Command> command) +ErrorOr<void> UndoStack::try_push(NonnullOwnPtr<Command> command) { // If the stack cursor is behind the top of the stack, nuke everything from here to the top. while (m_stack.size() != m_stack_index) @@ -57,14 +57,21 @@ void UndoStack::push(NonnullOwnPtr<Command> command) if (!m_stack.is_empty() && is_current_modified()) { if (m_stack.last().merge_with(*command)) - return; + return {}; } - m_stack.append(move(command)); + TRY(m_stack.try_append(move(command))); m_stack_index = m_stack.size(); if (on_state_change) on_state_change(); + + return {}; +} + +void UndoStack::push(NonnullOwnPtr<Command> command) +{ + MUST(try_push(move(command))); } void UndoStack::set_current_unmodified() diff --git a/Userland/Libraries/LibGUI/UndoStack.h b/Userland/Libraries/LibGUI/UndoStack.h index ab58d140e5..4ed787975b 100644 --- a/Userland/Libraries/LibGUI/UndoStack.h +++ b/Userland/Libraries/LibGUI/UndoStack.h @@ -20,6 +20,7 @@ public: ~UndoStack() = default; void push(NonnullOwnPtr<Command>); + ErrorOr<void> try_push(NonnullOwnPtr<Command>); bool can_undo() const; bool can_redo() const; |