demotool/undo.cc
Emmanuel BENOîT 2e01842c10 Undo manager - Improved interface
* Adding returns reference to the action
* Checks for whether undo or redo are possible
2017-11-22 10:42:28 +01:00

47 lines
948 B
C++

#include "externals.hh"
#include "undo.hh"
/*= A_UndoAction ===============================================================*/
A_UndoAction::~A_UndoAction( )
{}
/*= T_UndoManager ==============================================================*/
void T_UndoManager::undo( ) noexcept
{
if ( pos_ > 0 ) {
pos_ --;
actions_[ ( start_ + pos_ ) % MaxUndo ]->undo( );
}
}
void T_UndoManager::redo( ) noexcept
{
if ( pos_ < count_ ) {
pos_ ++;
actions_[ ( start_ + pos_ ) % MaxUndo ]->redo( );
}
}
A_UndoAction& T_UndoManager::addAction(
P_UndoAction action ) noexcept
{
assert( action );
while ( pos_ < count_ ) {
count_ --;
actions_[ ( start_ + count_ ) % MaxUndo ].clear( );
}
const auto ai{ count_ == MaxUndo ? start_ : ( ( start_ + pos_ ) % MaxUndo ) };
if ( count_ == MaxUndo ) {
start_ = ( start_ + 1 ) % MaxUndo;
} else {
count_ ++;
pos_ ++;
}
actions_[ ai ] = std::move( action );
return *actions_[ ai ];
}