From 5bb3c54840a68473c44983cba212fc82e69a9e4f Mon Sep 17 00:00:00 2001 From: Tian Liao Date: Wed, 16 Oct 2024 15:36:42 +0800 Subject: [PATCH] stash changes --- src/CalcViewModel/Snapshots.cpp | 90 +++++++++++++++++-- src/CalcViewModel/Snapshots.h | 35 ++++---- .../StandardCalculatorViewModel.cpp | 1 + 3 files changed, 104 insertions(+), 22 deletions(-) diff --git a/src/CalcViewModel/Snapshots.cpp b/src/CalcViewModel/Snapshots.cpp index 1af643c9..b238afce 100644 --- a/src/CalcViewModel/Snapshots.cpp +++ b/src/CalcViewModel/Snapshots.cpp @@ -3,26 +3,102 @@ #include "pch.h" #include +#include #include "Snapshots.h" +namespace +{ + ref struct UnaryCommand sealed : public CalculatorApp::ViewModel::ICalcManagerIExprCommand + { + property Windows::Foundation::Collections::IVector ^ Commands; + }; + + ref struct BinaryCommand sealed : public CalculatorApp::ViewModel::ICalcManagerIExprCommand + { + property int Command; + }; + + ref struct OperandCommand sealed : public CalculatorApp::ViewModel::ICalcManagerIExprCommand + { + property bool IsNegative; + property bool IsDecimalPresent; + property bool IsSciFmt; + property Windows::Foundation::Collections::IVector ^ Commands; + }; + + ref struct Parentheses sealed : public CalculatorApp::ViewModel::ICalcManagerIExprCommand + { + property int Command; + }; + + CalculatorApp::ViewModel::ICalcManagerIExprCommand ^ CreateExprCommand(const IExpressionCommand* exprCmd) { + switch (exprCmd->GetCommandType()) + { + case CalculationManager::CommandType::UnaryCommand: + { + auto cmd = static_cast(exprCmd); + auto result = ref new UnaryCommand(); + result->Commands = ref new Platform::Collections::Vector(); + for (auto& subcmd : *cmd->GetCommands()) + { + result->Commands->Append(subcmd); + } + return result; + } + case CalculationManager::CommandType::BinaryCommand: + { + auto cmd = static_cast(exprCmd); + auto result = ref new BinaryCommand(); + result->Command = cmd->GetCommand(); + return result; + } + case CalculationManager::CommandType::OperandCommand: + { + auto cmd = static_cast(exprCmd); + auto result = ref new OperandCommand(); + result->IsNegative = cmd->IsNegative(); + result->IsDecimalPresent = cmd->IsDecimalPresent(); + result->IsSciFmt = cmd->IsSciFmt(); + result->Commands = ref new Platform::Collections::Vector(); + for (auto& subcmd : *cmd->GetCommands()) + { + result->Commands->Append(subcmd); + } + return result; + } + case CalculationManager::CommandType::Parentheses: + { + auto cmd = static_cast(exprCmd); + auto result = ref new Parentheses(); + result->Command = cmd->GetCommand(); + return result; + } + default: + throw std::logic_error{ "unhandled command type." }; + } + } +} // namespace + namespace CalculatorApp::ViewModel { - CalcEngineHistoryToken::CalcEngineHistoryToken(Platform::String ^ opCodeName, int cmdIndex) + CalcManagerHistoryToken::CalcManagerHistoryToken(Platform::String ^ opCodeName, int cmdIndex) { assert(opCodeName != nullptr && "opCodeName is mandatory."); OpCodeName = opCodeName; CommandIndex = cmdIndex; } - CalcEnginHistoryItem::CalcEnginHistoryItem(const CalculationManager::HISTORYITEM& item) + CalcManagerHistoryItem::CalcManagerHistoryItem(const CalculationManager::HISTORYITEM& item) { - Tokens = ref new Platform::Collections::Vector(); + Tokens = ref new Platform::Collections::Vector(); + Commands = ref new Platform::Collections::Vector(); if (item.historyItemVector.spTokens != nullptr) { for (auto& [opCode, cmdIdx] : *item.historyItemVector.spTokens) { - Tokens->Append(ref new CalcEngineHistoryToken(ref new Platform::String(opCode.c_str()), cmdIdx)); + Tokens->Append(ref new CalcManagerHistoryToken(ref new Platform::String(opCode.c_str()), cmdIdx)); + // Commands->Append(); } } Expression = ref new Platform::String(item.historyItemVector.expression.c_str()); @@ -36,7 +112,7 @@ namespace CalculatorApp::ViewModel { for (auto& item : items) { - HistoryItems->Append(ref new CalcEnginHistoryItem(*item)); + HistoryItems->Append(ref new CalcManagerHistoryItem(*item)); } } } @@ -50,10 +126,10 @@ namespace CalculatorApp::ViewModel ExpressionDisplaySnapshot::ExpressionDisplaySnapshot(const std::vector& tokens) { - Tokens = ref new Platform::Collections::Vector(); + Tokens = ref new Platform::Collections::Vector(); for (auto& [opCode, cmdIdx] : tokens) { - Tokens->Append(ref new CalcEngineHistoryToken(ref new Platform::String(opCode.c_str()), cmdIdx)); + Tokens->Append(ref new CalcManagerHistoryToken(ref new Platform::String(opCode.c_str()), cmdIdx)); } } } diff --git a/src/CalcViewModel/Snapshots.h b/src/CalcViewModel/Snapshots.h index 91ee1a44..ab62dda0 100644 --- a/src/CalcViewModel/Snapshots.h +++ b/src/CalcViewModel/Snapshots.h @@ -7,31 +7,36 @@ namespace CalculatorApp::ViewModel { public - ref struct CalcEngineHistoryToken sealed + interface struct ICalcManagerIExprCommand + { + }; + +public + ref struct CalcManagerHistoryToken sealed { property Platform::String ^ OpCodeName; // mandatory property int CommandIndex; internal :; - explicit CalcEngineHistoryToken(Platform::String ^ opCodeName, int cmdIndex); + explicit CalcManagerHistoryToken(Platform::String ^ opCodeName, int cmdIndex); }; public - ref struct CalcEnginHistoryItem sealed + ref struct CalcManagerHistoryItem sealed { - property Windows::Foundation::Collections::IVector ^ Tokens; // mandatory - // TODO: commands - property Platform::String ^ Expression; // mandatory - property Platform::String ^ Result; // mandatory + property Windows::Foundation::Collections::IVector ^ Tokens; // mandatory + property Windows::Foundation::Collections::IVector ^ Commands; // mandatory + property Platform::String ^ Expression; // mandatory + property Platform::String ^ Result; // mandatory internal :; - explicit CalcEnginHistoryItem(const CalculationManager::HISTORYITEM& item); + explicit CalcManagerHistoryItem(const CalculationManager::HISTORYITEM& item); }; public ref struct CalcManagerSnapshot sealed { - property Windows::Foundation::Collections::IVector ^ HistoryItems; // optional + property Windows::Foundation::Collections::IVector ^ HistoryItems; // optional internal :; explicit CalcManagerSnapshot(const CalculationManager::CalculatorManager& calcMgr); @@ -50,7 +55,7 @@ public public ref struct ExpressionDisplaySnapshot sealed { - property Windows::Foundation::Collections::IVector ^ Tokens; + property Windows::Foundation::Collections::IVector ^ Tokens; // TODO: commands internal :; @@ -61,9 +66,9 @@ public public ref struct StandardCalculatorSnapshot sealed { - property CalcManagerSnapshot ^ CalcManager; // mandatory - property PrimaryDisplaySnapshot ^ PrimaryDisplay; // mandatory - property ExpressionDisplaySnapshot ^ ExpressionDisplay; // optional - // TODO: commands + property CalcManagerSnapshot ^ CalcManager; // mandatory + property PrimaryDisplaySnapshot ^ PrimaryDisplay; // mandatory + property ExpressionDisplaySnapshot ^ ExpressionDisplay; // optional + property Windows::Foundation::Collections::IVector ^ Commands; // mandatory }; -} +} // namespace CalculatorApp::ViewModel diff --git a/src/CalcViewModel/StandardCalculatorViewModel.cpp b/src/CalcViewModel/StandardCalculatorViewModel.cpp index d39e5848..6a0727ab 100644 --- a/src/CalcViewModel/StandardCalculatorViewModel.cpp +++ b/src/CalcViewModel/StandardCalculatorViewModel.cpp @@ -1789,6 +1789,7 @@ void StandardCalculatorViewModel::SetBitshiftRadioButtonCheckedAnnouncement(Plat StandardCalculatorSnapshot ^ StandardCalculatorViewModel::GetSnapshot() const { + CUnaryCommand; auto result = ref new StandardCalculatorSnapshot(); result->CalcManager = ref new CalcManagerSnapshot(m_standardCalculatorManager); result->PrimaryDisplay = ref new PrimaryDisplaySnapshot(m_DisplayValue, m_IsInError);