7 #include "fwCommand/UndoRedoManager.hpp" 17 m_maxMemory(maxMemory),
18 m_maxCommands(maxCommands == 0 ? 1 : maxCommands),
22 SLM_ASSERT(
"The number of commands must be greater than 0", maxCommands > 0);
31 SLM_WARN(
"Cannot add a command because maxMemory is 0.");
35 if(cmd->getSize() > m_maxMemory)
37 SLM_WARN(
"The current command is bigger than the maximum history size");
42 if(!m_commandQueue.empty())
44 for(std::int64_t i = m_commandQueue.size() - 1; i > m_commandIndex; --i)
46 m_usedMemory -= m_commandQueue[i]->getSize();
47 m_commandQueue.pop_back();
52 if(m_maxCommands == m_commandQueue.size())
58 while(m_usedMemory + cmd->getSize() > m_maxMemory)
63 m_commandQueue.push_back(cmd);
64 m_usedMemory += cmd->getSize();
65 m_commandIndex =
static_cast<std::int64_t
>(m_commandQueue.size() - 1);
76 if(m_commandIndex != m_commandQueue.size() - 1)
79 success = m_commandQueue[m_commandIndex]->redo();
91 if(m_commandIndex > -1)
93 success = m_commandQueue[m_commandIndex]->undo();
104 return m_commandIndex > -1;
118 m_commandQueue.clear();
127 return m_commandQueue.size();
135 m_maxCommands = cmdCount;
150 m_maxMemory = histSize;
155 void UndoRedoManager::popFront()
157 CommandHistoryType::iterator it = m_commandQueue.begin();
159 m_usedMemory -= (*it)->getSize();
160 m_commandQueue.pop_front();
FWCOMMAND_API size_t getHistorySize() const
Returns the amount of memory used by the history.
FWCOMMAND_API bool canUndo() const
Return true if we can undo.
FWCOMMAND_API bool enqueue(ICommand::sptr cmd)
Push a command to the history.
FWCOMMAND_API size_t getCommandCount() const
Get the number of enqueued commands.
FWCOMMAND_API UndoRedoManager(size_t maxMemory=std::numeric_limits< size_t >::max(), size_t maxCommands=std::numeric_limits< size_t >::max())
Default constructor.
#define SLM_WARN(message)
FWCOMMAND_API void setCommandCount(size_t cmdCount)
Set the maximum number of enqueued commands.
FWCOMMAND_API bool undo()
Execute the previous command if any.
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
The namespace fwCommand contains classes describing and managing commands.
FWCOMMAND_API bool canRedo() const
Return true if we can redo.
FWCOMMAND_API bool redo()
Execute the next command if any.
FWCOMMAND_API void clear()
Remove all commands in history.
This file defines SpyLog macros. These macros are used to log messages to a file or to the console du...
FWCOMMAND_API void setHistorySize(size_t histSize)
Set the maximum amount of memory used by the history.