From 2e01842c103c06278ab04a4f766621de17919b55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Wed, 22 Nov 2017 10:42:28 +0100 Subject: [PATCH] Undo manager - Improved interface * Adding returns reference to the action * Checks for whether undo or redo are possible --- undo.cc | 8 +++++--- undo.hh | 14 ++++++++++---- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/undo.cc b/undo.cc index ef27429..db449f9 100644 --- a/undo.cc +++ b/undo.cc @@ -26,7 +26,7 @@ void T_UndoManager::redo( ) noexcept } } -void T_UndoManager::addAction( +A_UndoAction& T_UndoManager::addAction( P_UndoAction action ) noexcept { assert( action ); @@ -34,12 +34,14 @@ void T_UndoManager::addAction( count_ --; actions_[ ( start_ + count_ ) % MaxUndo ].clear( ); } + + const auto ai{ count_ == MaxUndo ? start_ : ( ( start_ + pos_ ) % MaxUndo ) }; if ( count_ == MaxUndo ) { - actions_[ start_ ] = std::move( action ); start_ = ( start_ + 1 ) % MaxUndo; } else { - actions_[ ( start_ + pos_ ) % MaxUndo ] = std::move( action ); count_ ++; pos_ ++; } + actions_[ ai ] = std::move( action ); + return *actions_[ ai ]; } diff --git a/undo.hh b/undo.hh index 0180b45..c0462ae 100644 --- a/undo.hh +++ b/undo.hh @@ -36,22 +36,28 @@ class T_UndoManager // Redo last undone action void redo( ) noexcept; + // Check if it is possible to undo or redo + bool canUndo( ) const noexcept + { return pos_ > 0; } + bool canRedo( ) const noexcept + { return pos_ < count_; } + // Add an action using a pointer - void addAction( P_UndoAction action ) noexcept; + A_UndoAction& addAction( P_UndoAction action ) noexcept; // Construct and add an action template< typename T , typename... Args - > void add( Args&&... args ) noexcept; + > A_UndoAction& add( Args&&... args ) noexcept; }; template< typename T , typename... Args -> inline void T_UndoManager::add( +> inline A_UndoAction& T_UndoManager::add( Args&&... args ) noexcept { - addAction( NewOwned< T >( std::forward< Args >( args ) ... ) ); + return addAction( NewOwned< T >( std::forward< Args >( args ) ... ) ); }