Undo manager - Improved interface

* Adding returns reference to the action
* Checks for whether undo or redo are possible
This commit is contained in:
Emmanuel BENOîT 2017-11-22 10:42:28 +01:00
parent e11aff344f
commit 2e01842c10
2 changed files with 15 additions and 7 deletions

View file

@ -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 ];
}

14
undo.hh
View file

@ -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 ) ... ) );
}