/
redgpu
/
ezEngine
Обзор
Документация
Войти
/
redgpu
/
ezEngine
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
dev
Code/Tools/Libs/ToolsFoundation/Command/Implementation/Command.cpp
148 строк
4 KB
Jan Krassnigg
Small cleanup of ezStatus (#1595)
04 июл 2025, 08:50
Не верифицирован
04 июл 2025, 08:50
416c7ad
Код
Авторство
О чём код?
#include <ToolsFoundation/ToolsFoundationPCH.h> #include <Foundation/IO/MemoryStream.h> #include <Foundation/Serialization/ReflectionSerializer.h> #include <ToolsFoundation/Command/Command.h> #include <ToolsFoundation/CommandHistory/CommandHistory.h> #include <ToolsFoundation/Document/Document.h> EZ_BEGIN_DYNAMIC_REFLECTED_TYPE(ezCommand, 1, ezRTTINoAllocator) EZ_END_DYNAMIC_REFLECTED_TYPE; ezCommand::ezCommand() = default; ezCommand::~ezCommand() = default; bool ezCommand::HasModifiedDocument() const { if (m_bModifiedDocument) return true; for (const auto& ca : m_ChildActions) { if (ca->HasModifiedDocument()) return true; } return false; } ezStatus ezCommand::Do(bool bRedo) { ezStatus status = DoInternal(bRedo); if (status.Failed()) { if (bRedo) { // A command that originally succeeded failed on redo! return status; } else { for (ezInt32 j = m_ChildActions.GetCount() - 1; j >= 0; --j) { ezStatus status2 = m_ChildActions[j]->Undo(true); EZ_ASSERT_DEV(status2.Succeeded(), "Failed do could not be recovered! Inconsistent state!"); } return status; } } if (!bRedo) return EZ_SUCCESS; const ezUInt32 uiChildActions = m_ChildActions.GetCount(); for (ezUInt32 i = 0; i < uiChildActions; ++i) { status = m_ChildActions[i]->Do(bRedo); if (status.Failed()) { for (ezInt32 j = i - 1; j >= 0; --j) { ezStatus status2 = m_ChildActions[j]->Undo(true); EZ_ASSERT_DEV(status2.Succeeded(), "Failed redo could not be recovered! Inconsistent state!"); } // A command that originally succeeded failed on redo! return status; } } return EZ_SUCCESS; } ezStatus ezCommand::Undo(bool bFireEvents) { const ezUInt32 uiChildActions = m_ChildActions.GetCount(); for (ezInt32 i = uiChildActions - 1; i >= 0; --i) { ezStatus status = m_ChildActions[i]->Undo(bFireEvents); if (status.Failed()) { for (ezUInt32 j = i + 1; j < uiChildActions; ++j) { ezStatus status2 = m_ChildActions[j]->Do(true); EZ_ASSERT_DEV(status2.Succeeded(), "Failed undo could not be recovered! Inconsistent state!"); } // A command that originally succeeded failed on undo! return status; } } ezStatus status = UndoInternal(bFireEvents); if (status.Failed()) { for (ezUInt32 j = 0; j < uiChildActions; ++j) { ezStatus status2 = m_ChildActions[j]->Do(true); EZ_ASSERT_DEV(status2.Succeeded(), "Failed undo could not be recovered! Inconsistent state!"); } // A command that originally succeeded failed on undo! return status; } return EZ_SUCCESS; } void ezCommand::Cleanup(CommandState state) { CleanupInternal(state); for (ezCommand* pCommand : m_ChildActions) { pCommand->Cleanup(state); pCommand->GetDynamicRTTI()->GetAllocator()->Deallocate(pCommand); } m_ChildActions.Clear(); } ezStatus ezCommand::AddSubCommand(ezCommand& command) { ezCommand* pCommand = ezReflectionSerializer::Clone(&command); const ezRTTI* pRtti = pCommand->GetDynamicRTTI(); pCommand->m_pDocument = m_pDocument; m_ChildActions.PushBack(pCommand); m_pDocument->GetCommandHistory()->GetStorage()->m_ActiveCommandStack.PushBack(pCommand); ezStatus ret = pCommand->Do(false); m_pDocument->GetCommandHistory()->GetStorage()->m_ActiveCommandStack.PopBack(); if (ret.Failed()) { m_ChildActions.PopBack(); pCommand->GetDynamicRTTI()->GetAllocator()->Deallocate(pCommand); return ret; } if (pCommand->HasReturnValues()) { // Write properties back so any return values get written. ezDefaultMemoryStreamStorage storage; ezMemoryStreamWriter writer(&storage); ezMemoryStreamReader reader(&storage); ezReflectionSerializer::WriteObjectToBinary(writer, pCommand->GetDynamicRTTI(), pCommand); ezReflectionSerializer::ReadObjectPropertiesFromBinary(reader, *pRtti, &command); } return EZ_SUCCESS; }