demotool/c-undo.hh
2017-11-23 23:38:05 +01:00

64 lines
1.3 KiB
C++

#pragma once
#ifndef REAL_BUILD
# include "externals.hh"
#endif
// Interface for undo actions
class A_UndoAction
{
public:
virtual ~A_UndoAction( ) = 0;
virtual void undo( ) const noexcept = 0;
virtual void redo( ) const noexcept = 0;
};
using P_UndoAction = T_OwnPtr< A_UndoAction >;
// Undo manager
class T_UndoManager
{
private:
static constexpr uint32_t MaxUndo = 1024;
ebcl::T_StaticArray< P_UndoAction , MaxUndo > actions_;
uint32_t start_{ 0 };
uint32_t count_{ 0 };
uint32_t pos_{ 0 };
public:
T_UndoManager( ) noexcept;
DEF_MOVE( T_UndoManager );
NO_COPY( T_UndoManager );
// Undo last action
void undo( ) noexcept;
// 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
A_UndoAction& addAction( P_UndoAction action ) noexcept;
// Construct and add an action
template<
typename T ,
typename... Args
> T& add( Args&&... args ) noexcept;
};
template<
typename T ,
typename... Args
> inline T& T_UndoManager::add(
Args&&... args ) noexcept
{
return reinterpret_cast< T& >( addAction(
NewOwned< T >( std::forward< Args >( args ) ... ) ) );
}